• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/builtins/builtins-string-gen.h"
6 
7 #include "src/builtins/builtins-regexp-gen.h"
8 #include "src/builtins/builtins-utils-gen.h"
9 #include "src/builtins/builtins.h"
10 #include "src/codegen/code-factory.h"
11 #include "src/execution/protectors.h"
12 #include "src/heap/factory-inl.h"
13 #include "src/heap/heap-inl.h"
14 #include "src/logging/counters.h"
15 #include "src/objects/objects.h"
16 #include "src/objects/property-cell.h"
17 
18 namespace v8 {
19 namespace internal {
20 
21 using Node = compiler::Node;
22 
DirectStringData(TNode<String> string,TNode<Word32T> string_instance_type)23 TNode<RawPtrT> StringBuiltinsAssembler::DirectStringData(
24     TNode<String> string, TNode<Word32T> string_instance_type) {
25   // Compute the effective offset of the first character.
26   TVARIABLE(RawPtrT, var_data);
27   Label if_sequential(this), if_external(this), if_join(this);
28   Branch(Word32Equal(Word32And(string_instance_type,
29                                Int32Constant(kStringRepresentationMask)),
30                      Int32Constant(kSeqStringTag)),
31          &if_sequential, &if_external);
32 
33   BIND(&if_sequential);
34   {
35     var_data = RawPtrAdd(
36         ReinterpretCast<RawPtrT>(BitcastTaggedToWord(string)),
37         IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag));
38     Goto(&if_join);
39   }
40 
41   BIND(&if_external);
42   {
43     // This is only valid for ExternalStrings where the resource data
44     // pointer is cached (i.e. no uncached external strings).
45     CSA_ASSERT(this, Word32NotEqual(
46                          Word32And(string_instance_type,
47                                    Int32Constant(kUncachedExternalStringMask)),
48                          Int32Constant(kUncachedExternalStringTag)));
49     var_data = LoadExternalStringResourceDataPtr(CAST(string));
50     Goto(&if_join);
51   }
52 
53   BIND(&if_join);
54   return var_data.value();
55 }
56 
DispatchOnStringEncodings(TNode<Word32T> const lhs_instance_type,TNode<Word32T> const rhs_instance_type,Label * if_one_one,Label * if_one_two,Label * if_two_one,Label * if_two_two)57 void StringBuiltinsAssembler::DispatchOnStringEncodings(
58     TNode<Word32T> const lhs_instance_type,
59     TNode<Word32T> const rhs_instance_type, Label* if_one_one,
60     Label* if_one_two, Label* if_two_one, Label* if_two_two) {
61   STATIC_ASSERT(kStringEncodingMask == 0x8);
62   STATIC_ASSERT(kTwoByteStringTag == 0x0);
63   STATIC_ASSERT(kOneByteStringTag == 0x8);
64 
65   // First combine the encodings.
66 
67   const TNode<Int32T> encoding_mask = Int32Constant(kStringEncodingMask);
68   const TNode<Word32T> lhs_encoding =
69       Word32And(lhs_instance_type, encoding_mask);
70   const TNode<Word32T> rhs_encoding =
71       Word32And(rhs_instance_type, encoding_mask);
72 
73   const TNode<Word32T> combined_encodings =
74       Word32Or(lhs_encoding, Word32Shr(rhs_encoding, 1));
75 
76   // Then dispatch on the combined encoding.
77 
78   Label unreachable(this, Label::kDeferred);
79 
80   int32_t values[] = {
81       kOneByteStringTag | (kOneByteStringTag >> 1),
82       kOneByteStringTag | (kTwoByteStringTag >> 1),
83       kTwoByteStringTag | (kOneByteStringTag >> 1),
84       kTwoByteStringTag | (kTwoByteStringTag >> 1),
85   };
86   Label* labels[] = {
87       if_one_one, if_one_two, if_two_one, if_two_two,
88   };
89 
90   STATIC_ASSERT(arraysize(values) == arraysize(labels));
91   Switch(combined_encodings, &unreachable, values, labels, arraysize(values));
92 
93   BIND(&unreachable);
94   Unreachable();
95 }
96 
97 template <typename SubjectChar, typename PatternChar>
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)98 TNode<IntPtrT> StringBuiltinsAssembler::CallSearchStringRaw(
99     const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
100     const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
101     const TNode<IntPtrT> start_position) {
102   const TNode<ExternalReference> function_addr = ExternalConstant(
103       ExternalReference::search_string_raw<SubjectChar, PatternChar>());
104   const TNode<ExternalReference> isolate_ptr =
105       ExternalConstant(ExternalReference::isolate_address(isolate()));
106 
107   MachineType type_ptr = MachineType::Pointer();
108   MachineType type_intptr = MachineType::IntPtr();
109 
110   const TNode<IntPtrT> result = UncheckedCast<IntPtrT>(CallCFunction(
111       function_addr, type_intptr, std::make_pair(type_ptr, isolate_ptr),
112       std::make_pair(type_ptr, subject_ptr),
113       std::make_pair(type_intptr, subject_length),
114       std::make_pair(type_ptr, search_ptr),
115       std::make_pair(type_intptr, search_length),
116       std::make_pair(type_intptr, start_position)));
117 
118   return result;
119 }
120 
PointerToStringDataAtIndex(TNode<RawPtrT> string_data,TNode<IntPtrT> index,String::Encoding encoding)121 TNode<RawPtrT> StringBuiltinsAssembler::PointerToStringDataAtIndex(
122     TNode<RawPtrT> string_data, TNode<IntPtrT> index,
123     String::Encoding encoding) {
124   const ElementsKind kind = (encoding == String::ONE_BYTE_ENCODING)
125                                 ? UINT8_ELEMENTS
126                                 : UINT16_ELEMENTS;
127   TNode<IntPtrT> offset_in_bytes = ElementOffsetFromIndex(index, kind);
128   return RawPtrAdd(string_data, offset_in_bytes);
129 }
130 
GenerateStringEqual(TNode<String> left,TNode<String> right)131 void StringBuiltinsAssembler::GenerateStringEqual(TNode<String> left,
132                                                   TNode<String> right) {
133   TVARIABLE(String, var_left, left);
134   TVARIABLE(String, var_right, right);
135   Label if_equal(this), if_notequal(this), if_indirect(this, Label::kDeferred),
136       restart(this, {&var_left, &var_right});
137 
138   TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(left);
139   TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(right);
140 
141   // Strings with different lengths cannot be equal.
142   GotoIf(WordNotEqual(lhs_length, rhs_length), &if_notequal);
143 
144   Goto(&restart);
145   BIND(&restart);
146   TNode<String> lhs = var_left.value();
147   TNode<String> rhs = var_right.value();
148 
149   TNode<Uint16T> lhs_instance_type = LoadInstanceType(lhs);
150   TNode<Uint16T> rhs_instance_type = LoadInstanceType(rhs);
151 
152   StringEqual_Core(lhs, lhs_instance_type, rhs, rhs_instance_type, lhs_length,
153                    &if_equal, &if_notequal, &if_indirect);
154 
155   BIND(&if_indirect);
156   {
157     // Try to unwrap indirect strings, restart the above attempt on success.
158     MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
159                               rhs_instance_type, &restart);
160 
161     TailCallRuntime(Runtime::kStringEqual, NoContextConstant(), lhs, rhs);
162   }
163 
164   BIND(&if_equal);
165   Return(TrueConstant());
166 
167   BIND(&if_notequal);
168   Return(FalseConstant());
169 }
170 
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)171 void StringBuiltinsAssembler::StringEqual_Core(
172     TNode<String> lhs, TNode<Word32T> lhs_instance_type, TNode<String> rhs,
173     TNode<Word32T> rhs_instance_type, TNode<IntPtrT> length, Label* if_equal,
174     Label* if_not_equal, Label* if_indirect) {
175   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(lhs), length));
176   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(rhs), length));
177   // Fast check to see if {lhs} and {rhs} refer to the same String object.
178   GotoIf(TaggedEqual(lhs, rhs), if_equal);
179 
180   // Combine the instance types into a single 16-bit value, so we can check
181   // both of them at once.
182   TNode<Word32T> both_instance_types = Word32Or(
183       lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
184 
185   // Check if both {lhs} and {rhs} are internalized. Since we already know
186   // that they're not the same object, they're not equal in that case.
187   int const kBothInternalizedMask =
188       kIsNotInternalizedMask | (kIsNotInternalizedMask << 8);
189   int const kBothInternalizedTag = kInternalizedTag | (kInternalizedTag << 8);
190   GotoIf(Word32Equal(Word32And(both_instance_types,
191                                Int32Constant(kBothInternalizedMask)),
192                      Int32Constant(kBothInternalizedTag)),
193          if_not_equal);
194 
195   // Check if both {lhs} and {rhs} are direct strings, and that in case of
196   // ExternalStrings the data pointer is cached.
197   STATIC_ASSERT(kUncachedExternalStringTag != 0);
198   STATIC_ASSERT(kIsIndirectStringTag != 0);
199   int const kBothDirectStringMask =
200       kIsIndirectStringMask | kUncachedExternalStringMask |
201       ((kIsIndirectStringMask | kUncachedExternalStringMask) << 8);
202   GotoIfNot(Word32Equal(Word32And(both_instance_types,
203                                   Int32Constant(kBothDirectStringMask)),
204                         Int32Constant(0)),
205             if_indirect);
206 
207   // Dispatch based on the {lhs} and {rhs} string encoding.
208   int const kBothStringEncodingMask =
209       kStringEncodingMask | (kStringEncodingMask << 8);
210   int const kOneOneByteStringTag = kOneByteStringTag | (kOneByteStringTag << 8);
211   int const kTwoTwoByteStringTag = kTwoByteStringTag | (kTwoByteStringTag << 8);
212   int const kOneTwoByteStringTag = kOneByteStringTag | (kTwoByteStringTag << 8);
213   Label if_oneonebytestring(this), if_twotwobytestring(this),
214       if_onetwobytestring(this), if_twoonebytestring(this);
215   TNode<Word32T> masked_instance_types =
216       Word32And(both_instance_types, Int32Constant(kBothStringEncodingMask));
217   GotoIf(
218       Word32Equal(masked_instance_types, Int32Constant(kOneOneByteStringTag)),
219       &if_oneonebytestring);
220   GotoIf(
221       Word32Equal(masked_instance_types, Int32Constant(kTwoTwoByteStringTag)),
222       &if_twotwobytestring);
223   Branch(
224       Word32Equal(masked_instance_types, Int32Constant(kOneTwoByteStringTag)),
225       &if_onetwobytestring, &if_twoonebytestring);
226 
227   BIND(&if_oneonebytestring);
228   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
229                    rhs_instance_type, MachineType::Uint8(), length, if_equal,
230                    if_not_equal);
231 
232   BIND(&if_twotwobytestring);
233   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
234                    rhs_instance_type, MachineType::Uint16(), length, if_equal,
235                    if_not_equal);
236 
237   BIND(&if_onetwobytestring);
238   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
239                    rhs_instance_type, MachineType::Uint16(), length, if_equal,
240                    if_not_equal);
241 
242   BIND(&if_twoonebytestring);
243   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
244                    rhs_instance_type, MachineType::Uint8(), length, if_equal,
245                    if_not_equal);
246 }
247 
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)248 void StringBuiltinsAssembler::StringEqual_Loop(
249     TNode<String> lhs, TNode<Word32T> lhs_instance_type, MachineType lhs_type,
250     TNode<String> rhs, TNode<Word32T> rhs_instance_type, MachineType rhs_type,
251     TNode<IntPtrT> length, Label* if_equal, Label* if_not_equal) {
252   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(lhs), length));
253   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(rhs), length));
254 
255   // Compute the effective offset of the first character.
256   TNode<RawPtrT> lhs_data = DirectStringData(lhs, lhs_instance_type);
257   TNode<RawPtrT> rhs_data = DirectStringData(rhs, rhs_instance_type);
258 
259   // Loop over the {lhs} and {rhs} strings to see if they are equal.
260   TVARIABLE(IntPtrT, var_offset, IntPtrConstant(0));
261   Label loop(this, &var_offset);
262   Goto(&loop);
263   BIND(&loop);
264   {
265     // If {offset} equals {end}, no difference was found, so the
266     // strings are equal.
267     GotoIf(WordEqual(var_offset.value(), length), if_equal);
268 
269     // Load the next characters from {lhs} and {rhs}.
270     TNode<Word32T> lhs_value = UncheckedCast<Word32T>(
271         Load(lhs_type, lhs_data,
272              WordShl(var_offset.value(),
273                      ElementSizeLog2Of(lhs_type.representation()))));
274     TNode<Word32T> rhs_value = UncheckedCast<Word32T>(
275         Load(rhs_type, rhs_data,
276              WordShl(var_offset.value(),
277                      ElementSizeLog2Of(rhs_type.representation()))));
278 
279     // Check if the characters match.
280     GotoIf(Word32NotEqual(lhs_value, rhs_value), if_not_equal);
281 
282     // Advance to next character.
283     var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
284     Goto(&loop);
285   }
286 }
287 
StringFromSingleUTF16EncodedCodePoint(TNode<Int32T> codepoint)288 TNode<String> StringBuiltinsAssembler::StringFromSingleUTF16EncodedCodePoint(
289     TNode<Int32T> codepoint) {
290   TVARIABLE(String, var_result, EmptyStringConstant());
291 
292   Label if_isword16(this), if_isword32(this), return_result(this);
293 
294   Branch(Uint32LessThan(codepoint, Int32Constant(0x10000)), &if_isword16,
295          &if_isword32);
296 
297   BIND(&if_isword16);
298   {
299     var_result = StringFromSingleCharCode(codepoint);
300     Goto(&return_result);
301   }
302 
303   BIND(&if_isword32);
304   {
305     TNode<String> value = AllocateSeqTwoByteString(2);
306     StoreNoWriteBarrier(
307         MachineRepresentation::kWord32, value,
308         IntPtrConstant(SeqTwoByteString::kHeaderSize - kHeapObjectTag),
309         codepoint);
310     var_result = value;
311     Goto(&return_result);
312   }
313 
314   BIND(&return_result);
315   return var_result.value();
316 }
317 
AllocateConsString(TNode<Uint32T> length,TNode<String> left,TNode<String> right)318 TNode<String> StringBuiltinsAssembler::AllocateConsString(TNode<Uint32T> length,
319                                                           TNode<String> left,
320                                                           TNode<String> right) {
321   // Added string can be a cons string.
322   Comment("Allocating ConsString");
323   TNode<Int32T> left_instance_type = LoadInstanceType(left);
324   TNode<Int32T> right_instance_type = LoadInstanceType(right);
325 
326   // Determine the resulting ConsString map to use depending on whether
327   // any of {left} or {right} has two byte encoding.
328   STATIC_ASSERT(kOneByteStringTag != 0);
329   STATIC_ASSERT(kTwoByteStringTag == 0);
330   TNode<Int32T> combined_instance_type =
331       Word32And(left_instance_type, right_instance_type);
332   TNode<Map> result_map = CAST(Select<Object>(
333       IsSetWord32(combined_instance_type, kStringEncodingMask),
334       [=] { return ConsOneByteStringMapConstant(); },
335       [=] { return ConsStringMapConstant(); }));
336   TNode<HeapObject> result = AllocateInNewSpace(ConsString::kSize);
337   StoreMapNoWriteBarrier(result, result_map);
338   StoreObjectFieldNoWriteBarrier(result, ConsString::kLengthOffset, length);
339   StoreObjectFieldNoWriteBarrier(result, ConsString::kHashFieldOffset,
340                                  Int32Constant(String::kEmptyHashField));
341   StoreObjectFieldNoWriteBarrier(result, ConsString::kFirstOffset, left);
342   StoreObjectFieldNoWriteBarrier(result, ConsString::kSecondOffset, right);
343   return CAST(result);
344 }
345 
StringAdd(TNode<ContextOrEmptyContext> context,TNode<String> left,TNode<String> right)346 TNode<String> StringBuiltinsAssembler::StringAdd(
347     TNode<ContextOrEmptyContext> context, TNode<String> left,
348     TNode<String> right) {
349   CSA_ASSERT(this, IsZeroOrContext(context));
350 
351   TVARIABLE(String, result);
352   Label check_right(this), runtime(this, Label::kDeferred), cons(this),
353       done(this, &result), done_native(this, &result);
354   Counters* counters = isolate()->counters();
355 
356   TNode<Uint32T> left_length = LoadStringLengthAsWord32(left);
357   GotoIfNot(Word32Equal(left_length, Uint32Constant(0)), &check_right);
358   result = right;
359   Goto(&done_native);
360 
361   BIND(&check_right);
362   TNode<Uint32T> right_length = LoadStringLengthAsWord32(right);
363   GotoIfNot(Word32Equal(right_length, Uint32Constant(0)), &cons);
364   result = left;
365   Goto(&done_native);
366 
367   BIND(&cons);
368   {
369     TNode<Uint32T> new_length = Uint32Add(left_length, right_length);
370 
371     // If new length is greater than String::kMaxLength, goto runtime to
372     // throw. Note: we also need to invalidate the string length protector, so
373     // can't just throw here directly.
374     GotoIf(Uint32GreaterThan(new_length, Uint32Constant(String::kMaxLength)),
375            &runtime);
376 
377     TVARIABLE(String, var_left, left);
378     TVARIABLE(String, var_right, right);
379     Label non_cons(this, {&var_left, &var_right});
380     Label slow(this, Label::kDeferred);
381     GotoIf(Uint32LessThan(new_length, Uint32Constant(ConsString::kMinLength)),
382            &non_cons);
383 
384     result =
385         AllocateConsString(new_length, var_left.value(), var_right.value());
386     Goto(&done_native);
387 
388     BIND(&non_cons);
389 
390     Comment("Full string concatenate");
391     TNode<Int32T> left_instance_type = LoadInstanceType(var_left.value());
392     TNode<Int32T> right_instance_type = LoadInstanceType(var_right.value());
393     // Compute intersection and difference of instance types.
394 
395     TNode<Int32T> ored_instance_types =
396         Word32Or(left_instance_type, right_instance_type);
397     TNode<Word32T> xored_instance_types =
398         Word32Xor(left_instance_type, right_instance_type);
399 
400     // Check if both strings have the same encoding and both are sequential.
401     GotoIf(IsSetWord32(xored_instance_types, kStringEncodingMask), &runtime);
402     GotoIf(IsSetWord32(ored_instance_types, kStringRepresentationMask), &slow);
403 
404     TNode<IntPtrT> word_left_length = Signed(ChangeUint32ToWord(left_length));
405     TNode<IntPtrT> word_right_length = Signed(ChangeUint32ToWord(right_length));
406 
407     Label two_byte(this);
408     GotoIf(Word32Equal(Word32And(ored_instance_types,
409                                  Int32Constant(kStringEncodingMask)),
410                        Int32Constant(kTwoByteStringTag)),
411            &two_byte);
412     // One-byte sequential string case
413     result = AllocateSeqOneByteString(new_length);
414     CopyStringCharacters(var_left.value(), result.value(), IntPtrConstant(0),
415                          IntPtrConstant(0), word_left_length,
416                          String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING);
417     CopyStringCharacters(var_right.value(), result.value(), IntPtrConstant(0),
418                          word_left_length, word_right_length,
419                          String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING);
420     Goto(&done_native);
421 
422     BIND(&two_byte);
423     {
424       // Two-byte sequential string case
425       result = AllocateSeqTwoByteString(new_length);
426       CopyStringCharacters(var_left.value(), result.value(), IntPtrConstant(0),
427                            IntPtrConstant(0), word_left_length,
428                            String::TWO_BYTE_ENCODING,
429                            String::TWO_BYTE_ENCODING);
430       CopyStringCharacters(var_right.value(), result.value(), IntPtrConstant(0),
431                            word_left_length, word_right_length,
432                            String::TWO_BYTE_ENCODING,
433                            String::TWO_BYTE_ENCODING);
434       Goto(&done_native);
435     }
436 
437     BIND(&slow);
438     {
439       // Try to unwrap indirect strings, restart the above attempt on success.
440       MaybeDerefIndirectStrings(&var_left, left_instance_type, &var_right,
441                                 right_instance_type, &non_cons);
442       Goto(&runtime);
443     }
444   }
445   BIND(&runtime);
446   {
447     result = CAST(CallRuntime(Runtime::kStringAdd, context, left, right));
448     Goto(&done);
449   }
450 
451   BIND(&done_native);
452   {
453     IncrementCounter(counters->string_add_native(), 1);
454     Goto(&done);
455   }
456 
457   BIND(&done);
458   return result.value();
459 }
460 
BranchIfCanDerefIndirectString(TNode<String> string,TNode<Int32T> instance_type,Label * can_deref,Label * cannot_deref)461 void StringBuiltinsAssembler::BranchIfCanDerefIndirectString(
462     TNode<String> string, TNode<Int32T> instance_type, Label* can_deref,
463     Label* cannot_deref) {
464   TNode<Int32T> representation =
465       Word32And(instance_type, Int32Constant(kStringRepresentationMask));
466   GotoIf(Word32Equal(representation, Int32Constant(kThinStringTag)), can_deref);
467   GotoIf(Word32NotEqual(representation, Int32Constant(kConsStringTag)),
468          cannot_deref);
469   // Cons string.
470   TNode<String> rhs =
471       LoadObjectField<String>(string, ConsString::kSecondOffset);
472   GotoIf(IsEmptyString(rhs), can_deref);
473   Goto(cannot_deref);
474 }
475 
DerefIndirectString(TVariable<String> * var_string,TNode<Int32T> instance_type)476 void StringBuiltinsAssembler::DerefIndirectString(TVariable<String>* var_string,
477                                                   TNode<Int32T> instance_type) {
478 #ifdef DEBUG
479   Label can_deref(this), cannot_deref(this);
480   BranchIfCanDerefIndirectString(var_string->value(), instance_type, &can_deref,
481                                  &cannot_deref);
482   BIND(&cannot_deref);
483   DebugBreak();  // Should be able to dereference string.
484   Goto(&can_deref);
485   BIND(&can_deref);
486 #endif  // DEBUG
487 
488   STATIC_ASSERT(static_cast<int>(ThinString::kActualOffset) ==
489                 static_cast<int>(ConsString::kFirstOffset));
490   *var_string =
491       LoadObjectField<String>(var_string->value(), ThinString::kActualOffset);
492 }
493 
MaybeDerefIndirectString(TVariable<String> * var_string,TNode<Int32T> instance_type,Label * did_deref,Label * cannot_deref)494 void StringBuiltinsAssembler::MaybeDerefIndirectString(
495     TVariable<String>* var_string, TNode<Int32T> instance_type,
496     Label* did_deref, Label* cannot_deref) {
497   Label deref(this);
498   BranchIfCanDerefIndirectString(var_string->value(), instance_type, &deref,
499                                  cannot_deref);
500 
501   BIND(&deref);
502   {
503     DerefIndirectString(var_string, instance_type);
504     Goto(did_deref);
505   }
506 }
507 
MaybeDerefIndirectStrings(TVariable<String> * var_left,TNode<Int32T> left_instance_type,TVariable<String> * var_right,TNode<Int32T> right_instance_type,Label * did_something)508 void StringBuiltinsAssembler::MaybeDerefIndirectStrings(
509     TVariable<String>* var_left, TNode<Int32T> left_instance_type,
510     TVariable<String>* var_right, TNode<Int32T> right_instance_type,
511     Label* did_something) {
512   Label did_nothing_left(this), did_something_left(this),
513       didnt_do_anything(this);
514   MaybeDerefIndirectString(var_left, left_instance_type, &did_something_left,
515                            &did_nothing_left);
516 
517   BIND(&did_something_left);
518   {
519     MaybeDerefIndirectString(var_right, right_instance_type, did_something,
520                              did_something);
521   }
522 
523   BIND(&did_nothing_left);
524   {
525     MaybeDerefIndirectString(var_right, right_instance_type, did_something,
526                              &didnt_do_anything);
527   }
528 
529   BIND(&didnt_do_anything);
530   // Fall through if neither string was an indirect string.
531 }
532 
DerefIndirectString(TNode<String> string,TNode<Int32T> instance_type,Label * cannot_deref)533 TNode<String> StringBuiltinsAssembler::DerefIndirectString(
534     TNode<String> string, TNode<Int32T> instance_type, Label* cannot_deref) {
535   Label deref(this);
536   BranchIfCanDerefIndirectString(string, instance_type, &deref, cannot_deref);
537   BIND(&deref);
538   STATIC_ASSERT(static_cast<int>(ThinString::kActualOffset) ==
539                 static_cast<int>(ConsString::kFirstOffset));
540   return LoadObjectField<String>(string, ThinString::kActualOffset);
541 }
542 
TF_BUILTIN(StringAdd_CheckNone,StringBuiltinsAssembler)543 TF_BUILTIN(StringAdd_CheckNone, StringBuiltinsAssembler) {
544   auto left = Parameter<String>(Descriptor::kLeft);
545   auto right = Parameter<String>(Descriptor::kRight);
546   TNode<ContextOrEmptyContext> context =
547       UncheckedParameter<ContextOrEmptyContext>(Descriptor::kContext);
548   CSA_ASSERT(this, IsZeroOrContext(context));
549   Return(StringAdd(context, left, right));
550 }
551 
TF_BUILTIN(SubString,StringBuiltinsAssembler)552 TF_BUILTIN(SubString, StringBuiltinsAssembler) {
553   auto string = Parameter<String>(Descriptor::kString);
554   auto from = Parameter<Smi>(Descriptor::kFrom);
555   auto to = Parameter<Smi>(Descriptor::kTo);
556   Return(SubString(string, SmiUntag(from), SmiUntag(to)));
557 }
558 
GenerateStringRelationalComparison(TNode<String> left,TNode<String> right,Operation op)559 void StringBuiltinsAssembler::GenerateStringRelationalComparison(
560     TNode<String> left, TNode<String> right, Operation op) {
561   TVARIABLE(String, var_left, left);
562   TVARIABLE(String, var_right, right);
563 
564   Label if_less(this), if_equal(this), if_greater(this);
565   Label restart(this, {&var_left, &var_right});
566   Goto(&restart);
567   BIND(&restart);
568 
569   TNode<String> lhs = var_left.value();
570   TNode<String> rhs = var_right.value();
571   // Fast check to see if {lhs} and {rhs} refer to the same String object.
572   GotoIf(TaggedEqual(lhs, rhs), &if_equal);
573 
574   // Load instance types of {lhs} and {rhs}.
575   TNode<Uint16T> lhs_instance_type = LoadInstanceType(lhs);
576   TNode<Uint16T> rhs_instance_type = LoadInstanceType(rhs);
577 
578   // Combine the instance types into a single 16-bit value, so we can check
579   // both of them at once.
580   TNode<Int32T> both_instance_types = Word32Or(
581       lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
582 
583   // Check that both {lhs} and {rhs} are flat one-byte strings.
584   int const kBothSeqOneByteStringMask =
585       kStringEncodingMask | kStringRepresentationMask |
586       ((kStringEncodingMask | kStringRepresentationMask) << 8);
587   int const kBothSeqOneByteStringTag =
588       kOneByteStringTag | kSeqStringTag |
589       ((kOneByteStringTag | kSeqStringTag) << 8);
590   Label if_bothonebyteseqstrings(this), if_notbothonebyteseqstrings(this);
591   Branch(Word32Equal(Word32And(both_instance_types,
592                                Int32Constant(kBothSeqOneByteStringMask)),
593                      Int32Constant(kBothSeqOneByteStringTag)),
594          &if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
595 
596   BIND(&if_bothonebyteseqstrings);
597   {
598     // Load the length of {lhs} and {rhs}.
599     TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(lhs);
600     TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(rhs);
601 
602     // Determine the minimum length.
603     TNode<IntPtrT> length = IntPtrMin(lhs_length, rhs_length);
604 
605     // Compute the effective offset of the first character.
606     TNode<IntPtrT> begin =
607         IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag);
608 
609     // Compute the first offset after the string from the length.
610     TNode<IntPtrT> end = IntPtrAdd(begin, length);
611 
612     // Loop over the {lhs} and {rhs} strings to see if they are equal.
613     TVARIABLE(IntPtrT, var_offset, begin);
614     Label loop(this, &var_offset);
615     Goto(&loop);
616     BIND(&loop);
617     {
618       // Check if {offset} equals {end}.
619       Label if_done(this), if_notdone(this);
620       Branch(WordEqual(var_offset.value(), end), &if_done, &if_notdone);
621 
622       BIND(&if_notdone);
623       {
624         // Load the next characters from {lhs} and {rhs}.
625         TNode<Uint8T> lhs_value = Load<Uint8T>(lhs, var_offset.value());
626         TNode<Uint8T> rhs_value = Load<Uint8T>(rhs, var_offset.value());
627 
628         // Check if the characters match.
629         Label if_valueissame(this), if_valueisnotsame(this);
630         Branch(Word32Equal(lhs_value, rhs_value), &if_valueissame,
631                &if_valueisnotsame);
632 
633         BIND(&if_valueissame);
634         {
635           // Advance to next character.
636           var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
637         }
638         Goto(&loop);
639 
640         BIND(&if_valueisnotsame);
641         Branch(Uint32LessThan(lhs_value, rhs_value), &if_less, &if_greater);
642       }
643 
644       BIND(&if_done);
645       {
646         // All characters up to the min length are equal, decide based on
647         // string length.
648         GotoIf(IntPtrEqual(lhs_length, rhs_length), &if_equal);
649         Branch(IntPtrLessThan(lhs_length, rhs_length), &if_less, &if_greater);
650       }
651     }
652   }
653 
654   BIND(&if_notbothonebyteseqstrings);
655   {
656     // Try to unwrap indirect strings, restart the above attempt on success.
657     MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
658                               rhs_instance_type, &restart);
659     // TODO(bmeurer): Add support for two byte string relational comparisons.
660     switch (op) {
661       case Operation::kLessThan:
662         TailCallRuntime(Runtime::kStringLessThan, NoContextConstant(), lhs,
663                         rhs);
664         break;
665       case Operation::kLessThanOrEqual:
666         TailCallRuntime(Runtime::kStringLessThanOrEqual, NoContextConstant(),
667                         lhs, rhs);
668         break;
669       case Operation::kGreaterThan:
670         TailCallRuntime(Runtime::kStringGreaterThan, NoContextConstant(), lhs,
671                         rhs);
672         break;
673       case Operation::kGreaterThanOrEqual:
674         TailCallRuntime(Runtime::kStringGreaterThanOrEqual, NoContextConstant(),
675                         lhs, rhs);
676         break;
677       default:
678         UNREACHABLE();
679     }
680   }
681 
682   BIND(&if_less);
683   switch (op) {
684     case Operation::kLessThan:
685     case Operation::kLessThanOrEqual:
686       Return(TrueConstant());
687       break;
688 
689     case Operation::kGreaterThan:
690     case Operation::kGreaterThanOrEqual:
691       Return(FalseConstant());
692       break;
693     default:
694       UNREACHABLE();
695   }
696 
697   BIND(&if_equal);
698   switch (op) {
699     case Operation::kLessThan:
700     case Operation::kGreaterThan:
701       Return(FalseConstant());
702       break;
703 
704     case Operation::kLessThanOrEqual:
705     case Operation::kGreaterThanOrEqual:
706       Return(TrueConstant());
707       break;
708     default:
709       UNREACHABLE();
710   }
711 
712   BIND(&if_greater);
713   switch (op) {
714     case Operation::kLessThan:
715     case Operation::kLessThanOrEqual:
716       Return(FalseConstant());
717       break;
718 
719     case Operation::kGreaterThan:
720     case Operation::kGreaterThanOrEqual:
721       Return(TrueConstant());
722       break;
723     default:
724       UNREACHABLE();
725   }
726 }
727 
TF_BUILTIN(StringEqual,StringBuiltinsAssembler)728 TF_BUILTIN(StringEqual, StringBuiltinsAssembler) {
729   auto left = Parameter<String>(Descriptor::kLeft);
730   auto right = Parameter<String>(Descriptor::kRight);
731   GenerateStringEqual(left, right);
732 }
733 
TF_BUILTIN(StringLessThan,StringBuiltinsAssembler)734 TF_BUILTIN(StringLessThan, StringBuiltinsAssembler) {
735   auto left = Parameter<String>(Descriptor::kLeft);
736   auto right = Parameter<String>(Descriptor::kRight);
737   GenerateStringRelationalComparison(left, right, Operation::kLessThan);
738 }
739 
TF_BUILTIN(StringLessThanOrEqual,StringBuiltinsAssembler)740 TF_BUILTIN(StringLessThanOrEqual, StringBuiltinsAssembler) {
741   auto left = Parameter<String>(Descriptor::kLeft);
742   auto right = Parameter<String>(Descriptor::kRight);
743   GenerateStringRelationalComparison(left, right, Operation::kLessThanOrEqual);
744 }
745 
TF_BUILTIN(StringGreaterThan,StringBuiltinsAssembler)746 TF_BUILTIN(StringGreaterThan, StringBuiltinsAssembler) {
747   auto left = Parameter<String>(Descriptor::kLeft);
748   auto right = Parameter<String>(Descriptor::kRight);
749   GenerateStringRelationalComparison(left, right, Operation::kGreaterThan);
750 }
751 
TF_BUILTIN(StringGreaterThanOrEqual,StringBuiltinsAssembler)752 TF_BUILTIN(StringGreaterThanOrEqual, StringBuiltinsAssembler) {
753   auto left = Parameter<String>(Descriptor::kLeft);
754   auto right = Parameter<String>(Descriptor::kRight);
755   GenerateStringRelationalComparison(left, right,
756                                      Operation::kGreaterThanOrEqual);
757 }
758 
TF_BUILTIN(StringCodePointAt,StringBuiltinsAssembler)759 TF_BUILTIN(StringCodePointAt, StringBuiltinsAssembler) {
760   auto receiver = Parameter<String>(Descriptor::kReceiver);
761   auto position = UncheckedParameter<IntPtrT>(Descriptor::kPosition);
762 
763   // TODO(sigurds) Figure out if passing length as argument pays off.
764   TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
765   // Load the character code at the {position} from the {receiver}.
766   TNode<Int32T> code =
767       LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF32);
768   // And return it as TaggedSigned value.
769   // TODO(turbofan): Allow builtins to return values untagged.
770   TNode<Smi> result = SmiFromInt32(code);
771   Return(result);
772 }
773 
TF_BUILTIN(StringFromCodePointAt,StringBuiltinsAssembler)774 TF_BUILTIN(StringFromCodePointAt, StringBuiltinsAssembler) {
775   auto receiver = Parameter<String>(Descriptor::kReceiver);
776   auto position = UncheckedParameter<IntPtrT>(Descriptor::kPosition);
777 
778   // TODO(sigurds) Figure out if passing length as argument pays off.
779   TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
780   // Load the character code at the {position} from the {receiver}.
781   TNode<Int32T> code =
782       LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF16);
783   // Create a String from the UTF16 encoded code point
784   TNode<String> result = StringFromSingleUTF16EncodedCodePoint(code);
785   Return(result);
786 }
787 
788 // -----------------------------------------------------------------------------
789 // ES6 section 21.1 String Objects
790 
791 // ES6 #sec-string.fromcharcode
TF_BUILTIN(StringFromCharCode,StringBuiltinsAssembler)792 TF_BUILTIN(StringFromCharCode, StringBuiltinsAssembler) {
793   // TODO(ishell): use constants from Descriptor once the JSFunction linkage
794   // arguments are reordered.
795   auto argc = UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount);
796   auto context = Parameter<Context>(Descriptor::kContext);
797 
798   CodeStubArguments arguments(this, argc);
799   // Check if we have exactly one argument (plus the implicit receiver), i.e.
800   // if the parent frame is not an arguments adaptor frame.
801   Label if_oneargument(this), if_notoneargument(this);
802   Branch(Word32Equal(argc, Int32Constant(1)), &if_oneargument,
803          &if_notoneargument);
804 
805   BIND(&if_oneargument);
806   {
807     // Single argument case, perform fast single character string cache lookup
808     // for one-byte code units, or fall back to creating a single character
809     // string on the fly otherwise.
810     TNode<Object> code = arguments.AtIndex(0);
811     TNode<Word32T> code32 = TruncateTaggedToWord32(context, code);
812     TNode<Int32T> code16 =
813         Signed(Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit)));
814     TNode<String> result = StringFromSingleCharCode(code16);
815     arguments.PopAndReturn(result);
816   }
817 
818   TNode<Word32T> code16;
819   BIND(&if_notoneargument);
820   {
821     Label two_byte(this);
822     // Assume that the resulting string contains only one-byte characters.
823     TNode<String> one_byte_result = AllocateSeqOneByteString(Unsigned(argc));
824 
825     TVARIABLE(IntPtrT, var_max_index, IntPtrConstant(0));
826 
827     // Iterate over the incoming arguments, converting them to 8-bit character
828     // codes. Stop if any of the conversions generates a code that doesn't fit
829     // in 8 bits.
830     CodeStubAssembler::VariableList vars({&var_max_index}, zone());
831     arguments.ForEach(vars, [&](TNode<Object> arg) {
832       TNode<Word32T> code32 = TruncateTaggedToWord32(context, arg);
833       code16 = Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
834 
835       GotoIf(
836           Int32GreaterThan(code16, Int32Constant(String::kMaxOneByteCharCode)),
837           &two_byte);
838 
839       // The {code16} fits into the SeqOneByteString {one_byte_result}.
840       TNode<IntPtrT> offset = ElementOffsetFromIndex(
841           var_max_index.value(), UINT8_ELEMENTS,
842           SeqOneByteString::kHeaderSize - kHeapObjectTag);
843       StoreNoWriteBarrier(MachineRepresentation::kWord8, one_byte_result,
844                           offset, code16);
845       var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
846     });
847     arguments.PopAndReturn(one_byte_result);
848 
849     BIND(&two_byte);
850 
851     // At least one of the characters in the string requires a 16-bit
852     // representation.  Allocate a SeqTwoByteString to hold the resulting
853     // string.
854     TNode<String> two_byte_result = AllocateSeqTwoByteString(Unsigned(argc));
855 
856     // Copy the characters that have already been put in the 8-bit string into
857     // their corresponding positions in the new 16-bit string.
858     TNode<IntPtrT> zero = IntPtrConstant(0);
859     CopyStringCharacters(one_byte_result, two_byte_result, zero, zero,
860                          var_max_index.value(), String::ONE_BYTE_ENCODING,
861                          String::TWO_BYTE_ENCODING);
862 
863     // Write the character that caused the 8-bit to 16-bit fault.
864     TNode<IntPtrT> max_index_offset =
865         ElementOffsetFromIndex(var_max_index.value(), UINT16_ELEMENTS,
866                                SeqTwoByteString::kHeaderSize - kHeapObjectTag);
867     StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
868                         max_index_offset, code16);
869     var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
870 
871     // Resume copying the passed-in arguments from the same place where the
872     // 8-bit copy stopped, but this time copying over all of the characters
873     // using a 16-bit representation.
874     arguments.ForEach(
875         vars,
876         [&](TNode<Object> arg) {
877           TNode<Word32T> code32 = TruncateTaggedToWord32(context, arg);
878           TNode<Word32T> code16 =
879               Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
880 
881           TNode<IntPtrT> offset = ElementOffsetFromIndex(
882               var_max_index.value(), UINT16_ELEMENTS,
883               SeqTwoByteString::kHeaderSize - kHeapObjectTag);
884           StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
885                               offset, code16);
886           var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
887         },
888         var_max_index.value());
889 
890     arguments.PopAndReturn(two_byte_result);
891   }
892 }
893 
StringIndexOf(const TNode<String> subject_string,const TNode<String> search_string,const TNode<Smi> position,const std::function<void (TNode<Smi>)> & f_return)894 void StringBuiltinsAssembler::StringIndexOf(
895     const TNode<String> subject_string, const TNode<String> search_string,
896     const TNode<Smi> position,
897     const std::function<void(TNode<Smi>)>& f_return) {
898   const TNode<IntPtrT> int_zero = IntPtrConstant(0);
899   const TNode<IntPtrT> search_length = LoadStringLengthAsWord(search_string);
900   const TNode<IntPtrT> subject_length = LoadStringLengthAsWord(subject_string);
901   const TNode<IntPtrT> start_position = IntPtrMax(SmiUntag(position), int_zero);
902 
903   Label zero_length_needle(this), return_minus_1(this);
904   {
905     GotoIf(IntPtrEqual(int_zero, search_length), &zero_length_needle);
906 
907     // Check that the needle fits in the start position.
908     GotoIfNot(IntPtrLessThanOrEqual(search_length,
909                                     IntPtrSub(subject_length, start_position)),
910               &return_minus_1);
911   }
912 
913   // If the string pointers are identical, we can just return 0. Note that this
914   // implies {start_position} == 0 since we've passed the check above.
915   Label return_zero(this);
916   GotoIf(TaggedEqual(subject_string, search_string), &return_zero);
917 
918   // Try to unpack subject and search strings. Bail to runtime if either needs
919   // to be flattened.
920   ToDirectStringAssembler subject_to_direct(state(), subject_string);
921   ToDirectStringAssembler search_to_direct(state(), search_string);
922 
923   Label call_runtime_unchecked(this, Label::kDeferred);
924 
925   subject_to_direct.TryToDirect(&call_runtime_unchecked);
926   search_to_direct.TryToDirect(&call_runtime_unchecked);
927 
928   // Load pointers to string data.
929   const TNode<RawPtrT> subject_ptr =
930       subject_to_direct.PointerToData(&call_runtime_unchecked);
931   const TNode<RawPtrT> search_ptr =
932       search_to_direct.PointerToData(&call_runtime_unchecked);
933 
934   const TNode<IntPtrT> subject_offset = subject_to_direct.offset();
935   const TNode<IntPtrT> search_offset = search_to_direct.offset();
936 
937   // Like String::IndexOf, the actual matching is done by the optimized
938   // SearchString method in string-search.h. Dispatch based on string instance
939   // types, then call straight into C++ for matching.
940 
941   CSA_ASSERT(this, IntPtrGreaterThan(search_length, int_zero));
942   CSA_ASSERT(this, IntPtrGreaterThanOrEqual(start_position, int_zero));
943   CSA_ASSERT(this, IntPtrGreaterThanOrEqual(subject_length, start_position));
944   CSA_ASSERT(this,
945              IntPtrLessThanOrEqual(search_length,
946                                    IntPtrSub(subject_length, start_position)));
947 
948   Label one_one(this), one_two(this), two_one(this), two_two(this);
949   DispatchOnStringEncodings(subject_to_direct.instance_type(),
950                             search_to_direct.instance_type(), &one_one,
951                             &one_two, &two_one, &two_two);
952 
953   using onebyte_t = const uint8_t;
954   using twobyte_t = const uc16;
955 
956   BIND(&one_one);
957   {
958     const TNode<RawPtrT> adjusted_subject_ptr = PointerToStringDataAtIndex(
959         subject_ptr, subject_offset, String::ONE_BYTE_ENCODING);
960     const TNode<RawPtrT> adjusted_search_ptr = PointerToStringDataAtIndex(
961         search_ptr, search_offset, String::ONE_BYTE_ENCODING);
962 
963     Label direct_memchr_call(this), generic_fast_path(this);
964     Branch(IntPtrEqual(search_length, IntPtrConstant(1)), &direct_memchr_call,
965            &generic_fast_path);
966 
967     // An additional fast path that calls directly into memchr for 1-length
968     // search strings.
969     BIND(&direct_memchr_call);
970     {
971       const TNode<RawPtrT> string_addr =
972           RawPtrAdd(adjusted_subject_ptr, start_position);
973       const TNode<IntPtrT> search_length =
974           IntPtrSub(subject_length, start_position);
975       const TNode<IntPtrT> search_byte =
976           ChangeInt32ToIntPtr(Load<Uint8T>(adjusted_search_ptr));
977 
978       const TNode<ExternalReference> memchr =
979           ExternalConstant(ExternalReference::libc_memchr_function());
980       const TNode<RawPtrT> result_address = UncheckedCast<RawPtrT>(
981           CallCFunction(memchr, MachineType::Pointer(),
982                         std::make_pair(MachineType::Pointer(), string_addr),
983                         std::make_pair(MachineType::IntPtr(), search_byte),
984                         std::make_pair(MachineType::UintPtr(), search_length)));
985       GotoIf(WordEqual(result_address, int_zero), &return_minus_1);
986       const TNode<IntPtrT> result_index =
987           IntPtrAdd(RawPtrSub(result_address, string_addr), start_position);
988       f_return(SmiTag(result_index));
989     }
990 
991     BIND(&generic_fast_path);
992     {
993       const TNode<IntPtrT> result = CallSearchStringRaw<onebyte_t, onebyte_t>(
994           adjusted_subject_ptr, subject_length, adjusted_search_ptr,
995           search_length, start_position);
996       f_return(SmiTag(result));
997     }
998   }
999 
1000   BIND(&one_two);
1001   {
1002     const TNode<RawPtrT> adjusted_subject_ptr = PointerToStringDataAtIndex(
1003         subject_ptr, subject_offset, String::ONE_BYTE_ENCODING);
1004     const TNode<RawPtrT> adjusted_search_ptr = PointerToStringDataAtIndex(
1005         search_ptr, search_offset, String::TWO_BYTE_ENCODING);
1006 
1007     const TNode<IntPtrT> result = CallSearchStringRaw<onebyte_t, twobyte_t>(
1008         adjusted_subject_ptr, subject_length, adjusted_search_ptr,
1009         search_length, start_position);
1010     f_return(SmiTag(result));
1011   }
1012 
1013   BIND(&two_one);
1014   {
1015     const TNode<RawPtrT> adjusted_subject_ptr = PointerToStringDataAtIndex(
1016         subject_ptr, subject_offset, String::TWO_BYTE_ENCODING);
1017     const TNode<RawPtrT> adjusted_search_ptr = PointerToStringDataAtIndex(
1018         search_ptr, search_offset, String::ONE_BYTE_ENCODING);
1019 
1020     const TNode<IntPtrT> result = CallSearchStringRaw<twobyte_t, onebyte_t>(
1021         adjusted_subject_ptr, subject_length, adjusted_search_ptr,
1022         search_length, start_position);
1023     f_return(SmiTag(result));
1024   }
1025 
1026   BIND(&two_two);
1027   {
1028     const TNode<RawPtrT> adjusted_subject_ptr = PointerToStringDataAtIndex(
1029         subject_ptr, subject_offset, String::TWO_BYTE_ENCODING);
1030     const TNode<RawPtrT> adjusted_search_ptr = PointerToStringDataAtIndex(
1031         search_ptr, search_offset, String::TWO_BYTE_ENCODING);
1032 
1033     const TNode<IntPtrT> result = CallSearchStringRaw<twobyte_t, twobyte_t>(
1034         adjusted_subject_ptr, subject_length, adjusted_search_ptr,
1035         search_length, start_position);
1036     f_return(SmiTag(result));
1037   }
1038 
1039   BIND(&return_minus_1);
1040   f_return(SmiConstant(-1));
1041 
1042   BIND(&return_zero);
1043   f_return(SmiConstant(0));
1044 
1045   BIND(&zero_length_needle);
1046   {
1047     Comment("0-length search_string");
1048     f_return(SmiTag(IntPtrMin(subject_length, start_position)));
1049   }
1050 
1051   BIND(&call_runtime_unchecked);
1052   {
1053     // Simplified version of the runtime call where the types of the arguments
1054     // are already known due to type checks in this stub.
1055     Comment("Call Runtime Unchecked");
1056     TNode<Smi> result =
1057         CAST(CallRuntime(Runtime::kStringIndexOfUnchecked, NoContextConstant(),
1058                          subject_string, search_string, position));
1059     f_return(result);
1060   }
1061 }
1062 
1063 // ES6 String.prototype.indexOf(searchString [, position])
1064 // #sec-string.prototype.indexof
1065 // Unchecked helper for builtins lowering.
TF_BUILTIN(StringIndexOf,StringBuiltinsAssembler)1066 TF_BUILTIN(StringIndexOf, StringBuiltinsAssembler) {
1067   auto receiver = Parameter<String>(Descriptor::kReceiver);
1068   auto search_string = Parameter<String>(Descriptor::kSearchString);
1069   auto position = Parameter<Smi>(Descriptor::kPosition);
1070   StringIndexOf(receiver, search_string, position,
1071                 [this](TNode<Smi> result) { this->Return(result); });
1072 }
1073 
1074 // ES6 String.prototype.includes(searchString [, position])
1075 // #sec-string.prototype.includes
TF_BUILTIN(StringPrototypeIncludes,StringIncludesIndexOfAssembler)1076 TF_BUILTIN(StringPrototypeIncludes, StringIncludesIndexOfAssembler) {
1077   TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
1078       UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
1079   auto context = Parameter<Context>(Descriptor::kContext);
1080   Generate(kIncludes, argc, context);
1081 }
1082 
1083 // ES6 String.prototype.indexOf(searchString [, position])
1084 // #sec-string.prototype.indexof
TF_BUILTIN(StringPrototypeIndexOf,StringIncludesIndexOfAssembler)1085 TF_BUILTIN(StringPrototypeIndexOf, StringIncludesIndexOfAssembler) {
1086   TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
1087       UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
1088   auto context = Parameter<Context>(Descriptor::kContext);
1089   Generate(kIndexOf, argc, context);
1090 }
1091 
Generate(SearchVariant variant,TNode<IntPtrT> argc,TNode<Context> context)1092 void StringIncludesIndexOfAssembler::Generate(SearchVariant variant,
1093                                               TNode<IntPtrT> argc,
1094                                               TNode<Context> context) {
1095   CodeStubArguments arguments(this, argc);
1096   const TNode<Object> receiver = arguments.GetReceiver();
1097 
1098   TVARIABLE(Object, var_search_string);
1099   TVARIABLE(Object, var_position);
1100   Label argc_1(this), argc_2(this), call_runtime(this, Label::kDeferred),
1101       fast_path(this);
1102 
1103   GotoIf(IntPtrEqual(argc, IntPtrConstant(1)), &argc_1);
1104   GotoIf(IntPtrGreaterThan(argc, IntPtrConstant(1)), &argc_2);
1105   {
1106     Comment("0 Argument case");
1107     CSA_ASSERT(this, IntPtrEqual(argc, IntPtrConstant(0)));
1108     TNode<Oddball> undefined = UndefinedConstant();
1109     var_search_string = undefined;
1110     var_position = undefined;
1111     Goto(&call_runtime);
1112   }
1113   BIND(&argc_1);
1114   {
1115     Comment("1 Argument case");
1116     var_search_string = arguments.AtIndex(0);
1117     var_position = SmiConstant(0);
1118     Goto(&fast_path);
1119   }
1120   BIND(&argc_2);
1121   {
1122     Comment("2 Argument case");
1123     var_search_string = arguments.AtIndex(0);
1124     var_position = arguments.AtIndex(1);
1125     GotoIfNot(TaggedIsSmi(var_position.value()), &call_runtime);
1126     Goto(&fast_path);
1127   }
1128   BIND(&fast_path);
1129   {
1130     Comment("Fast Path");
1131     const TNode<Object> search = var_search_string.value();
1132     const TNode<Smi> position = CAST(var_position.value());
1133     GotoIf(TaggedIsSmi(receiver), &call_runtime);
1134     GotoIf(TaggedIsSmi(search), &call_runtime);
1135     GotoIfNot(IsString(CAST(receiver)), &call_runtime);
1136     GotoIfNot(IsString(CAST(search)), &call_runtime);
1137 
1138     StringIndexOf(CAST(receiver), CAST(search), position,
1139                   [&](TNode<Smi> result) {
1140                     if (variant == kIndexOf) {
1141                       arguments.PopAndReturn(result);
1142                     } else {
1143                       arguments.PopAndReturn(SelectBooleanConstant(
1144                           SmiGreaterThanOrEqual(result, SmiConstant(0))));
1145                     }
1146                   });
1147   }
1148   BIND(&call_runtime);
1149   {
1150     Comment("Call Runtime");
1151     Runtime::FunctionId runtime = variant == kIndexOf
1152                                       ? Runtime::kStringIndexOf
1153                                       : Runtime::kStringIncludes;
1154     const TNode<Object> result =
1155         CallRuntime(runtime, context, receiver, var_search_string.value(),
1156                     var_position.value());
1157     arguments.PopAndReturn(result);
1158   }
1159 }
1160 
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)1161 void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
1162     const TNode<Context> context, const TNode<Object> object,
1163     const TNode<Object> maybe_string, Handle<Symbol> symbol,
1164     DescriptorIndexNameValue additional_property_to_check,
1165     const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
1166   Label out(this);
1167   Label get_property_lookup(this);
1168 
1169   // Smis have to go through the GetProperty lookup in case Number.prototype or
1170   // Object.prototype was modified.
1171   GotoIf(TaggedIsSmi(object), &get_property_lookup);
1172 
1173   // Take the fast path for RegExps.
1174   // There's two conditions: {object} needs to be a fast regexp, and
1175   // {maybe_string} must be a string (we can't call ToString on the fast path
1176   // since it may mutate {object}).
1177   {
1178     Label stub_call(this), slow_lookup(this);
1179 
1180     TNode<HeapObject> heap_object = CAST(object);
1181 
1182     GotoIf(TaggedIsSmi(maybe_string), &slow_lookup);
1183     GotoIfNot(IsString(CAST(maybe_string)), &slow_lookup);
1184 
1185     // Note we don't run a full (= permissive) check here, because passing the
1186     // check implies calling the fast variants of target builtins, which assume
1187     // we've already made their appropriate fast path checks. This is not the
1188     // case though; e.g.: some of the target builtins access flag getters.
1189     // TODO(jgruber): Handle slow flag accesses on the fast path and make this
1190     // permissive.
1191     RegExpBuiltinsAssembler regexp_asm(state());
1192     regexp_asm.BranchIfFastRegExp(
1193         context, heap_object, LoadMap(heap_object),
1194         PrototypeCheckAssembler::kCheckPrototypePropertyConstness,
1195         additional_property_to_check, &stub_call, &slow_lookup);
1196 
1197     BIND(&stub_call);
1198     // TODO(jgruber): Add a no-JS scope once it exists.
1199     regexp_call();
1200 
1201     BIND(&slow_lookup);
1202     // Special case null and undefined to skip the property lookup.
1203     Branch(IsNullOrUndefined(heap_object), &out, &get_property_lookup);
1204   }
1205 
1206   // Fall back to a slow lookup of {heap_object[symbol]}.
1207   //
1208   // The spec uses GetMethod({heap_object}, {symbol}), which has a few quirks:
1209   // * null values are turned into undefined, and
1210   // * an exception is thrown if the value is not undefined, null, or callable.
1211   // We handle the former by jumping to {out} for null values as well, while
1212   // the latter is already handled by the Call({maybe_func}) operation.
1213 
1214   BIND(&get_property_lookup);
1215   const TNode<Object> maybe_func = GetProperty(context, object, symbol);
1216   GotoIf(IsUndefined(maybe_func), &out);
1217   GotoIf(IsNull(maybe_func), &out);
1218 
1219   // Attempt to call the function.
1220   generic_call(maybe_func);
1221 
1222   BIND(&out);
1223 }
1224 
IndexOfDollarChar(const TNode<Context> context,const TNode<String> string)1225 const TNode<Smi> StringBuiltinsAssembler::IndexOfDollarChar(
1226     const TNode<Context> context, const TNode<String> string) {
1227   const TNode<String> dollar_string = HeapConstant(
1228       isolate()->factory()->LookupSingleCharacterStringFromCode('$'));
1229   const TNode<Smi> dollar_ix =
1230       CAST(CallBuiltin(Builtins::kStringIndexOf, context, string, dollar_string,
1231                        SmiConstant(0)));
1232   return dollar_ix;
1233 }
1234 
GetSubstitution(TNode<Context> context,TNode<String> subject_string,TNode<Smi> match_start_index,TNode<Smi> match_end_index,TNode<String> replace_string)1235 TNode<String> StringBuiltinsAssembler::GetSubstitution(
1236     TNode<Context> context, TNode<String> subject_string,
1237     TNode<Smi> match_start_index, TNode<Smi> match_end_index,
1238     TNode<String> replace_string) {
1239   CSA_ASSERT(this, TaggedIsPositiveSmi(match_start_index));
1240   CSA_ASSERT(this, TaggedIsPositiveSmi(match_end_index));
1241 
1242   TVARIABLE(String, var_result, replace_string);
1243   Label runtime(this), out(this);
1244 
1245   // In this primitive implementation we simply look for the next '$' char in
1246   // {replace_string}. If it doesn't exist, we can simply return
1247   // {replace_string} itself. If it does, then we delegate to
1248   // String::GetSubstitution, passing in the index of the first '$' to avoid
1249   // repeated scanning work.
1250   // TODO(jgruber): Possibly extend this in the future to handle more complex
1251   // cases without runtime calls.
1252 
1253   const TNode<Smi> dollar_index = IndexOfDollarChar(context, replace_string);
1254   Branch(SmiIsNegative(dollar_index), &out, &runtime);
1255 
1256   BIND(&runtime);
1257   {
1258     CSA_ASSERT(this, TaggedIsPositiveSmi(dollar_index));
1259 
1260     const TNode<Object> matched =
1261         CallBuiltin(Builtins::kStringSubstring, context, subject_string,
1262                     SmiUntag(match_start_index), SmiUntag(match_end_index));
1263     const TNode<String> replacement_string = CAST(
1264         CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
1265                     match_start_index, replace_string, dollar_index));
1266     var_result = replacement_string;
1267 
1268     Goto(&out);
1269   }
1270 
1271   BIND(&out);
1272   return var_result.value();
1273 }
1274 
1275 // ES6 #sec-string.prototype.replace
TF_BUILTIN(StringPrototypeReplace,StringBuiltinsAssembler)1276 TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
1277   Label out(this);
1278 
1279   auto receiver = Parameter<Object>(Descriptor::kReceiver);
1280   const auto search = Parameter<Object>(Descriptor::kSearch);
1281   const auto replace = Parameter<Object>(Descriptor::kReplace);
1282   auto context = Parameter<Context>(Descriptor::kContext);
1283 
1284   const TNode<Smi> smi_zero = SmiConstant(0);
1285 
1286   RequireObjectCoercible(context, receiver, "String.prototype.replace");
1287 
1288   // Redirect to replacer method if {search[@@replace]} is not undefined.
1289 
1290   MaybeCallFunctionAtSymbol(
1291       context, search, receiver, isolate()->factory()->replace_symbol(),
1292       DescriptorIndexNameValue{JSRegExp::kSymbolReplaceFunctionDescriptorIndex,
1293                                RootIndex::kreplace_symbol,
1294                                Context::REGEXP_REPLACE_FUNCTION_INDEX},
1295       [=]() {
1296         Return(CallBuiltin(Builtins::kRegExpReplace, context, search, receiver,
1297                            replace));
1298       },
1299       [=](TNode<Object> fn) {
1300         Return(Call(context, fn, search, receiver, replace));
1301       });
1302 
1303   // Convert {receiver} and {search} to strings.
1304 
1305   const TNode<String> subject_string = ToString_Inline(context, receiver);
1306   const TNode<String> search_string = ToString_Inline(context, search);
1307 
1308   const TNode<IntPtrT> subject_length = LoadStringLengthAsWord(subject_string);
1309   const TNode<IntPtrT> search_length = LoadStringLengthAsWord(search_string);
1310 
1311   // Fast-path single-char {search}, long cons {receiver}, and simple string
1312   // {replace}.
1313   {
1314     Label next(this);
1315 
1316     GotoIfNot(WordEqual(search_length, IntPtrConstant(1)), &next);
1317     GotoIfNot(IntPtrGreaterThan(subject_length, IntPtrConstant(0xFF)), &next);
1318     GotoIf(TaggedIsSmi(replace), &next);
1319     GotoIfNot(IsString(CAST(replace)), &next);
1320 
1321     TNode<String> replace_string = CAST(replace);
1322     const TNode<Uint16T> subject_instance_type =
1323         LoadInstanceType(subject_string);
1324     GotoIfNot(IsConsStringInstanceType(subject_instance_type), &next);
1325 
1326     GotoIf(TaggedIsPositiveSmi(IndexOfDollarChar(context, replace_string)),
1327            &next);
1328 
1329     // Searching by traversing a cons string tree and replace with cons of
1330     // slices works only when the replaced string is a single character, being
1331     // replaced by a simple string and only pays off for long strings.
1332     // TODO(jgruber): Reevaluate if this is still beneficial.
1333     // TODO(jgruber): TailCallRuntime when it correctly handles adapter frames.
1334     Return(CallRuntime(Runtime::kStringReplaceOneCharWithString, context,
1335                        subject_string, search_string, replace_string));
1336 
1337     BIND(&next);
1338   }
1339 
1340   // TODO(jgruber): Extend StringIndexOf to handle two-byte strings and
1341   // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars
1342   // (2-byte).
1343 
1344   const TNode<Smi> match_start_index =
1345       CAST(CallBuiltin(Builtins::kStringIndexOf, context, subject_string,
1346                        search_string, smi_zero));
1347 
1348   // Early exit if no match found.
1349   {
1350     Label next(this), return_subject(this);
1351 
1352     GotoIfNot(SmiIsNegative(match_start_index), &next);
1353 
1354     // The spec requires to perform ToString(replace) if the {replace} is not
1355     // callable even if we are going to exit here.
1356     // Since ToString() being applied to Smi does not have side effects for
1357     // numbers we can skip it.
1358     GotoIf(TaggedIsSmi(replace), &return_subject);
1359     GotoIf(IsCallableMap(LoadMap(CAST(replace))), &return_subject);
1360 
1361     // TODO(jgruber): Could introduce ToStringSideeffectsStub which only
1362     // performs observable parts of ToString.
1363     ToString_Inline(context, replace);
1364     Goto(&return_subject);
1365 
1366     BIND(&return_subject);
1367     Return(subject_string);
1368 
1369     BIND(&next);
1370   }
1371 
1372   const TNode<Smi> match_end_index =
1373       SmiAdd(match_start_index, SmiFromIntPtr(search_length));
1374 
1375   TVARIABLE(String, var_result, EmptyStringConstant());
1376 
1377   // Compute the prefix.
1378   {
1379     Label next(this);
1380 
1381     GotoIf(SmiEqual(match_start_index, smi_zero), &next);
1382     const TNode<String> prefix =
1383         CAST(CallBuiltin(Builtins::kStringSubstring, context, subject_string,
1384                          IntPtrConstant(0), SmiUntag(match_start_index)));
1385     var_result = prefix;
1386 
1387     Goto(&next);
1388     BIND(&next);
1389   }
1390 
1391   // Compute the string to replace with.
1392 
1393   Label if_iscallablereplace(this), if_notcallablereplace(this);
1394   GotoIf(TaggedIsSmi(replace), &if_notcallablereplace);
1395   Branch(IsCallableMap(LoadMap(CAST(replace))), &if_iscallablereplace,
1396          &if_notcallablereplace);
1397 
1398   BIND(&if_iscallablereplace);
1399   {
1400     const TNode<Object> replacement =
1401         Call(context, replace, UndefinedConstant(), search_string,
1402              match_start_index, subject_string);
1403     const TNode<String> replacement_string =
1404         ToString_Inline(context, replacement);
1405     var_result = CAST(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
1406                                   var_result.value(), replacement_string));
1407     Goto(&out);
1408   }
1409 
1410   BIND(&if_notcallablereplace);
1411   {
1412     const TNode<String> replace_string = ToString_Inline(context, replace);
1413     const TNode<Object> replacement =
1414         GetSubstitution(context, subject_string, match_start_index,
1415                         match_end_index, replace_string);
1416     var_result = CAST(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
1417                                   var_result.value(), replacement));
1418     Goto(&out);
1419   }
1420 
1421   BIND(&out);
1422   {
1423     const TNode<Object> suffix =
1424         CallBuiltin(Builtins::kStringSubstring, context, subject_string,
1425                     SmiUntag(match_end_index), subject_length);
1426     const TNode<Object> result = CallBuiltin(
1427         Builtins::kStringAdd_CheckNone, context, var_result.value(), suffix);
1428     Return(result);
1429   }
1430 }
1431 
1432 class StringMatchSearchAssembler : public StringBuiltinsAssembler {
1433  public:
StringMatchSearchAssembler(compiler::CodeAssemblerState * state)1434   explicit StringMatchSearchAssembler(compiler::CodeAssemblerState* state)
1435       : StringBuiltinsAssembler(state) {}
1436 
1437  protected:
1438   enum Variant { kMatch, kSearch };
1439 
Generate(Variant variant,const char * method_name,TNode<Object> receiver,TNode<Object> maybe_regexp,TNode<Context> context)1440   void Generate(Variant variant, const char* method_name,
1441                 TNode<Object> receiver, TNode<Object> maybe_regexp,
1442                 TNode<Context> context) {
1443     Label call_regexp_match_search(this);
1444 
1445     Builtins::Name builtin;
1446     Handle<Symbol> symbol;
1447     DescriptorIndexNameValue property_to_check;
1448     if (variant == kMatch) {
1449       builtin = Builtins::kRegExpMatchFast;
1450       symbol = isolate()->factory()->match_symbol();
1451       property_to_check = DescriptorIndexNameValue{
1452           JSRegExp::kSymbolMatchFunctionDescriptorIndex,
1453           RootIndex::kmatch_symbol, Context::REGEXP_MATCH_FUNCTION_INDEX};
1454     } else {
1455       builtin = Builtins::kRegExpSearchFast;
1456       symbol = isolate()->factory()->search_symbol();
1457       property_to_check = DescriptorIndexNameValue{
1458           JSRegExp::kSymbolSearchFunctionDescriptorIndex,
1459           RootIndex::ksearch_symbol, Context::REGEXP_SEARCH_FUNCTION_INDEX};
1460     }
1461 
1462     RequireObjectCoercible(context, receiver, method_name);
1463 
1464     MaybeCallFunctionAtSymbol(
1465         context, maybe_regexp, receiver, symbol, property_to_check,
1466         [=] { Return(CallBuiltin(builtin, context, maybe_regexp, receiver)); },
1467         [=](TNode<Object> fn) {
1468           Return(Call(context, fn, maybe_regexp, receiver));
1469         });
1470 
1471     // maybe_regexp is not a RegExp nor has [@@match / @@search] property.
1472     {
1473       RegExpBuiltinsAssembler regexp_asm(state());
1474 
1475       TNode<String> receiver_string = ToString_Inline(context, receiver);
1476       TNode<NativeContext> native_context = LoadNativeContext(context);
1477       TNode<HeapObject> regexp_function = CAST(
1478           LoadContextElement(native_context, Context::REGEXP_FUNCTION_INDEX));
1479       TNode<Map> initial_map = CAST(LoadObjectField(
1480           regexp_function, JSFunction::kPrototypeOrInitialMapOffset));
1481       TNode<Object> regexp = regexp_asm.RegExpCreate(
1482           context, initial_map, maybe_regexp, EmptyStringConstant());
1483 
1484       // TODO(jgruber): Handle slow flag accesses on the fast path and make this
1485       // permissive.
1486       Label fast_path(this), slow_path(this);
1487       regexp_asm.BranchIfFastRegExp(
1488           context, CAST(regexp), initial_map,
1489           PrototypeCheckAssembler::kCheckPrototypePropertyConstness,
1490           property_to_check, &fast_path, &slow_path);
1491 
1492       BIND(&fast_path);
1493       Return(CallBuiltin(builtin, context, regexp, receiver_string));
1494 
1495       BIND(&slow_path);
1496       {
1497         TNode<Object> maybe_func = GetProperty(context, regexp, symbol);
1498         Return(Call(context, maybe_func, regexp, receiver_string));
1499       }
1500     }
1501   }
1502 };
1503 
1504 // ES6 #sec-string.prototype.match
TF_BUILTIN(StringPrototypeMatch,StringMatchSearchAssembler)1505 TF_BUILTIN(StringPrototypeMatch, StringMatchSearchAssembler) {
1506   auto receiver = Parameter<Object>(Descriptor::kReceiver);
1507   auto maybe_regexp = Parameter<Object>(Descriptor::kRegexp);
1508   auto context = Parameter<Context>(Descriptor::kContext);
1509 
1510   Generate(kMatch, "String.prototype.match", receiver, maybe_regexp, context);
1511 }
1512 
1513 // ES #sec-string.prototype.matchAll
TF_BUILTIN(StringPrototypeMatchAll,StringBuiltinsAssembler)1514 TF_BUILTIN(StringPrototypeMatchAll, StringBuiltinsAssembler) {
1515   char const* method_name = "String.prototype.matchAll";
1516 
1517   auto context = Parameter<Context>(Descriptor::kContext);
1518   auto maybe_regexp = Parameter<Object>(Descriptor::kRegexp);
1519   auto receiver = Parameter<Object>(Descriptor::kReceiver);
1520   TNode<NativeContext> native_context = LoadNativeContext(context);
1521 
1522   // 1. Let O be ? RequireObjectCoercible(this value).
1523   RequireObjectCoercible(context, receiver, method_name);
1524 
1525   RegExpMatchAllAssembler regexp_asm(state());
1526   {
1527     Label fast(this), slow(this, Label::kDeferred),
1528         throw_exception(this, Label::kDeferred),
1529         throw_flags_exception(this, Label::kDeferred), next(this);
1530 
1531     // 2. If regexp is neither undefined nor null, then
1532     //   a. Let isRegExp be ? IsRegExp(regexp).
1533     //   b. If isRegExp is true, then
1534     //     i. Let flags be ? Get(regexp, "flags").
1535     //    ii. Perform ? RequireObjectCoercible(flags).
1536     //   iii. If ? ToString(flags) does not contain "g", throw a
1537     //        TypeError exception.
1538     GotoIf(TaggedIsSmi(maybe_regexp), &next);
1539     TNode<HeapObject> heap_maybe_regexp = CAST(maybe_regexp);
1540     regexp_asm.BranchIfFastRegExp_Strict(context, heap_maybe_regexp, &fast,
1541                                          &slow);
1542 
1543     BIND(&fast);
1544     {
1545       TNode<BoolT> is_global = regexp_asm.FlagGetter(context, heap_maybe_regexp,
1546                                                      JSRegExp::kGlobal, true);
1547       Branch(is_global, &next, &throw_exception);
1548     }
1549 
1550     BIND(&slow);
1551     {
1552       GotoIfNot(regexp_asm.IsRegExp(native_context, heap_maybe_regexp), &next);
1553 
1554       TNode<Object> flags = GetProperty(context, heap_maybe_regexp,
1555                                         isolate()->factory()->flags_string());
1556       // TODO(syg): Implement a RequireObjectCoercible with more flexible error
1557       // messages.
1558       GotoIf(IsNullOrUndefined(flags), &throw_flags_exception);
1559 
1560       TNode<String> flags_string = ToString_Inline(context, flags);
1561       TNode<String> global_char_string = StringConstant("g");
1562       TNode<Smi> global_ix =
1563           CAST(CallBuiltin(Builtins::kStringIndexOf, context, flags_string,
1564                            global_char_string, SmiConstant(0)));
1565       Branch(SmiEqual(global_ix, SmiConstant(-1)), &throw_exception, &next);
1566     }
1567 
1568     BIND(&throw_exception);
1569     ThrowTypeError(context, MessageTemplate::kRegExpGlobalInvokedOnNonGlobal,
1570                    method_name);
1571 
1572     BIND(&throw_flags_exception);
1573     ThrowTypeError(context,
1574                    MessageTemplate::kStringMatchAllNullOrUndefinedFlags);
1575 
1576     BIND(&next);
1577   }
1578   //   a. Let matcher be ? GetMethod(regexp, @@matchAll).
1579   //   b. If matcher is not undefined, then
1580   //     i. Return ? Call(matcher, regexp, « O »).
1581   auto if_regexp_call = [&] {
1582     // MaybeCallFunctionAtSymbol guarantees fast path is chosen only if
1583     // maybe_regexp is a fast regexp and receiver is a string.
1584     TNode<String> s = CAST(receiver);
1585 
1586     Return(
1587         RegExpPrototypeMatchAllImpl(context, native_context, maybe_regexp, s));
1588   };
1589   auto if_generic_call = [=](TNode<Object> fn) {
1590     Return(Call(context, fn, maybe_regexp, receiver));
1591   };
1592   MaybeCallFunctionAtSymbol(
1593       context, maybe_regexp, receiver, isolate()->factory()->match_all_symbol(),
1594       DescriptorIndexNameValue{JSRegExp::kSymbolMatchAllFunctionDescriptorIndex,
1595                                RootIndex::kmatch_all_symbol,
1596                                Context::REGEXP_MATCH_ALL_FUNCTION_INDEX},
1597       if_regexp_call, if_generic_call);
1598 
1599   // 3. Let S be ? ToString(O).
1600   TNode<String> s = ToString_Inline(context, receiver);
1601 
1602   // 4. Let rx be ? RegExpCreate(R, "g").
1603   TNode<Object> rx = regexp_asm.RegExpCreate(context, native_context,
1604                                              maybe_regexp, StringConstant("g"));
1605 
1606   // 5. Return ? Invoke(rx, @@matchAll, « S »).
1607   TNode<Object> match_all_func =
1608       GetProperty(context, rx, isolate()->factory()->match_all_symbol());
1609   Return(Call(context, match_all_func, rx, s));
1610 }
1611 
1612 // ES6 #sec-string.prototype.search
TF_BUILTIN(StringPrototypeSearch,StringMatchSearchAssembler)1613 TF_BUILTIN(StringPrototypeSearch, StringMatchSearchAssembler) {
1614   auto receiver = Parameter<Object>(Descriptor::kReceiver);
1615   auto maybe_regexp = Parameter<Object>(Descriptor::kRegexp);
1616   auto context = Parameter<Context>(Descriptor::kContext);
1617   Generate(kSearch, "String.prototype.search", receiver, maybe_regexp, context);
1618 }
1619 
StringToArray(TNode<NativeContext> context,TNode<String> subject_string,TNode<Smi> subject_length,TNode<Number> limit_number)1620 TNode<JSArray> StringBuiltinsAssembler::StringToArray(
1621     TNode<NativeContext> context, TNode<String> subject_string,
1622     TNode<Smi> subject_length, TNode<Number> limit_number) {
1623   CSA_ASSERT(this, SmiGreaterThan(subject_length, SmiConstant(0)));
1624 
1625   Label done(this), call_runtime(this, Label::kDeferred),
1626       fill_thehole_and_call_runtime(this, Label::kDeferred);
1627   TVARIABLE(JSArray, result_array);
1628 
1629   TNode<Uint16T> instance_type = LoadInstanceType(subject_string);
1630   GotoIfNot(IsOneByteStringInstanceType(instance_type), &call_runtime);
1631 
1632   // Try to use cached one byte characters.
1633   {
1634     TNode<Smi> length_smi =
1635         Select<Smi>(TaggedIsSmi(limit_number),
1636                     [=] { return SmiMin(CAST(limit_number), subject_length); },
1637                     [=] { return subject_length; });
1638     TNode<IntPtrT> length = SmiToIntPtr(length_smi);
1639 
1640     ToDirectStringAssembler to_direct(state(), subject_string);
1641     to_direct.TryToDirect(&call_runtime);
1642 
1643     // The extracted direct string may be two-byte even though the wrapping
1644     // string is one-byte.
1645     GotoIfNot(IsOneByteStringInstanceType(to_direct.instance_type()),
1646               &call_runtime);
1647 
1648     TNode<FixedArray> elements = CAST(AllocateFixedArray(
1649         PACKED_ELEMENTS, length, AllocationFlag::kAllowLargeObjectAllocation));
1650     // Don't allocate anything while {string_data} is live!
1651     TNode<RawPtrT> string_data =
1652         to_direct.PointerToData(&fill_thehole_and_call_runtime);
1653     TNode<IntPtrT> string_data_offset = to_direct.offset();
1654     TNode<FixedArray> cache = SingleCharacterStringCacheConstant();
1655 
1656     BuildFastLoop<IntPtrT>(
1657         IntPtrConstant(0), length,
1658         [&](TNode<IntPtrT> index) {
1659           // TODO(jkummerow): Implement a CSA version of DisallowHeapAllocation
1660           // and use that to guard ToDirectStringAssembler.PointerToData().
1661           CSA_ASSERT(this, WordEqual(to_direct.PointerToData(&call_runtime),
1662                                      string_data));
1663           TNode<Int32T> char_code =
1664               UncheckedCast<Int32T>(Load(MachineType::Uint8(), string_data,
1665                                          IntPtrAdd(index, string_data_offset)));
1666           TNode<UintPtrT> code_index = ChangeUint32ToWord(char_code);
1667           TNode<Object> entry = LoadFixedArrayElement(cache, code_index);
1668 
1669           // If we cannot find a char in the cache, fill the hole for the fixed
1670           // array, and call runtime.
1671           GotoIf(IsUndefined(entry), &fill_thehole_and_call_runtime);
1672 
1673           StoreFixedArrayElement(elements, index, entry);
1674         },
1675         1, IndexAdvanceMode::kPost);
1676 
1677     TNode<Map> array_map = LoadJSArrayElementsMap(PACKED_ELEMENTS, context);
1678     result_array = AllocateJSArray(array_map, elements, length_smi);
1679     Goto(&done);
1680 
1681     BIND(&fill_thehole_and_call_runtime);
1682     {
1683       FillFixedArrayWithValue(PACKED_ELEMENTS, elements, IntPtrConstant(0),
1684                               length, RootIndex::kTheHoleValue);
1685       Goto(&call_runtime);
1686     }
1687   }
1688 
1689   BIND(&call_runtime);
1690   {
1691     result_array = CAST(CallRuntime(Runtime::kStringToArray, context,
1692                                     subject_string, limit_number));
1693     Goto(&done);
1694   }
1695 
1696   BIND(&done);
1697   return result_array.value();
1698 }
1699 
1700 // ES6 section 21.1.3.19 String.prototype.split ( separator, limit )
TF_BUILTIN(StringPrototypeSplit,StringBuiltinsAssembler)1701 TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
1702   const int kSeparatorArg = 0;
1703   const int kLimitArg = 1;
1704 
1705   const TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
1706       UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
1707   CodeStubArguments args(this, argc);
1708 
1709   TNode<Object> receiver = args.GetReceiver();
1710   const TNode<Object> separator = args.GetOptionalArgumentValue(kSeparatorArg);
1711   const TNode<Object> limit = args.GetOptionalArgumentValue(kLimitArg);
1712   auto context = Parameter<NativeContext>(Descriptor::kContext);
1713 
1714   TNode<Smi> smi_zero = SmiConstant(0);
1715 
1716   RequireObjectCoercible(context, receiver, "String.prototype.split");
1717 
1718   // Redirect to splitter method if {separator[@@split]} is not undefined.
1719 
1720   MaybeCallFunctionAtSymbol(
1721       context, separator, receiver, isolate()->factory()->split_symbol(),
1722       DescriptorIndexNameValue{JSRegExp::kSymbolSplitFunctionDescriptorIndex,
1723                                RootIndex::ksplit_symbol,
1724                                Context::REGEXP_SPLIT_FUNCTION_INDEX},
1725       [&]() {
1726         args.PopAndReturn(CallBuiltin(Builtins::kRegExpSplit, context,
1727                                       separator, receiver, limit));
1728       },
1729       [&](TNode<Object> fn) {
1730         args.PopAndReturn(Call(context, fn, separator, receiver, limit));
1731       });
1732 
1733   // String and integer conversions.
1734 
1735   TNode<String> subject_string = ToString_Inline(context, receiver);
1736   TNode<Number> limit_number = Select<Number>(
1737       IsUndefined(limit), [=] { return NumberConstant(kMaxUInt32); },
1738       [=] { return ToUint32(context, limit); });
1739   const TNode<String> separator_string = ToString_Inline(context, separator);
1740 
1741   Label return_empty_array(this);
1742 
1743   // Shortcut for {limit} == 0.
1744   GotoIf(TaggedEqual(limit_number, smi_zero), &return_empty_array);
1745 
1746   // ECMA-262 says that if {separator} is undefined, the result should
1747   // be an array of size 1 containing the entire string.
1748   {
1749     Label next(this);
1750     GotoIfNot(IsUndefined(separator), &next);
1751 
1752     const ElementsKind kind = PACKED_ELEMENTS;
1753     const TNode<NativeContext> native_context = LoadNativeContext(context);
1754     TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
1755 
1756     TNode<Smi> length = SmiConstant(1);
1757     TNode<IntPtrT> capacity = IntPtrConstant(1);
1758     TNode<JSArray> result = AllocateJSArray(kind, array_map, capacity, length);
1759 
1760     TNode<FixedArray> fixed_array = CAST(LoadElements(result));
1761     StoreFixedArrayElement(fixed_array, 0, subject_string);
1762 
1763     args.PopAndReturn(result);
1764 
1765     BIND(&next);
1766   }
1767 
1768   // If the separator string is empty then return the elements in the subject.
1769   {
1770     Label next(this);
1771     GotoIfNot(SmiEqual(LoadStringLengthAsSmi(separator_string), smi_zero),
1772               &next);
1773 
1774     TNode<Smi> subject_length = LoadStringLengthAsSmi(subject_string);
1775     GotoIf(SmiEqual(subject_length, smi_zero), &return_empty_array);
1776 
1777     args.PopAndReturn(
1778         StringToArray(context, subject_string, subject_length, limit_number));
1779 
1780     BIND(&next);
1781   }
1782 
1783   const TNode<Object> result =
1784       CallRuntime(Runtime::kStringSplit, context, subject_string,
1785                   separator_string, limit_number);
1786   args.PopAndReturn(result);
1787 
1788   BIND(&return_empty_array);
1789   {
1790     const ElementsKind kind = PACKED_ELEMENTS;
1791     const TNode<NativeContext> native_context = LoadNativeContext(context);
1792     TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
1793 
1794     TNode<Smi> length = smi_zero;
1795     TNode<IntPtrT> capacity = IntPtrConstant(0);
1796     TNode<JSArray> result = AllocateJSArray(kind, array_map, capacity, length);
1797 
1798     args.PopAndReturn(result);
1799   }
1800 }
1801 
TF_BUILTIN(StringSubstring,StringBuiltinsAssembler)1802 TF_BUILTIN(StringSubstring, StringBuiltinsAssembler) {
1803   auto string = Parameter<String>(Descriptor::kString);
1804   auto from = UncheckedParameter<IntPtrT>(Descriptor::kFrom);
1805   auto to = UncheckedParameter<IntPtrT>(Descriptor::kTo);
1806 
1807   Return(SubString(string, from, to));
1808 }
1809 
1810 
1811 // Return the |word32| codepoint at {index}. Supports SeqStrings and
1812 // ExternalStrings.
1813 // TODO(v8:9880): Use UintPtrT here.
LoadSurrogatePairAt(TNode<String> string,TNode<IntPtrT> length,TNode<IntPtrT> index,UnicodeEncoding encoding)1814 TNode<Int32T> StringBuiltinsAssembler::LoadSurrogatePairAt(
1815     TNode<String> string, TNode<IntPtrT> length, TNode<IntPtrT> index,
1816     UnicodeEncoding encoding) {
1817   Label handle_surrogate_pair(this), return_result(this);
1818   TVARIABLE(Int32T, var_result);
1819   TVARIABLE(Int32T, var_trail);
1820   var_result = StringCharCodeAt(string, Unsigned(index));
1821   var_trail = Int32Constant(0);
1822 
1823   GotoIf(Word32NotEqual(Word32And(var_result.value(), Int32Constant(0xFC00)),
1824                         Int32Constant(0xD800)),
1825          &return_result);
1826   TNode<IntPtrT> next_index = IntPtrAdd(index, IntPtrConstant(1));
1827 
1828   GotoIfNot(IntPtrLessThan(next_index, length), &return_result);
1829   var_trail = StringCharCodeAt(string, Unsigned(next_index));
1830   Branch(Word32Equal(Word32And(var_trail.value(), Int32Constant(0xFC00)),
1831                      Int32Constant(0xDC00)),
1832          &handle_surrogate_pair, &return_result);
1833 
1834   BIND(&handle_surrogate_pair);
1835   {
1836     TNode<Int32T> lead = var_result.value();
1837     TNode<Int32T> trail = var_trail.value();
1838 
1839     // Check that this path is only taken if a surrogate pair is found
1840     CSA_SLOW_ASSERT(this,
1841                     Uint32GreaterThanOrEqual(lead, Int32Constant(0xD800)));
1842     CSA_SLOW_ASSERT(this, Uint32LessThan(lead, Int32Constant(0xDC00)));
1843     CSA_SLOW_ASSERT(this,
1844                     Uint32GreaterThanOrEqual(trail, Int32Constant(0xDC00)));
1845     CSA_SLOW_ASSERT(this, Uint32LessThan(trail, Int32Constant(0xE000)));
1846 
1847     switch (encoding) {
1848       case UnicodeEncoding::UTF16:
1849         var_result = Word32Or(
1850 // Need to swap the order for big-endian platforms
1851 #if V8_TARGET_BIG_ENDIAN
1852             Word32Shl(lead, Int32Constant(16)), trail);
1853 #else
1854             Word32Shl(trail, Int32Constant(16)), lead);
1855 #endif
1856         break;
1857 
1858       case UnicodeEncoding::UTF32: {
1859         // Convert UTF16 surrogate pair into |word32| code point, encoded as
1860         // UTF32.
1861         TNode<Int32T> surrogate_offset =
1862             Int32Constant(0x10000 - (0xD800 << 10) - 0xDC00);
1863 
1864         // (lead << 10) + trail + SURROGATE_OFFSET
1865         var_result = Int32Add(Word32Shl(lead, Int32Constant(10)),
1866                               Int32Add(trail, surrogate_offset));
1867         break;
1868       }
1869     }
1870     Goto(&return_result);
1871   }
1872 
1873   BIND(&return_result);
1874   return var_result.value();
1875 }
1876 
BranchIfStringPrimitiveWithNoCustomIteration(TNode<Object> object,TNode<Context> context,Label * if_true,Label * if_false)1877 void StringBuiltinsAssembler::BranchIfStringPrimitiveWithNoCustomIteration(
1878     TNode<Object> object, TNode<Context> context, Label* if_true,
1879     Label* if_false) {
1880   GotoIf(TaggedIsSmi(object), if_false);
1881   GotoIfNot(IsString(CAST(object)), if_false);
1882 
1883   // Check that the String iterator hasn't been modified in a way that would
1884   // affect iteration.
1885   TNode<PropertyCell> protector_cell = StringIteratorProtectorConstant();
1886   DCHECK(isolate()->heap()->string_iterator_protector().IsPropertyCell());
1887   Branch(
1888       TaggedEqual(LoadObjectField(protector_cell, PropertyCell::kValueOffset),
1889                   SmiConstant(Protectors::kProtectorValid)),
1890       if_true, if_false);
1891 }
1892 
1893 // Instantiate template due to shared library requirements.
1894 template V8_EXPORT_PRIVATE void StringBuiltinsAssembler::CopyStringCharacters(
1895     TNode<String> from_string, TNode<String> to_string,
1896     TNode<IntPtrT> from_index, TNode<IntPtrT> to_index,
1897     TNode<IntPtrT> character_count, String::Encoding from_encoding,
1898     String::Encoding to_encoding);
1899 
1900 template V8_EXPORT_PRIVATE void StringBuiltinsAssembler::CopyStringCharacters(
1901     TNode<RawPtrT> from_string, TNode<String> to_string,
1902     TNode<IntPtrT> from_index, TNode<IntPtrT> to_index,
1903     TNode<IntPtrT> character_count, String::Encoding from_encoding,
1904     String::Encoding to_encoding);
1905 
1906 template <typename T>
CopyStringCharacters(TNode<T> from_string,TNode<String> to_string,TNode<IntPtrT> from_index,TNode<IntPtrT> to_index,TNode<IntPtrT> character_count,String::Encoding from_encoding,String::Encoding to_encoding)1907 void StringBuiltinsAssembler::CopyStringCharacters(
1908     TNode<T> from_string, TNode<String> to_string, TNode<IntPtrT> from_index,
1909     TNode<IntPtrT> to_index, TNode<IntPtrT> character_count,
1910     String::Encoding from_encoding, String::Encoding to_encoding) {
1911   // from_string could be either a String or a RawPtrT in the case we pass in
1912   // faked sequential strings when handling external subject strings.
1913   bool from_one_byte = from_encoding == String::ONE_BYTE_ENCODING;
1914   bool to_one_byte = to_encoding == String::ONE_BYTE_ENCODING;
1915   DCHECK_IMPLIES(to_one_byte, from_one_byte);
1916   Comment("CopyStringCharacters ",
1917           from_one_byte ? "ONE_BYTE_ENCODING" : "TWO_BYTE_ENCODING", " -> ",
1918           to_one_byte ? "ONE_BYTE_ENCODING" : "TWO_BYTE_ENCODING");
1919 
1920   ElementsKind from_kind = from_one_byte ? UINT8_ELEMENTS : UINT16_ELEMENTS;
1921   ElementsKind to_kind = to_one_byte ? UINT8_ELEMENTS : UINT16_ELEMENTS;
1922   STATIC_ASSERT(SeqOneByteString::kHeaderSize == SeqTwoByteString::kHeaderSize);
1923   int header_size = SeqOneByteString::kHeaderSize - kHeapObjectTag;
1924   TNode<IntPtrT> from_offset =
1925       ElementOffsetFromIndex(from_index, from_kind, header_size);
1926   TNode<IntPtrT> to_offset =
1927       ElementOffsetFromIndex(to_index, to_kind, header_size);
1928   TNode<IntPtrT> byte_count =
1929       ElementOffsetFromIndex(character_count, from_kind);
1930   TNode<IntPtrT> limit_offset = IntPtrAdd(from_offset, byte_count);
1931 
1932   // Prepare the fast loop
1933   MachineType type =
1934       from_one_byte ? MachineType::Uint8() : MachineType::Uint16();
1935   MachineRepresentation rep = to_one_byte ? MachineRepresentation::kWord8
1936                                           : MachineRepresentation::kWord16;
1937   int from_increment = 1 << ElementsKindToShiftSize(from_kind);
1938   int to_increment = 1 << ElementsKindToShiftSize(to_kind);
1939 
1940   TVARIABLE(IntPtrT, current_to_offset, to_offset);
1941   VariableList vars({&current_to_offset}, zone());
1942   int to_index_constant = 0, from_index_constant = 0;
1943   bool index_same = (from_encoding == to_encoding) &&
1944                     (from_index == to_index ||
1945                      (ToInt32Constant(from_index, &from_index_constant) &&
1946                       ToInt32Constant(to_index, &to_index_constant) &&
1947                       from_index_constant == to_index_constant));
1948   BuildFastLoop<IntPtrT>(
1949       vars, from_offset, limit_offset,
1950       [&](TNode<IntPtrT> offset) {
1951         StoreNoWriteBarrier(rep, to_string,
1952                             index_same ? offset : current_to_offset.value(),
1953                             Load(type, from_string, offset));
1954         if (!index_same) {
1955           Increment(&current_to_offset, to_increment);
1956         }
1957       },
1958       from_increment, IndexAdvanceMode::kPost);
1959 }
1960 
1961 // A wrapper around CopyStringCharacters which determines the correct string
1962 // encoding, allocates a corresponding sequential string, and then copies the
1963 // given character range using CopyStringCharacters.
1964 // |from_string| must be a sequential string.
1965 // 0 <= |from_index| <= |from_index| + |character_count| < from_string.length.
1966 template <typename T>
AllocAndCopyStringCharacters(TNode<T> from,TNode<Int32T> from_instance_type,TNode<IntPtrT> from_index,TNode<IntPtrT> character_count)1967 TNode<String> StringBuiltinsAssembler::AllocAndCopyStringCharacters(
1968     TNode<T> from, TNode<Int32T> from_instance_type, TNode<IntPtrT> from_index,
1969     TNode<IntPtrT> character_count) {
1970   Label end(this), one_byte_sequential(this), two_byte_sequential(this);
1971   TVARIABLE(String, var_result);
1972 
1973   Branch(IsOneByteStringInstanceType(from_instance_type), &one_byte_sequential,
1974          &two_byte_sequential);
1975 
1976   // The subject string is a sequential one-byte string.
1977   BIND(&one_byte_sequential);
1978   {
1979     TNode<String> result = AllocateSeqOneByteString(
1980         Unsigned(TruncateIntPtrToInt32(character_count)));
1981     CopyStringCharacters<T>(from, result, from_index, IntPtrConstant(0),
1982                             character_count, String::ONE_BYTE_ENCODING,
1983                             String::ONE_BYTE_ENCODING);
1984     var_result = result;
1985     Goto(&end);
1986   }
1987 
1988   // The subject string is a sequential two-byte string.
1989   BIND(&two_byte_sequential);
1990   {
1991     TNode<String> result = AllocateSeqTwoByteString(
1992         Unsigned(TruncateIntPtrToInt32(character_count)));
1993     CopyStringCharacters<T>(from, result, from_index, IntPtrConstant(0),
1994                             character_count, String::TWO_BYTE_ENCODING,
1995                             String::TWO_BYTE_ENCODING);
1996     var_result = result;
1997     Goto(&end);
1998   }
1999 
2000   BIND(&end);
2001   return var_result.value();
2002 }
2003 
2004 // TODO(v8:9880): Use UintPtrT here.
SubString(TNode<String> string,TNode<IntPtrT> from,TNode<IntPtrT> to)2005 TNode<String> StringBuiltinsAssembler::SubString(TNode<String> string,
2006                                                  TNode<IntPtrT> from,
2007                                                  TNode<IntPtrT> to) {
2008   TVARIABLE(String, var_result);
2009   ToDirectStringAssembler to_direct(state(), string);
2010   Label end(this), runtime(this);
2011 
2012   const TNode<IntPtrT> substr_length = IntPtrSub(to, from);
2013   const TNode<IntPtrT> string_length = LoadStringLengthAsWord(string);
2014 
2015   // Begin dispatching based on substring length.
2016 
2017   Label original_string_or_invalid_length(this);
2018   GotoIf(UintPtrGreaterThanOrEqual(substr_length, string_length),
2019          &original_string_or_invalid_length);
2020 
2021   // A real substring (substr_length < string_length).
2022   Label empty(this);
2023   GotoIf(IntPtrEqual(substr_length, IntPtrConstant(0)), &empty);
2024 
2025   Label single_char(this);
2026   GotoIf(IntPtrEqual(substr_length, IntPtrConstant(1)), &single_char);
2027 
2028   // Deal with different string types: update the index if necessary
2029   // and extract the underlying string.
2030 
2031   TNode<String> direct_string = to_direct.TryToDirect(&runtime);
2032   TNode<IntPtrT> offset = IntPtrAdd(from, to_direct.offset());
2033   const TNode<Int32T> instance_type = to_direct.instance_type();
2034 
2035   // The subject string can only be external or sequential string of either
2036   // encoding at this point.
2037   Label external_string(this);
2038   {
2039     if (FLAG_string_slices) {
2040       Label next(this);
2041 
2042       // Short slice.  Copy instead of slicing.
2043       GotoIf(IntPtrLessThan(substr_length,
2044                             IntPtrConstant(SlicedString::kMinLength)),
2045              &next);
2046 
2047       // Allocate new sliced string.
2048 
2049       Counters* counters = isolate()->counters();
2050       IncrementCounter(counters->sub_string_native(), 1);
2051 
2052       Label one_byte_slice(this), two_byte_slice(this);
2053       Branch(IsOneByteStringInstanceType(to_direct.instance_type()),
2054              &one_byte_slice, &two_byte_slice);
2055 
2056       BIND(&one_byte_slice);
2057       {
2058         var_result = AllocateSlicedOneByteString(
2059             Unsigned(TruncateIntPtrToInt32(substr_length)), direct_string,
2060             SmiTag(offset));
2061         Goto(&end);
2062       }
2063 
2064       BIND(&two_byte_slice);
2065       {
2066         var_result = AllocateSlicedTwoByteString(
2067             Unsigned(TruncateIntPtrToInt32(substr_length)), direct_string,
2068             SmiTag(offset));
2069         Goto(&end);
2070       }
2071 
2072       BIND(&next);
2073     }
2074 
2075     // The subject string can only be external or sequential string of either
2076     // encoding at this point.
2077     GotoIf(to_direct.is_external(), &external_string);
2078 
2079     var_result = AllocAndCopyStringCharacters(direct_string, instance_type,
2080                                               offset, substr_length);
2081 
2082     Counters* counters = isolate()->counters();
2083     IncrementCounter(counters->sub_string_native(), 1);
2084 
2085     Goto(&end);
2086   }
2087 
2088   // Handle external string.
2089   BIND(&external_string);
2090   {
2091     const TNode<RawPtrT> fake_sequential_string =
2092         to_direct.PointerToString(&runtime);
2093 
2094     var_result = AllocAndCopyStringCharacters(
2095         fake_sequential_string, instance_type, offset, substr_length);
2096 
2097     Counters* counters = isolate()->counters();
2098     IncrementCounter(counters->sub_string_native(), 1);
2099 
2100     Goto(&end);
2101   }
2102 
2103   BIND(&empty);
2104   {
2105     var_result = EmptyStringConstant();
2106     Goto(&end);
2107   }
2108 
2109   // Substrings of length 1 are generated through CharCodeAt and FromCharCode.
2110   BIND(&single_char);
2111   {
2112     TNode<Int32T> char_code = StringCharCodeAt(string, Unsigned(from));
2113     var_result = StringFromSingleCharCode(char_code);
2114     Goto(&end);
2115   }
2116 
2117   BIND(&original_string_or_invalid_length);
2118   {
2119     CSA_ASSERT(this, IntPtrEqual(substr_length, string_length));
2120 
2121     // Equal length - check if {from, to} == {0, str.length}.
2122     GotoIf(UintPtrGreaterThan(from, IntPtrConstant(0)), &runtime);
2123 
2124     // Return the original string (substr_length == string_length).
2125 
2126     Counters* counters = isolate()->counters();
2127     IncrementCounter(counters->sub_string_native(), 1);
2128 
2129     var_result = string;
2130     Goto(&end);
2131   }
2132 
2133   // Fall back to a runtime call.
2134   BIND(&runtime);
2135   {
2136     var_result =
2137         CAST(CallRuntime(Runtime::kStringSubstring, NoContextConstant(), string,
2138                          SmiTag(from), SmiTag(to)));
2139     Goto(&end);
2140   }
2141 
2142   BIND(&end);
2143   return var_result.value();
2144 }
2145 
2146 }  // namespace internal
2147 }  // namespace v8
2148