LCOV - code coverage report
Current view: top level - src/builtins - builtins-string-gen.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 1099 1103 99.6 %
Date: 2019-04-17 Functions: 126 126 100.0 %

          Line data    Source code
       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/code-factory.h"
      11             : #include "src/heap/factory-inl.h"
      12             : #include "src/heap/heap-inl.h"
      13             : #include "src/objects.h"
      14             : #include "src/objects/property-cell.h"
      15             : 
      16             : namespace v8 {
      17             : namespace internal {
      18             : 
      19             : typedef compiler::Node Node;
      20             : template <class T>
      21             : using TNode = compiler::TNode<T>;
      22             : 
      23        1344 : Node* StringBuiltinsAssembler::DirectStringData(Node* string,
      24             :                                                 Node* string_instance_type) {
      25             :   // Compute the effective offset of the first character.
      26        2688 :   VARIABLE(var_data, MachineType::PointerRepresentation());
      27        1344 :   Label if_sequential(this), if_external(this), if_join(this);
      28        5376 :   Branch(Word32Equal(Word32And(string_instance_type,
      29        2688 :                                Int32Constant(kStringRepresentationMask)),
      30        4032 :                      Int32Constant(kSeqStringTag)),
      31        1344 :          &if_sequential, &if_external);
      32             : 
      33        1344 :   BIND(&if_sequential);
      34             :   {
      35             :     var_data.Bind(IntPtrAdd(
      36             :         IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag),
      37        2688 :         BitcastTaggedToWord(string)));
      38        1344 :     Goto(&if_join);
      39             :   }
      40             : 
      41        1344 :   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        2688 :     var_data.Bind(LoadObjectField(string, ExternalString::kResourceDataOffset,
      50        1344 :                                   MachineType::Pointer()));
      51        1344 :     Goto(&if_join);
      52             :   }
      53             : 
      54        1344 :   BIND(&if_join);
      55        2688 :   return var_data.value();
      56             : }
      57             : 
      58         168 : void StringBuiltinsAssembler::DispatchOnStringEncodings(
      59             :     Node* const lhs_instance_type, Node* const rhs_instance_type,
      60             :     Label* if_one_one, Label* if_one_two, Label* if_two_one,
      61             :     Label* if_two_two) {
      62             :   STATIC_ASSERT(kStringEncodingMask == 0x8);
      63             :   STATIC_ASSERT(kTwoByteStringTag == 0x0);
      64             :   STATIC_ASSERT(kOneByteStringTag == 0x8);
      65             : 
      66             :   // First combine the encodings.
      67             : 
      68         336 :   Node* const encoding_mask = Int32Constant(kStringEncodingMask);
      69         336 :   Node* const lhs_encoding = Word32And(lhs_instance_type, encoding_mask);
      70         336 :   Node* const rhs_encoding = Word32And(rhs_instance_type, encoding_mask);
      71             : 
      72             :   Node* const combined_encodings =
      73         504 :       Word32Or(lhs_encoding, Word32Shr(rhs_encoding, 1));
      74             : 
      75             :   // Then dispatch on the combined encoding.
      76             : 
      77         168 :   Label unreachable(this, Label::kDeferred);
      78             : 
      79             :   int32_t values[] = {
      80             :       kOneByteStringTag | (kOneByteStringTag >> 1),
      81             :       kOneByteStringTag | (kTwoByteStringTag >> 1),
      82             :       kTwoByteStringTag | (kOneByteStringTag >> 1),
      83             :       kTwoByteStringTag | (kTwoByteStringTag >> 1),
      84         168 :   };
      85             :   Label* labels[] = {
      86             :       if_one_one, if_one_two, if_two_one, if_two_two,
      87         168 :   };
      88             : 
      89             :   STATIC_ASSERT(arraysize(values) == arraysize(labels));
      90         168 :   Switch(combined_encodings, &unreachable, values, labels, arraysize(values));
      91             : 
      92         168 :   BIND(&unreachable);
      93         168 :   Unreachable();
      94         168 : }
      95             : 
      96             : template <typename SubjectChar, typename PatternChar>
      97         672 : Node* StringBuiltinsAssembler::CallSearchStringRaw(Node* const subject_ptr,
      98             :                                                    Node* const subject_length,
      99             :                                                    Node* const search_ptr,
     100             :                                                    Node* const search_length,
     101             :                                                    Node* const start_position) {
     102             :   Node* const function_addr = ExternalConstant(
     103        1344 :       ExternalReference::search_string_raw<SubjectChar, PatternChar>());
     104             :   Node* const isolate_ptr =
     105        1344 :       ExternalConstant(ExternalReference::isolate_address(isolate()));
     106             : 
     107             :   MachineType type_ptr = MachineType::Pointer();
     108             :   MachineType type_intptr = MachineType::IntPtr();
     109             : 
     110             :   Node* const result = 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         672 :       std::make_pair(type_intptr, start_position));
     117             : 
     118         672 :   return result;
     119             : }
     120             : 
     121        1344 : Node* StringBuiltinsAssembler::PointerToStringDataAtIndex(
     122             :     Node* const string_data, Node* const index, String::Encoding encoding) {
     123             :   const ElementsKind kind = (encoding == String::ONE_BYTE_ENCODING)
     124             :                                 ? UINT8_ELEMENTS
     125        1344 :                                 : UINT16_ELEMENTS;
     126             :   Node* const offset_in_bytes =
     127        2688 :       ElementOffsetFromIndex(index, kind, INTPTR_PARAMETERS);
     128        2688 :   return IntPtrAdd(string_data, offset_in_bytes);
     129             : }
     130             : 
     131          56 : void StringBuiltinsAssembler::GenerateStringEqual(Node* context, Node* left,
     132             :                                                   Node* right) {
     133         112 :   VARIABLE(var_left, MachineRepresentation::kTagged, left);
     134         112 :   VARIABLE(var_right, MachineRepresentation::kTagged, right);
     135          56 :   Label if_equal(this), if_notequal(this), if_indirect(this, Label::kDeferred),
     136         168 :       restart(this, {&var_left, &var_right});
     137             : 
     138          56 :   TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(left);
     139          56 :   TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(right);
     140             : 
     141             :   // Strings with different lengths cannot be equal.
     142         112 :   GotoIf(WordNotEqual(lhs_length, rhs_length), &if_notequal);
     143             : 
     144          56 :   Goto(&restart);
     145          56 :   BIND(&restart);
     146          56 :   Node* lhs = var_left.value();
     147          56 :   Node* rhs = var_right.value();
     148             : 
     149         112 :   Node* lhs_instance_type = LoadInstanceType(lhs);
     150         112 :   Node* rhs_instance_type = LoadInstanceType(rhs);
     151             : 
     152             :   StringEqual_Core(context, lhs, lhs_instance_type, rhs, rhs_instance_type,
     153          56 :                    lhs_length, &if_equal, &if_notequal, &if_indirect);
     154             : 
     155          56 :   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          56 :                               rhs_instance_type, &restart);
     160             : 
     161          56 :     TailCallRuntime(Runtime::kStringEqual, context, lhs, rhs);
     162             :   }
     163             : 
     164          56 :   BIND(&if_equal);
     165         112 :   Return(TrueConstant());
     166             : 
     167          56 :   BIND(&if_notequal);
     168         112 :   Return(FalseConstant());
     169          56 : }
     170             : 
     171         168 : void StringBuiltinsAssembler::StringEqual_Core(
     172             :     Node* context, Node* lhs, Node* lhs_instance_type, Node* rhs,
     173             :     Node* rhs_instance_type, TNode<IntPtrT> length, Label* if_equal,
     174             :     Label* if_not_equal, Label* if_indirect) {
     175             :   CSA_ASSERT(this, IsString(lhs));
     176             :   CSA_ASSERT(this, IsString(rhs));
     177             :   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(lhs), length));
     178             :   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(rhs), length));
     179             :   // Fast check to see if {lhs} and {rhs} refer to the same String object.
     180         336 :   GotoIf(WordEqual(lhs, rhs), if_equal);
     181             : 
     182             :   // Combine the instance types into a single 16-bit value, so we can check
     183             :   // both of them at once.
     184         336 :   Node* both_instance_types = Word32Or(
     185         504 :       lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
     186             : 
     187             :   // Check if both {lhs} and {rhs} are internalized. Since we already know
     188             :   // that they're not the same object, they're not equal in that case.
     189             :   int const kBothInternalizedMask =
     190             :       kIsNotInternalizedMask | (kIsNotInternalizedMask << 8);
     191             :   int const kBothInternalizedTag = kInternalizedTag | (kInternalizedTag << 8);
     192         672 :   GotoIf(Word32Equal(Word32And(both_instance_types,
     193         336 :                                Int32Constant(kBothInternalizedMask)),
     194         504 :                      Int32Constant(kBothInternalizedTag)),
     195         168 :          if_not_equal);
     196             : 
     197             :   // Check if both {lhs} and {rhs} are direct strings, and that in case of
     198             :   // ExternalStrings the data pointer is cached.
     199             :   STATIC_ASSERT(kUncachedExternalStringTag != 0);
     200             :   STATIC_ASSERT(kIsIndirectStringTag != 0);
     201             :   int const kBothDirectStringMask =
     202             :       kIsIndirectStringMask | kUncachedExternalStringMask |
     203             :       ((kIsIndirectStringMask | kUncachedExternalStringMask) << 8);
     204         672 :   GotoIfNot(Word32Equal(Word32And(both_instance_types,
     205         336 :                                   Int32Constant(kBothDirectStringMask)),
     206         504 :                         Int32Constant(0)),
     207         168 :             if_indirect);
     208             : 
     209             :   // Dispatch based on the {lhs} and {rhs} string encoding.
     210             :   int const kBothStringEncodingMask =
     211             :       kStringEncodingMask | (kStringEncodingMask << 8);
     212             :   int const kOneOneByteStringTag = kOneByteStringTag | (kOneByteStringTag << 8);
     213             :   int const kTwoTwoByteStringTag = kTwoByteStringTag | (kTwoByteStringTag << 8);
     214             :   int const kOneTwoByteStringTag = kOneByteStringTag | (kTwoByteStringTag << 8);
     215         168 :   Label if_oneonebytestring(this), if_twotwobytestring(this),
     216         168 :       if_onetwobytestring(this), if_twoonebytestring(this);
     217             :   Node* masked_instance_types =
     218         504 :       Word32And(both_instance_types, Int32Constant(kBothStringEncodingMask));
     219         168 :   GotoIf(
     220         504 :       Word32Equal(masked_instance_types, Int32Constant(kOneOneByteStringTag)),
     221         168 :       &if_oneonebytestring);
     222         168 :   GotoIf(
     223         504 :       Word32Equal(masked_instance_types, Int32Constant(kTwoTwoByteStringTag)),
     224         168 :       &if_twotwobytestring);
     225         168 :   Branch(
     226         504 :       Word32Equal(masked_instance_types, Int32Constant(kOneTwoByteStringTag)),
     227         168 :       &if_onetwobytestring, &if_twoonebytestring);
     228             : 
     229         168 :   BIND(&if_oneonebytestring);
     230             :   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
     231             :                    rhs_instance_type, MachineType::Uint8(), length, if_equal,
     232         168 :                    if_not_equal);
     233             : 
     234         168 :   BIND(&if_twotwobytestring);
     235             :   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
     236             :                    rhs_instance_type, MachineType::Uint16(), length, if_equal,
     237         168 :                    if_not_equal);
     238             : 
     239         168 :   BIND(&if_onetwobytestring);
     240             :   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
     241             :                    rhs_instance_type, MachineType::Uint16(), length, if_equal,
     242         168 :                    if_not_equal);
     243             : 
     244         168 :   BIND(&if_twoonebytestring);
     245             :   StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
     246             :                    rhs_instance_type, MachineType::Uint8(), length, if_equal,
     247         168 :                    if_not_equal);
     248         168 : }
     249             : 
     250         672 : void StringBuiltinsAssembler::StringEqual_Loop(
     251             :     Node* lhs, Node* lhs_instance_type, MachineType lhs_type, Node* rhs,
     252             :     Node* rhs_instance_type, MachineType rhs_type, TNode<IntPtrT> length,
     253             :     Label* if_equal, Label* if_not_equal) {
     254             :   CSA_ASSERT(this, IsString(lhs));
     255             :   CSA_ASSERT(this, IsString(rhs));
     256             :   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(lhs), length));
     257             :   CSA_ASSERT(this, WordEqual(LoadStringLengthAsWord(rhs), length));
     258             : 
     259             :   // Compute the effective offset of the first character.
     260         672 :   Node* lhs_data = DirectStringData(lhs, lhs_instance_type);
     261         672 :   Node* rhs_data = DirectStringData(rhs, rhs_instance_type);
     262             : 
     263             :   // Loop over the {lhs} and {rhs} strings to see if they are equal.
     264         672 :   TVARIABLE(IntPtrT, var_offset, IntPtrConstant(0));
     265         672 :   Label loop(this, &var_offset);
     266         672 :   Goto(&loop);
     267         672 :   BIND(&loop);
     268             :   {
     269             :     // If {offset} equals {end}, no difference was found, so the
     270             :     // strings are equal.
     271        1344 :     GotoIf(WordEqual(var_offset.value(), length), if_equal);
     272             : 
     273             :     // Load the next characters from {lhs} and {rhs}.
     274             :     Node* lhs_value =
     275             :         Load(lhs_type, lhs_data,
     276        1344 :              WordShl(var_offset.value(),
     277        2016 :                      ElementSizeLog2Of(lhs_type.representation())));
     278             :     Node* rhs_value =
     279             :         Load(rhs_type, rhs_data,
     280        1344 :              WordShl(var_offset.value(),
     281        2016 :                      ElementSizeLog2Of(rhs_type.representation())));
     282             : 
     283             :     // Check if the characters match.
     284        1344 :     GotoIf(Word32NotEqual(lhs_value, rhs_value), if_not_equal);
     285             : 
     286             :     // Advance to next character.
     287         672 :     var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
     288         672 :     Goto(&loop);
     289             :   }
     290         672 : }
     291             : 
     292         336 : TF_BUILTIN(StringAdd_CheckNone, StringBuiltinsAssembler) {
     293          56 :   TNode<String> left = CAST(Parameter(Descriptor::kLeft));
     294          56 :   TNode<String> right = CAST(Parameter(Descriptor::kRight));
     295             :   Node* context = Parameter(Descriptor::kContext);
     296         112 :   Return(StringAdd(context, left, right));
     297          56 : }
     298             : 
     299         280 : TF_BUILTIN(StringAdd_ConvertLeft, StringBuiltinsAssembler) {
     300             :   TNode<Object> left = CAST(Parameter(Descriptor::kLeft));
     301          56 :   TNode<String> right = CAST(Parameter(Descriptor::kRight));
     302             :   Node* context = Parameter(Descriptor::kContext);
     303             :   // TODO(danno): The ToString and JSReceiverToPrimitive below could be
     304             :   // combined to avoid duplicate smi and instance type checks.
     305         168 :   left = ToString(context, JSReceiverToPrimitive(context, left));
     306          56 :   TailCallBuiltin(Builtins::kStringAdd_CheckNone, context, left, right);
     307          56 : }
     308             : 
     309         280 : TF_BUILTIN(StringAdd_ConvertRight, StringBuiltinsAssembler) {
     310          56 :   TNode<String> left = CAST(Parameter(Descriptor::kLeft));
     311             :   TNode<Object> right = CAST(Parameter(Descriptor::kRight));
     312             :   Node* context = Parameter(Descriptor::kContext);
     313             :   // TODO(danno): The ToString and JSReceiverToPrimitive below could be
     314             :   // combined to avoid duplicate smi and instance type checks.
     315         168 :   right = ToString(context, JSReceiverToPrimitive(context, right));
     316          56 :   TailCallBuiltin(Builtins::kStringAdd_CheckNone, context, left, right);
     317          56 : }
     318             : 
     319         280 : TF_BUILTIN(SubString, StringBuiltinsAssembler) {
     320          56 :   TNode<String> string = CAST(Parameter(Descriptor::kString));
     321             :   TNode<Smi> from = CAST(Parameter(Descriptor::kFrom));
     322             :   TNode<Smi> to = CAST(Parameter(Descriptor::kTo));
     323         168 :   Return(SubString(string, SmiUntag(from), SmiUntag(to)));
     324          56 : }
     325             : 
     326         168 : void StringBuiltinsAssembler::GenerateStringAt(
     327             :     char const* method_name, TNode<Context> context, TNode<Object> receiver,
     328             :     TNode<Object> maybe_position, TNode<Object> default_return,
     329             :     const StringAtAccessor& accessor) {
     330             :   // Check that {receiver} is coercible to Object and convert it to a String.
     331         168 :   TNode<String> string = ToThisString(context, receiver, method_name);
     332             : 
     333             :   // Convert the {position} to a Smi and check that it's in bounds of the
     334             :   // {string}.
     335         336 :   Label if_outofbounds(this, Label::kDeferred);
     336             :   TNode<Number> position = ToInteger_Inline(
     337         168 :       context, maybe_position, CodeStubAssembler::kTruncateMinusZero);
     338         336 :   GotoIfNot(TaggedIsSmi(position), &if_outofbounds);
     339         168 :   TNode<IntPtrT> index = SmiUntag(CAST(position));
     340         168 :   TNode<IntPtrT> length = LoadStringLengthAsWord(string);
     341         336 :   GotoIfNot(UintPtrLessThan(index, length), &if_outofbounds);
     342             :   TNode<Object> result = accessor(string, length, index);
     343         168 :   Return(result);
     344             : 
     345         168 :   BIND(&if_outofbounds);
     346         168 :   Return(default_return);
     347         168 : }
     348             : 
     349         224 : void StringBuiltinsAssembler::GenerateStringRelationalComparison(Node* context,
     350             :                                                                  Node* left,
     351             :                                                                  Node* right,
     352             :                                                                  Operation op) {
     353         448 :   VARIABLE(var_left, MachineRepresentation::kTagged, left);
     354         448 :   VARIABLE(var_right, MachineRepresentation::kTagged, right);
     355             : 
     356         224 :   Variable* input_vars[2] = {&var_left, &var_right};
     357         224 :   Label if_less(this), if_equal(this), if_greater(this);
     358         448 :   Label restart(this, 2, input_vars);
     359         224 :   Goto(&restart);
     360         224 :   BIND(&restart);
     361             : 
     362         224 :   Node* lhs = var_left.value();
     363         224 :   Node* rhs = var_right.value();
     364             :   // Fast check to see if {lhs} and {rhs} refer to the same String object.
     365         448 :   GotoIf(WordEqual(lhs, rhs), &if_equal);
     366             : 
     367             :   // Load instance types of {lhs} and {rhs}.
     368         448 :   Node* lhs_instance_type = LoadInstanceType(lhs);
     369         448 :   Node* rhs_instance_type = LoadInstanceType(rhs);
     370             : 
     371             :   // Combine the instance types into a single 16-bit value, so we can check
     372             :   // both of them at once.
     373         448 :   Node* both_instance_types = Word32Or(
     374         672 :       lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
     375             : 
     376             :   // Check that both {lhs} and {rhs} are flat one-byte strings.
     377             :   int const kBothSeqOneByteStringMask =
     378             :       kStringEncodingMask | kStringRepresentationMask |
     379             :       ((kStringEncodingMask | kStringRepresentationMask) << 8);
     380             :   int const kBothSeqOneByteStringTag =
     381             :       kOneByteStringTag | kSeqStringTag |
     382             :       ((kOneByteStringTag | kSeqStringTag) << 8);
     383         224 :   Label if_bothonebyteseqstrings(this), if_notbothonebyteseqstrings(this);
     384         896 :   Branch(Word32Equal(Word32And(both_instance_types,
     385         448 :                                Int32Constant(kBothSeqOneByteStringMask)),
     386         672 :                      Int32Constant(kBothSeqOneByteStringTag)),
     387         224 :          &if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
     388             : 
     389         224 :   BIND(&if_bothonebyteseqstrings);
     390             :   {
     391             :     // Load the length of {lhs} and {rhs}.
     392         224 :     TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(lhs);
     393         224 :     TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(rhs);
     394             : 
     395             :     // Determine the minimum length.
     396         224 :     TNode<IntPtrT> length = IntPtrMin(lhs_length, rhs_length);
     397             : 
     398             :     // Compute the effective offset of the first character.
     399             :     TNode<IntPtrT> begin =
     400         224 :         IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag);
     401             : 
     402             :     // Compute the first offset after the string from the length.
     403             :     TNode<IntPtrT> end = IntPtrAdd(begin, length);
     404             : 
     405             :     // Loop over the {lhs} and {rhs} strings to see if they are equal.
     406             :     TVARIABLE(IntPtrT, var_offset, begin);
     407         224 :     Label loop(this, &var_offset);
     408         224 :     Goto(&loop);
     409         224 :     BIND(&loop);
     410             :     {
     411             :       // Check if {offset} equals {end}.
     412         224 :       Label if_done(this), if_notdone(this);
     413         448 :       Branch(WordEqual(var_offset.value(), end), &if_done, &if_notdone);
     414             : 
     415         224 :       BIND(&if_notdone);
     416             :       {
     417             :         // Load the next characters from {lhs} and {rhs}.
     418         224 :         Node* lhs_value = Load(MachineType::Uint8(), lhs, var_offset.value());
     419         224 :         Node* rhs_value = Load(MachineType::Uint8(), rhs, var_offset.value());
     420             : 
     421             :         // Check if the characters match.
     422         224 :         Label if_valueissame(this), if_valueisnotsame(this);
     423         448 :         Branch(Word32Equal(lhs_value, rhs_value), &if_valueissame,
     424         224 :                &if_valueisnotsame);
     425             : 
     426         224 :         BIND(&if_valueissame);
     427             :         {
     428             :           // Advance to next character.
     429         224 :           var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
     430             :         }
     431         224 :         Goto(&loop);
     432             : 
     433         224 :         BIND(&if_valueisnotsame);
     434         448 :         Branch(Uint32LessThan(lhs_value, rhs_value), &if_less, &if_greater);
     435             :       }
     436             : 
     437         224 :       BIND(&if_done);
     438             :       {
     439             :         // All characters up to the min length are equal, decide based on
     440             :         // string length.
     441         448 :         GotoIf(IntPtrEqual(lhs_length, rhs_length), &if_equal);
     442         448 :         Branch(IntPtrLessThan(lhs_length, rhs_length), &if_less, &if_greater);
     443             :       }
     444             :     }
     445             :   }
     446             : 
     447         224 :   BIND(&if_notbothonebyteseqstrings);
     448             :   {
     449             :     // Try to unwrap indirect strings, restart the above attempt on success.
     450             :     MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
     451         224 :                               rhs_instance_type, &restart);
     452             :     // TODO(bmeurer): Add support for two byte string relational comparisons.
     453         224 :     switch (op) {
     454             :       case Operation::kLessThan:
     455          56 :         TailCallRuntime(Runtime::kStringLessThan, context, lhs, rhs);
     456          56 :         break;
     457             :       case Operation::kLessThanOrEqual:
     458          56 :         TailCallRuntime(Runtime::kStringLessThanOrEqual, context, lhs, rhs);
     459          56 :         break;
     460             :       case Operation::kGreaterThan:
     461          56 :         TailCallRuntime(Runtime::kStringGreaterThan, context, lhs, rhs);
     462          56 :         break;
     463             :       case Operation::kGreaterThanOrEqual:
     464          56 :         TailCallRuntime(Runtime::kStringGreaterThanOrEqual, context, lhs, rhs);
     465          56 :         break;
     466             :       default:
     467           0 :         UNREACHABLE();
     468             :     }
     469             :   }
     470             : 
     471         224 :   BIND(&if_less);
     472         224 :   switch (op) {
     473             :     case Operation::kLessThan:
     474             :     case Operation::kLessThanOrEqual:
     475         224 :       Return(TrueConstant());
     476         112 :       break;
     477             : 
     478             :     case Operation::kGreaterThan:
     479             :     case Operation::kGreaterThanOrEqual:
     480         224 :       Return(FalseConstant());
     481         112 :       break;
     482             :     default:
     483           0 :       UNREACHABLE();
     484             :   }
     485             : 
     486         224 :   BIND(&if_equal);
     487         224 :   switch (op) {
     488             :     case Operation::kLessThan:
     489             :     case Operation::kGreaterThan:
     490         224 :       Return(FalseConstant());
     491         112 :       break;
     492             : 
     493             :     case Operation::kLessThanOrEqual:
     494             :     case Operation::kGreaterThanOrEqual:
     495         224 :       Return(TrueConstant());
     496         112 :       break;
     497             :     default:
     498           0 :       UNREACHABLE();
     499             :   }
     500             : 
     501         224 :   BIND(&if_greater);
     502         224 :   switch (op) {
     503             :     case Operation::kLessThan:
     504             :     case Operation::kLessThanOrEqual:
     505         224 :       Return(FalseConstant());
     506         112 :       break;
     507             : 
     508             :     case Operation::kGreaterThan:
     509             :     case Operation::kGreaterThanOrEqual:
     510         224 :       Return(TrueConstant());
     511         112 :       break;
     512             :     default:
     513           0 :       UNREACHABLE();
     514             :   }
     515         224 : }
     516             : 
     517         224 : TF_BUILTIN(StringEqual, StringBuiltinsAssembler) {
     518             :   Node* context = Parameter(Descriptor::kContext);
     519             :   Node* left = Parameter(Descriptor::kLeft);
     520             :   Node* right = Parameter(Descriptor::kRight);
     521          56 :   GenerateStringEqual(context, left, right);
     522          56 : }
     523             : 
     524         224 : TF_BUILTIN(StringLessThan, StringBuiltinsAssembler) {
     525             :   Node* context = Parameter(Descriptor::kContext);
     526             :   Node* left = Parameter(Descriptor::kLeft);
     527             :   Node* right = Parameter(Descriptor::kRight);
     528          56 :   GenerateStringRelationalComparison(context, left, right,
     529          56 :                                      Operation::kLessThan);
     530          56 : }
     531             : 
     532         224 : TF_BUILTIN(StringLessThanOrEqual, StringBuiltinsAssembler) {
     533             :   Node* context = Parameter(Descriptor::kContext);
     534             :   Node* left = Parameter(Descriptor::kLeft);
     535             :   Node* right = Parameter(Descriptor::kRight);
     536          56 :   GenerateStringRelationalComparison(context, left, right,
     537          56 :                                      Operation::kLessThanOrEqual);
     538          56 : }
     539             : 
     540         224 : TF_BUILTIN(StringGreaterThan, StringBuiltinsAssembler) {
     541             :   Node* context = Parameter(Descriptor::kContext);
     542             :   Node* left = Parameter(Descriptor::kLeft);
     543             :   Node* right = Parameter(Descriptor::kRight);
     544          56 :   GenerateStringRelationalComparison(context, left, right,
     545          56 :                                      Operation::kGreaterThan);
     546          56 : }
     547             : 
     548         224 : TF_BUILTIN(StringGreaterThanOrEqual, StringBuiltinsAssembler) {
     549             :   Node* context = Parameter(Descriptor::kContext);
     550             :   Node* left = Parameter(Descriptor::kLeft);
     551             :   Node* right = Parameter(Descriptor::kRight);
     552          56 :   GenerateStringRelationalComparison(context, left, right,
     553          56 :                                      Operation::kGreaterThanOrEqual);
     554          56 : }
     555             : 
     556         224 : TF_BUILTIN(StringCharAt, StringBuiltinsAssembler) {
     557             :   TNode<String> receiver = CAST(Parameter(Descriptor::kReceiver));
     558             :   TNode<IntPtrT> position =
     559             :       UncheckedCast<IntPtrT>(Parameter(Descriptor::kPosition));
     560             : 
     561             :   // Load the character code at the {position} from the {receiver}.
     562          56 :   TNode<Int32T> code = StringCharCodeAt(receiver, position);
     563             : 
     564             :   // And return the single character string with only that {code}
     565          56 :   TNode<String> result = StringFromSingleCharCode(code);
     566          56 :   Return(result);
     567          56 : }
     568             : 
     569         224 : TF_BUILTIN(StringCodePointAtUTF16, StringBuiltinsAssembler) {
     570             :   Node* receiver = Parameter(Descriptor::kReceiver);
     571             :   Node* position = Parameter(Descriptor::kPosition);
     572             :   // TODO(sigurds) Figure out if passing length as argument pays off.
     573          56 :   TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
     574             :   // Load the character code at the {position} from the {receiver}.
     575             :   TNode<Int32T> code =
     576          56 :       LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF16);
     577             :   // And return it as TaggedSigned value.
     578             :   // TODO(turbofan): Allow builtins to return values untagged.
     579          56 :   TNode<Smi> result = SmiFromInt32(code);
     580          56 :   Return(result);
     581          56 : }
     582             : 
     583         224 : TF_BUILTIN(StringCodePointAtUTF32, StringBuiltinsAssembler) {
     584             :   Node* receiver = Parameter(Descriptor::kReceiver);
     585             :   Node* position = Parameter(Descriptor::kPosition);
     586             : 
     587             :   // TODO(sigurds) Figure out if passing length as argument pays off.
     588          56 :   TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
     589             :   // Load the character code at the {position} from the {receiver}.
     590             :   TNode<Int32T> code =
     591          56 :       LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF32);
     592             :   // And return it as TaggedSigned value.
     593             :   // TODO(turbofan): Allow builtins to return values untagged.
     594          56 :   TNode<Smi> result = SmiFromInt32(code);
     595          56 :   Return(result);
     596          56 : }
     597             : 
     598             : // -----------------------------------------------------------------------------
     599             : // ES6 section 21.1 String Objects
     600             : 
     601             : // ES6 #sec-string.fromcharcode
     602         168 : TF_BUILTIN(StringFromCharCode, CodeStubAssembler) {
     603             :   // TODO(ishell): use constants from Descriptor once the JSFunction linkage
     604             :   // arguments are reordered.
     605             :   TNode<Int32T> argc =
     606             :       UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount));
     607             :   Node* context = Parameter(Descriptor::kContext);
     608             : 
     609         168 :   CodeStubArguments arguments(this, ChangeInt32ToIntPtr(argc));
     610             :   // Check if we have exactly one argument (plus the implicit receiver), i.e.
     611             :   // if the parent frame is not an arguments adaptor frame.
     612          56 :   Label if_oneargument(this), if_notoneargument(this);
     613         168 :   Branch(Word32Equal(argc, Int32Constant(1)), &if_oneargument,
     614          56 :          &if_notoneargument);
     615             : 
     616          56 :   BIND(&if_oneargument);
     617             :   {
     618             :     // Single argument case, perform fast single character string cache lookup
     619             :     // for one-byte code units, or fall back to creating a single character
     620             :     // string on the fly otherwise.
     621         112 :     Node* code = arguments.AtIndex(0);
     622          56 :     Node* code32 = TruncateTaggedToWord32(context, code);
     623             :     TNode<Int32T> code16 =
     624         112 :         Signed(Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit)));
     625         112 :     Node* result = StringFromSingleCharCode(code16);
     626          56 :     arguments.PopAndReturn(result);
     627             :   }
     628             : 
     629          56 :   Node* code16 = nullptr;
     630          56 :   BIND(&if_notoneargument);
     631             :   {
     632          56 :     Label two_byte(this);
     633             :     // Assume that the resulting string contains only one-byte characters.
     634         112 :     Node* one_byte_result = AllocateSeqOneByteString(context, Unsigned(argc));
     635             : 
     636             :     TVARIABLE(IntPtrT, var_max_index);
     637          56 :     var_max_index = IntPtrConstant(0);
     638             : 
     639             :     // Iterate over the incoming arguments, converting them to 8-bit character
     640             :     // codes. Stop if any of the conversions generates a code that doesn't fit
     641             :     // in 8 bits.
     642         112 :     CodeStubAssembler::VariableList vars({&var_max_index}, zone());
     643          56 :     arguments.ForEach(vars, [this, context, &two_byte, &var_max_index, &code16,
     644         840 :                              one_byte_result](Node* arg) {
     645          56 :       Node* code32 = TruncateTaggedToWord32(context, arg);
     646         168 :       code16 = Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
     647             : 
     648         112 :       GotoIf(
     649         224 :           Int32GreaterThan(code16, Int32Constant(String::kMaxOneByteCharCode)),
     650          56 :           &two_byte);
     651             : 
     652             :       // The {code16} fits into the SeqOneByteString {one_byte_result}.
     653         112 :       Node* offset = ElementOffsetFromIndex(
     654             :           var_max_index.value(), UINT8_ELEMENTS,
     655             :           CodeStubAssembler::INTPTR_PARAMETERS,
     656          56 :           SeqOneByteString::kHeaderSize - kHeapObjectTag);
     657         112 :       StoreNoWriteBarrier(MachineRepresentation::kWord8, one_byte_result,
     658          56 :                           offset, code16);
     659         112 :       var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
     660         112 :     });
     661          56 :     arguments.PopAndReturn(one_byte_result);
     662             : 
     663          56 :     BIND(&two_byte);
     664             : 
     665             :     // At least one of the characters in the string requires a 16-bit
     666             :     // representation.  Allocate a SeqTwoByteString to hold the resulting
     667             :     // string.
     668         112 :     Node* two_byte_result = AllocateSeqTwoByteString(context, Unsigned(argc));
     669             : 
     670             :     // Copy the characters that have already been put in the 8-bit string into
     671             :     // their corresponding positions in the new 16-bit string.
     672          56 :     TNode<IntPtrT> zero = IntPtrConstant(0);
     673             :     CopyStringCharacters(one_byte_result, two_byte_result, zero, zero,
     674             :                          var_max_index.value(), String::ONE_BYTE_ENCODING,
     675          56 :                          String::TWO_BYTE_ENCODING);
     676             : 
     677             :     // Write the character that caused the 8-bit to 16-bit fault.
     678             :     Node* max_index_offset =
     679         112 :         ElementOffsetFromIndex(var_max_index.value(), UINT16_ELEMENTS,
     680             :                                CodeStubAssembler::INTPTR_PARAMETERS,
     681             :                                SeqTwoByteString::kHeaderSize - kHeapObjectTag);
     682             :     StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
     683          56 :                         max_index_offset, code16);
     684          56 :     var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
     685             : 
     686             :     // Resume copying the passed-in arguments from the same place where the
     687             :     // 8-bit copy stopped, but this time copying over all of the characters
     688             :     // using a 16-bit representation.
     689          56 :     arguments.ForEach(
     690             :         vars,
     691         560 :         [this, context, two_byte_result, &var_max_index](Node* arg) {
     692          56 :           Node* code32 = TruncateTaggedToWord32(context, arg);
     693             :           Node* code16 =
     694         168 :               Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
     695             : 
     696         112 :           Node* offset = ElementOffsetFromIndex(
     697             :               var_max_index.value(), UINT16_ELEMENTS,
     698             :               CodeStubAssembler::INTPTR_PARAMETERS,
     699          56 :               SeqTwoByteString::kHeaderSize - kHeapObjectTag);
     700          56 :           StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
     701          56 :                               offset, code16);
     702         112 :           var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
     703          56 :         },
     704          56 :         var_max_index.value());
     705             : 
     706          56 :     arguments.PopAndReturn(two_byte_result);
     707             :   }
     708          56 : }
     709             : 
     710             : // ES6 #sec-string.prototype.charat
     711         336 : TF_BUILTIN(StringPrototypeCharAt, StringBuiltinsAssembler) {
     712          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
     713          56 :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
     714          56 :   TNode<Object> maybe_position = CAST(Parameter(Descriptor::kPosition));
     715             : 
     716         168 :   GenerateStringAt("String.prototype.charAt", context, receiver, maybe_position,
     717         112 :                    EmptyStringConstant(),
     718             :                    [this](TNode<String> string, TNode<IntPtrT> length,
     719         168 :                           TNode<IntPtrT> index) {
     720          56 :                      TNode<Int32T> code = StringCharCodeAt(string, index);
     721          56 :                      return StringFromSingleCharCode(code);
     722          56 :                    });
     723          56 : }
     724             : 
     725             : // ES6 #sec-string.prototype.charcodeat
     726         336 : TF_BUILTIN(StringPrototypeCharCodeAt, StringBuiltinsAssembler) {
     727          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
     728          56 :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
     729          56 :   TNode<Object> maybe_position = CAST(Parameter(Descriptor::kPosition));
     730             : 
     731         168 :   GenerateStringAt("String.prototype.charCodeAt", context, receiver,
     732         112 :                    maybe_position, NanConstant(),
     733             :                    [this](TNode<String> receiver, TNode<IntPtrT> length,
     734         168 :                           TNode<IntPtrT> index) {
     735         112 :                      Node* value = StringCharCodeAt(receiver, index);
     736          56 :                      return SmiFromInt32(value);
     737          56 :                    });
     738          56 : }
     739             : 
     740             : // ES6 #sec-string.prototype.codepointat
     741         336 : TF_BUILTIN(StringPrototypeCodePointAt, StringBuiltinsAssembler) {
     742          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
     743          56 :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
     744          56 :   TNode<Object> maybe_position = CAST(Parameter(Descriptor::kPosition));
     745             : 
     746         168 :   GenerateStringAt("String.prototype.codePointAt", context, receiver,
     747         112 :                    maybe_position, UndefinedConstant(),
     748             :                    [this](TNode<String> receiver, TNode<IntPtrT> length,
     749         168 :                           TNode<IntPtrT> index) {
     750             :                      // This is always a call to a builtin from Javascript,
     751             :                      // so we need to produce UTF32.
     752         112 :                      Node* value = LoadSurrogatePairAt(receiver, length, index,
     753         112 :                                                        UnicodeEncoding::UTF32);
     754          56 :                      return SmiFromInt32(value);
     755          56 :                    });
     756          56 : }
     757             : 
     758             : // ES6 String.prototype.concat(...args)
     759             : // ES6 #sec-string.prototype.concat
     760         224 : TF_BUILTIN(StringPrototypeConcat, CodeStubAssembler) {
     761             :   // TODO(ishell): use constants from Descriptor once the JSFunction linkage
     762             :   // arguments are reordered.
     763             :   CodeStubArguments arguments(
     764             :       this,
     765         168 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount)));
     766          56 :   TNode<Object> receiver = arguments.GetReceiver();
     767             :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
     768             : 
     769             :   // Check that {receiver} is coercible to Object and convert it to a String.
     770             :   receiver = ToThisString(context, receiver, "String.prototype.concat");
     771             : 
     772             :   // Concatenate all the arguments passed to this builtin.
     773         112 :   VARIABLE(var_result, MachineRepresentation::kTagged);
     774          56 :   var_result.Bind(receiver);
     775         168 :   arguments.ForEach(
     776             :       CodeStubAssembler::VariableList({&var_result}, zone()),
     777         336 :       [this, context, &var_result](Node* arg) {
     778         112 :         arg = ToString_Inline(context, arg);
     779         224 :         var_result.Bind(CallStub(CodeFactory::StringAdd(isolate()), context,
     780         224 :                                  var_result.value(), arg));
     781         112 :       });
     782          56 :   arguments.PopAndReturn(var_result.value());
     783          56 : }
     784             : 
     785         168 : void StringBuiltinsAssembler::StringIndexOf(
     786             :     Node* const subject_string, Node* const search_string, Node* const position,
     787             :     const std::function<void(Node*)>& f_return) {
     788             :   CSA_ASSERT(this, IsString(subject_string));
     789             :   CSA_ASSERT(this, IsString(search_string));
     790             :   CSA_ASSERT(this, TaggedIsSmi(position));
     791             : 
     792         168 :   TNode<IntPtrT> const int_zero = IntPtrConstant(0);
     793         168 :   TNode<IntPtrT> const search_length = LoadStringLengthAsWord(search_string);
     794         168 :   TNode<IntPtrT> const subject_length = LoadStringLengthAsWord(subject_string);
     795         336 :   TNode<IntPtrT> const start_position = IntPtrMax(SmiUntag(position), int_zero);
     796             : 
     797         168 :   Label zero_length_needle(this), return_minus_1(this);
     798             :   {
     799         336 :     GotoIf(IntPtrEqual(int_zero, search_length), &zero_length_needle);
     800             : 
     801             :     // Check that the needle fits in the start position.
     802         336 :     GotoIfNot(IntPtrLessThanOrEqual(search_length,
     803         168 :                                     IntPtrSub(subject_length, start_position)),
     804         168 :               &return_minus_1);
     805             :   }
     806             : 
     807             :   // If the string pointers are identical, we can just return 0. Note that this
     808             :   // implies {start_position} == 0 since we've passed the check above.
     809         168 :   Label return_zero(this);
     810         336 :   GotoIf(WordEqual(subject_string, search_string), &return_zero);
     811             : 
     812             :   // Try to unpack subject and search strings. Bail to runtime if either needs
     813             :   // to be flattened.
     814         336 :   ToDirectStringAssembler subject_to_direct(state(), subject_string);
     815         336 :   ToDirectStringAssembler search_to_direct(state(), search_string);
     816             : 
     817         168 :   Label call_runtime_unchecked(this, Label::kDeferred);
     818             : 
     819         168 :   subject_to_direct.TryToDirect(&call_runtime_unchecked);
     820         168 :   search_to_direct.TryToDirect(&call_runtime_unchecked);
     821             : 
     822             :   // Load pointers to string data.
     823             :   Node* const subject_ptr =
     824             :       subject_to_direct.PointerToData(&call_runtime_unchecked);
     825             :   Node* const search_ptr =
     826             :       search_to_direct.PointerToData(&call_runtime_unchecked);
     827             : 
     828             :   Node* const subject_offset = subject_to_direct.offset();
     829             :   Node* const search_offset = search_to_direct.offset();
     830             : 
     831             :   // Like String::IndexOf, the actual matching is done by the optimized
     832             :   // SearchString method in string-search.h. Dispatch based on string instance
     833             :   // types, then call straight into C++ for matching.
     834             : 
     835             :   CSA_ASSERT(this, IntPtrGreaterThan(search_length, int_zero));
     836             :   CSA_ASSERT(this, IntPtrGreaterThanOrEqual(start_position, int_zero));
     837             :   CSA_ASSERT(this, IntPtrGreaterThanOrEqual(subject_length, start_position));
     838             :   CSA_ASSERT(this,
     839             :              IntPtrLessThanOrEqual(search_length,
     840             :                                    IntPtrSub(subject_length, start_position)));
     841             : 
     842         168 :   Label one_one(this), one_two(this), two_one(this), two_two(this);
     843             :   DispatchOnStringEncodings(subject_to_direct.instance_type(),
     844             :                             search_to_direct.instance_type(), &one_one,
     845         168 :                             &one_two, &two_one, &two_two);
     846             : 
     847             :   typedef const uint8_t onebyte_t;
     848             :   typedef const uc16 twobyte_t;
     849             : 
     850         168 :   BIND(&one_one);
     851             :   {
     852             :     Node* const adjusted_subject_ptr = PointerToStringDataAtIndex(
     853         168 :         subject_ptr, subject_offset, String::ONE_BYTE_ENCODING);
     854             :     Node* const adjusted_search_ptr = PointerToStringDataAtIndex(
     855         168 :         search_ptr, search_offset, String::ONE_BYTE_ENCODING);
     856             : 
     857         168 :     Label direct_memchr_call(this), generic_fast_path(this);
     858         504 :     Branch(IntPtrEqual(search_length, IntPtrConstant(1)), &direct_memchr_call,
     859         168 :            &generic_fast_path);
     860             : 
     861             :     // An additional fast path that calls directly into memchr for 1-length
     862             :     // search strings.
     863         168 :     BIND(&direct_memchr_call);
     864             :     {
     865         336 :       Node* const string_addr = IntPtrAdd(adjusted_subject_ptr, start_position);
     866             :       Node* const search_length = IntPtrSub(subject_length, start_position);
     867             :       Node* const search_byte =
     868         504 :           ChangeInt32ToIntPtr(Load(MachineType::Uint8(), adjusted_search_ptr));
     869             : 
     870             :       Node* const memchr =
     871         336 :           ExternalConstant(ExternalReference::libc_memchr_function());
     872             :       Node* const result_address =
     873             :           CallCFunction(memchr, MachineType::Pointer(),
     874             :                         std::make_pair(MachineType::Pointer(), string_addr),
     875             :                         std::make_pair(MachineType::IntPtr(), search_byte),
     876         168 :                         std::make_pair(MachineType::UintPtr(), search_length));
     877         336 :       GotoIf(WordEqual(result_address, int_zero), &return_minus_1);
     878             :       Node* const result_index =
     879         504 :           IntPtrAdd(IntPtrSub(result_address, string_addr), start_position);
     880         336 :       f_return(SmiTag(result_index));
     881             :     }
     882             : 
     883         168 :     BIND(&generic_fast_path);
     884             :     {
     885             :       Node* const result = CallSearchStringRaw<onebyte_t, onebyte_t>(
     886             :           adjusted_subject_ptr, subject_length, adjusted_search_ptr,
     887         168 :           search_length, start_position);
     888         336 :       f_return(SmiTag(result));
     889             :     }
     890             :   }
     891             : 
     892         168 :   BIND(&one_two);
     893             :   {
     894             :     Node* const adjusted_subject_ptr = PointerToStringDataAtIndex(
     895         168 :         subject_ptr, subject_offset, String::ONE_BYTE_ENCODING);
     896             :     Node* const adjusted_search_ptr = PointerToStringDataAtIndex(
     897         168 :         search_ptr, search_offset, String::TWO_BYTE_ENCODING);
     898             : 
     899             :     Node* const result = CallSearchStringRaw<onebyte_t, twobyte_t>(
     900             :         adjusted_subject_ptr, subject_length, adjusted_search_ptr,
     901         168 :         search_length, start_position);
     902         336 :     f_return(SmiTag(result));
     903             :   }
     904             : 
     905         168 :   BIND(&two_one);
     906             :   {
     907             :     Node* const adjusted_subject_ptr = PointerToStringDataAtIndex(
     908         168 :         subject_ptr, subject_offset, String::TWO_BYTE_ENCODING);
     909             :     Node* const adjusted_search_ptr = PointerToStringDataAtIndex(
     910         168 :         search_ptr, search_offset, String::ONE_BYTE_ENCODING);
     911             : 
     912             :     Node* const result = CallSearchStringRaw<twobyte_t, onebyte_t>(
     913             :         adjusted_subject_ptr, subject_length, adjusted_search_ptr,
     914         168 :         search_length, start_position);
     915         336 :     f_return(SmiTag(result));
     916             :   }
     917             : 
     918         168 :   BIND(&two_two);
     919             :   {
     920             :     Node* const adjusted_subject_ptr = PointerToStringDataAtIndex(
     921         168 :         subject_ptr, subject_offset, String::TWO_BYTE_ENCODING);
     922             :     Node* const adjusted_search_ptr = PointerToStringDataAtIndex(
     923         168 :         search_ptr, search_offset, String::TWO_BYTE_ENCODING);
     924             : 
     925             :     Node* const result = CallSearchStringRaw<twobyte_t, twobyte_t>(
     926             :         adjusted_subject_ptr, subject_length, adjusted_search_ptr,
     927         168 :         search_length, start_position);
     928         336 :     f_return(SmiTag(result));
     929             :   }
     930             : 
     931         168 :   BIND(&return_minus_1);
     932         336 :   f_return(SmiConstant(-1));
     933             : 
     934         168 :   BIND(&return_zero);
     935         336 :   f_return(SmiConstant(0));
     936             : 
     937         168 :   BIND(&zero_length_needle);
     938             :   {
     939         168 :     Comment("0-length search_string");
     940         504 :     f_return(SmiTag(IntPtrMin(subject_length, start_position)));
     941             :   }
     942             : 
     943         168 :   BIND(&call_runtime_unchecked);
     944             :   {
     945             :     // Simplified version of the runtime call where the types of the arguments
     946             :     // are already known due to type checks in this stub.
     947         168 :     Comment("Call Runtime Unchecked");
     948             :     Node* result =
     949         336 :         CallRuntime(Runtime::kStringIndexOfUnchecked, NoContextConstant(),
     950             :                     subject_string, search_string, position);
     951             :     f_return(result);
     952             :   }
     953         168 : }
     954             : 
     955             : // ES6 String.prototype.indexOf(searchString [, position])
     956             : // #sec-string.prototype.indexof
     957             : // Unchecked helper for builtins lowering.
     958         224 : TF_BUILTIN(StringIndexOf, StringBuiltinsAssembler) {
     959             :   Node* receiver = Parameter(Descriptor::kReceiver);
     960             :   Node* search_string = Parameter(Descriptor::kSearchString);
     961             :   Node* position = Parameter(Descriptor::kPosition);
     962         112 :   StringIndexOf(receiver, search_string, position,
     963         560 :                 [this](Node* result) { this->Return(result); });
     964          56 : }
     965             : 
     966             : // ES6 String.prototype.includes(searchString [, position])
     967             : // #sec-string.prototype.includes
     968         280 : TF_BUILTIN(StringPrototypeIncludes, StringIncludesIndexOfAssembler) {
     969             :   TNode<IntPtrT> argc =
     970          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
     971          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
     972          56 :   Generate(kIncludes, argc, context);
     973          56 : }
     974             : 
     975             : // ES6 String.prototype.indexOf(searchString [, position])
     976             : // #sec-string.prototype.indexof
     977         280 : TF_BUILTIN(StringPrototypeIndexOf, StringIncludesIndexOfAssembler) {
     978             :   TNode<IntPtrT> argc =
     979          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
     980          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
     981          56 :   Generate(kIndexOf, argc, context);
     982          56 : }
     983             : 
     984         112 : void StringIncludesIndexOfAssembler::Generate(SearchVariant variant,
     985             :                                               TNode<IntPtrT> argc,
     986             :                                               TNode<Context> context) {
     987         112 :   CodeStubArguments arguments(this, argc);
     988         224 :   Node* const receiver = arguments.GetReceiver();
     989             : 
     990         224 :   VARIABLE(var_search_string, MachineRepresentation::kTagged);
     991         224 :   VARIABLE(var_position, MachineRepresentation::kTagged);
     992         112 :   Label argc_1(this), argc_2(this), call_runtime(this, Label::kDeferred),
     993         112 :       fast_path(this);
     994             : 
     995         336 :   GotoIf(IntPtrEqual(argc, IntPtrConstant(1)), &argc_1);
     996         336 :   GotoIf(IntPtrGreaterThan(argc, IntPtrConstant(1)), &argc_2);
     997             :   {
     998         112 :     Comment("0 Argument case");
     999             :     CSA_ASSERT(this, IntPtrEqual(argc, IntPtrConstant(0)));
    1000         224 :     Node* const undefined = UndefinedConstant();
    1001         112 :     var_search_string.Bind(undefined);
    1002         112 :     var_position.Bind(undefined);
    1003         112 :     Goto(&call_runtime);
    1004             :   }
    1005         112 :   BIND(&argc_1);
    1006             :   {
    1007         112 :     Comment("1 Argument case");
    1008         224 :     var_search_string.Bind(arguments.AtIndex(0));
    1009         224 :     var_position.Bind(SmiConstant(0));
    1010         112 :     Goto(&fast_path);
    1011             :   }
    1012         112 :   BIND(&argc_2);
    1013             :   {
    1014         112 :     Comment("2 Argument case");
    1015         224 :     var_search_string.Bind(arguments.AtIndex(0));
    1016         224 :     var_position.Bind(arguments.AtIndex(1));
    1017         336 :     GotoIfNot(TaggedIsSmi(var_position.value()), &call_runtime);
    1018         112 :     Goto(&fast_path);
    1019             :   }
    1020         112 :   BIND(&fast_path);
    1021             :   {
    1022         112 :     Comment("Fast Path");
    1023         112 :     Node* const search = var_search_string.value();
    1024         112 :     Node* const position = var_position.value();
    1025         224 :     GotoIf(TaggedIsSmi(receiver), &call_runtime);
    1026         224 :     GotoIf(TaggedIsSmi(search), &call_runtime);
    1027         224 :     GotoIfNot(IsString(receiver), &call_runtime);
    1028         224 :     GotoIfNot(IsString(search), &call_runtime);
    1029             : 
    1030        1232 :     StringIndexOf(receiver, search, position, [&](Node* result) {
    1031             :       CSA_ASSERT(this, TaggedIsSmi(result));
    1032        2016 :       arguments.PopAndReturn((variant == kIndexOf)
    1033             :                                  ? result
    1034        1008 :                                  : SelectBooleanConstant(SmiGreaterThanOrEqual(
    1035        2016 :                                        CAST(result), SmiConstant(0))));
    1036        1120 :     });
    1037             :   }
    1038         112 :   BIND(&call_runtime);
    1039             :   {
    1040         112 :     Comment("Call Runtime");
    1041         112 :     Runtime::FunctionId runtime = variant == kIndexOf
    1042             :                                       ? Runtime::kStringIndexOf
    1043         112 :                                       : Runtime::kStringIncludes;
    1044             :     Node* const result =
    1045             :         CallRuntime(runtime, context, receiver, var_search_string.value(),
    1046         112 :                     var_position.value());
    1047         112 :     arguments.PopAndReturn(result);
    1048             :   }
    1049         112 : }
    1050             : 
    1051         336 : void StringBuiltinsAssembler::RequireObjectCoercible(Node* const context,
    1052             :                                                      Node* const value,
    1053             :                                                      const char* method_name) {
    1054         672 :   Label out(this), throw_exception(this, Label::kDeferred);
    1055         672 :   Branch(IsNullOrUndefined(value), &throw_exception, &out);
    1056             : 
    1057         336 :   BIND(&throw_exception);
    1058             :   ThrowTypeError(context, MessageTemplate::kCalledOnNullOrUndefined,
    1059         336 :                  method_name);
    1060             : 
    1061         336 :   BIND(&out);
    1062         336 : }
    1063             : 
    1064         280 : void StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
    1065             :     Node* const context, Node* const object, Node* const maybe_string,
    1066             :     Handle<Symbol> symbol, DescriptorIndexAndName symbol_index,
    1067             :     const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
    1068         560 :   Label out(this);
    1069             : 
    1070             :   // Smis definitely don't have an attached symbol.
    1071         560 :   GotoIf(TaggedIsSmi(object), &out);
    1072             : 
    1073             :   // Take the fast path for RegExps.
    1074             :   // There's two conditions: {object} needs to be a fast regexp, and
    1075             :   // {maybe_string} must be a string (we can't call ToString on the fast path
    1076             :   // since it may mutate {object}).
    1077             :   {
    1078         280 :     Label stub_call(this), slow_lookup(this);
    1079             : 
    1080         560 :     GotoIf(TaggedIsSmi(maybe_string), &slow_lookup);
    1081         560 :     GotoIfNot(IsString(maybe_string), &slow_lookup);
    1082             : 
    1083             :     RegExpBuiltinsAssembler regexp_asm(state());
    1084         560 :     regexp_asm.BranchIfFastRegExp(context, object, LoadMap(object),
    1085         280 :                                   symbol_index, &stub_call, &slow_lookup);
    1086             : 
    1087         280 :     BIND(&stub_call);
    1088             :     // TODO(jgruber): Add a no-JS scope once it exists.
    1089             :     regexp_call();
    1090             : 
    1091         280 :     BIND(&slow_lookup);
    1092             :   }
    1093             : 
    1094         560 :   GotoIf(IsNullOrUndefined(object), &out);
    1095             : 
    1096             :   // Fall back to a slow lookup of {object[symbol]}.
    1097             :   //
    1098             :   // The spec uses GetMethod({object}, {symbol}), which has a few quirks:
    1099             :   // * null values are turned into undefined, and
    1100             :   // * an exception is thrown if the value is not undefined, null, or callable.
    1101             :   // We handle the former by jumping to {out} for null values as well, while
    1102             :   // the latter is already handled by the Call({maybe_func}) operation.
    1103             : 
    1104         560 :   Node* const maybe_func = GetProperty(context, object, symbol);
    1105         560 :   GotoIf(IsUndefined(maybe_func), &out);
    1106         560 :   GotoIf(IsNull(maybe_func), &out);
    1107             : 
    1108             :   // Attempt to call the function.
    1109             :   generic_call(maybe_func);
    1110             : 
    1111         280 :   BIND(&out);
    1112         280 : }
    1113             : 
    1114         112 : TNode<Smi> StringBuiltinsAssembler::IndexOfDollarChar(Node* const context,
    1115             :                                                       Node* const string) {
    1116             :   CSA_ASSERT(this, IsString(string));
    1117             : 
    1118             :   TNode<String> const dollar_string = HeapConstant(
    1119         224 :       isolate()->factory()->LookupSingleCharacterStringFromCode('$'));
    1120             :   TNode<Smi> const dollar_ix =
    1121         224 :       CAST(CallBuiltin(Builtins::kStringIndexOf, context, string, dollar_string,
    1122             :                        SmiConstant(0)));
    1123         112 :   return dollar_ix;
    1124             : }
    1125             : 
    1126          56 : compiler::Node* StringBuiltinsAssembler::GetSubstitution(
    1127             :     Node* context, Node* subject_string, Node* match_start_index,
    1128             :     Node* match_end_index, Node* replace_string) {
    1129             :   CSA_ASSERT(this, IsString(subject_string));
    1130             :   CSA_ASSERT(this, IsString(replace_string));
    1131             :   CSA_ASSERT(this, TaggedIsPositiveSmi(match_start_index));
    1132             :   CSA_ASSERT(this, TaggedIsPositiveSmi(match_end_index));
    1133             : 
    1134         112 :   VARIABLE(var_result, MachineRepresentation::kTagged, replace_string);
    1135          56 :   Label runtime(this), out(this);
    1136             : 
    1137             :   // In this primitive implementation we simply look for the next '$' char in
    1138             :   // {replace_string}. If it doesn't exist, we can simply return
    1139             :   // {replace_string} itself. If it does, then we delegate to
    1140             :   // String::GetSubstitution, passing in the index of the first '$' to avoid
    1141             :   // repeated scanning work.
    1142             :   // TODO(jgruber): Possibly extend this in the future to handle more complex
    1143             :   // cases without runtime calls.
    1144             : 
    1145          56 :   TNode<Smi> const dollar_index = IndexOfDollarChar(context, replace_string);
    1146         112 :   Branch(SmiIsNegative(dollar_index), &out, &runtime);
    1147             : 
    1148          56 :   BIND(&runtime);
    1149             :   {
    1150             :     CSA_ASSERT(this, TaggedIsPositiveSmi(dollar_index));
    1151             : 
    1152             :     Node* const matched =
    1153         112 :         CallBuiltin(Builtins::kStringSubstring, context, subject_string,
    1154         168 :                     SmiUntag(match_start_index), SmiUntag(match_end_index));
    1155             :     Node* const replacement_string =
    1156             :         CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
    1157             :                     match_start_index, replace_string, dollar_index);
    1158          56 :     var_result.Bind(replacement_string);
    1159             : 
    1160          56 :     Goto(&out);
    1161             :   }
    1162             : 
    1163          56 :   BIND(&out);
    1164         112 :   return var_result.value();
    1165             : }
    1166             : 
    1167             : // ES6 #sec-string.prototype.replace
    1168         280 : TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
    1169         112 :   Label out(this);
    1170             : 
    1171             :   Node* const receiver = Parameter(Descriptor::kReceiver);
    1172             :   Node* const search = Parameter(Descriptor::kSearch);
    1173             :   Node* const replace = Parameter(Descriptor::kReplace);
    1174             :   Node* const context = Parameter(Descriptor::kContext);
    1175             : 
    1176          56 :   TNode<Smi> const smi_zero = SmiConstant(0);
    1177             : 
    1178          56 :   RequireObjectCoercible(context, receiver, "String.prototype.replace");
    1179             : 
    1180             :   // Redirect to replacer method if {search[@@replace]} is not undefined.
    1181             : 
    1182         224 :   MaybeCallFunctionAtSymbol(
    1183             :       context, search, receiver, isolate()->factory()->replace_symbol(),
    1184             :       DescriptorIndexAndName{JSRegExp::kSymbolReplaceFunctionDescriptorIndex,
    1185             :                              RootIndex::kreplace_symbol},
    1186          56 :       [=]() {
    1187         224 :         Return(CallBuiltin(Builtins::kRegExpReplace, context, search, receiver,
    1188         280 :                            replace));
    1189          56 :       },
    1190          56 :       [=](Node* fn) {
    1191         168 :         Callable call_callable = CodeFactory::Call(isolate());
    1192         168 :         Return(CallJS(call_callable, context, fn, search, receiver, replace));
    1193         112 :       });
    1194             : 
    1195             :   // Convert {receiver} and {search} to strings.
    1196             : 
    1197          56 :   TNode<String> const subject_string = ToString_Inline(context, receiver);
    1198          56 :   TNode<String> const search_string = ToString_Inline(context, search);
    1199             : 
    1200          56 :   TNode<IntPtrT> const subject_length = LoadStringLengthAsWord(subject_string);
    1201          56 :   TNode<IntPtrT> const search_length = LoadStringLengthAsWord(search_string);
    1202             : 
    1203             :   // Fast-path single-char {search}, long cons {receiver}, and simple string
    1204             :   // {replace}.
    1205             :   {
    1206          56 :     Label next(this);
    1207             : 
    1208         168 :     GotoIfNot(WordEqual(search_length, IntPtrConstant(1)), &next);
    1209         168 :     GotoIfNot(IntPtrGreaterThan(subject_length, IntPtrConstant(0xFF)), &next);
    1210         112 :     GotoIf(TaggedIsSmi(replace), &next);
    1211         112 :     GotoIfNot(IsString(replace), &next);
    1212             : 
    1213         112 :     Node* const subject_instance_type = LoadInstanceType(subject_string);
    1214         112 :     GotoIfNot(IsConsStringInstanceType(subject_instance_type), &next);
    1215             : 
    1216         168 :     GotoIf(TaggedIsPositiveSmi(IndexOfDollarChar(context, replace)), &next);
    1217             : 
    1218             :     // Searching by traversing a cons string tree and replace with cons of
    1219             :     // slices works only when the replaced string is a single character, being
    1220             :     // replaced by a simple string and only pays off for long strings.
    1221             :     // TODO(jgruber): Reevaluate if this is still beneficial.
    1222             :     // TODO(jgruber): TailCallRuntime when it correctly handles adapter frames.
    1223          56 :     Return(CallRuntime(Runtime::kStringReplaceOneCharWithString, context,
    1224          56 :                        subject_string, search_string, replace));
    1225             : 
    1226          56 :     BIND(&next);
    1227             :   }
    1228             : 
    1229             :   // TODO(jgruber): Extend StringIndexOf to handle two-byte strings and
    1230             :   // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars
    1231             :   // (2-byte).
    1232             : 
    1233             :   TNode<Smi> const match_start_index =
    1234          56 :       CAST(CallBuiltin(Builtins::kStringIndexOf, context, subject_string,
    1235             :                        search_string, smi_zero));
    1236             : 
    1237             :   // Early exit if no match found.
    1238             :   {
    1239          56 :     Label next(this), return_subject(this);
    1240             : 
    1241         112 :     GotoIfNot(SmiIsNegative(match_start_index), &next);
    1242             : 
    1243             :     // The spec requires to perform ToString(replace) if the {replace} is not
    1244             :     // callable even if we are going to exit here.
    1245             :     // Since ToString() being applied to Smi does not have side effects for
    1246             :     // numbers we can skip it.
    1247         112 :     GotoIf(TaggedIsSmi(replace), &return_subject);
    1248         168 :     GotoIf(IsCallableMap(LoadMap(replace)), &return_subject);
    1249             : 
    1250             :     // TODO(jgruber): Could introduce ToStringSideeffectsStub which only
    1251             :     // performs observable parts of ToString.
    1252          56 :     ToString_Inline(context, replace);
    1253          56 :     Goto(&return_subject);
    1254             : 
    1255          56 :     BIND(&return_subject);
    1256          56 :     Return(subject_string);
    1257             : 
    1258          56 :     BIND(&next);
    1259             :   }
    1260             : 
    1261             :   TNode<Smi> const match_end_index =
    1262          56 :       SmiAdd(match_start_index, SmiFromIntPtr(search_length));
    1263             : 
    1264         168 :   VARIABLE(var_result, MachineRepresentation::kTagged, EmptyStringConstant());
    1265             : 
    1266             :   // Compute the prefix.
    1267             :   {
    1268          56 :     Label next(this);
    1269             : 
    1270         112 :     GotoIf(SmiEqual(match_start_index, smi_zero), &next);
    1271             :     Node* const prefix =
    1272         112 :         CallBuiltin(Builtins::kStringSubstring, context, subject_string,
    1273         112 :                     IntPtrConstant(0), SmiUntag(match_start_index));
    1274          56 :     var_result.Bind(prefix);
    1275             : 
    1276          56 :     Goto(&next);
    1277          56 :     BIND(&next);
    1278             :   }
    1279             : 
    1280             :   // Compute the string to replace with.
    1281             : 
    1282          56 :   Label if_iscallablereplace(this), if_notcallablereplace(this);
    1283         112 :   GotoIf(TaggedIsSmi(replace), &if_notcallablereplace);
    1284         168 :   Branch(IsCallableMap(LoadMap(replace)), &if_iscallablereplace,
    1285          56 :          &if_notcallablereplace);
    1286             : 
    1287          56 :   BIND(&if_iscallablereplace);
    1288             :   {
    1289          56 :     Callable call_callable = CodeFactory::Call(isolate());
    1290             :     Node* const replacement =
    1291         112 :         CallJS(call_callable, context, replace, UndefinedConstant(),
    1292          56 :                search_string, match_start_index, subject_string);
    1293         112 :     Node* const replacement_string = ToString_Inline(context, replacement);
    1294         112 :     var_result.Bind(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
    1295         168 :                                 var_result.value(), replacement_string));
    1296          56 :     Goto(&out);
    1297             :   }
    1298             : 
    1299          56 :   BIND(&if_notcallablereplace);
    1300             :   {
    1301         112 :     Node* const replace_string = ToString_Inline(context, replace);
    1302             :     Node* const replacement =
    1303             :         GetSubstitution(context, subject_string, match_start_index,
    1304          56 :                         match_end_index, replace_string);
    1305         112 :     var_result.Bind(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
    1306         168 :                                 var_result.value(), replacement));
    1307          56 :     Goto(&out);
    1308             :   }
    1309             : 
    1310          56 :   BIND(&out);
    1311             :   {
    1312             :     Node* const suffix =
    1313         112 :         CallBuiltin(Builtins::kStringSubstring, context, subject_string,
    1314         112 :                     SmiUntag(match_end_index), subject_length);
    1315         112 :     Node* const result = CallBuiltin(Builtins::kStringAdd_CheckNone, context,
    1316         112 :                                      var_result.value(), suffix);
    1317          56 :     Return(result);
    1318             :   }
    1319          56 : }
    1320             : 
    1321         112 : class StringMatchSearchAssembler : public StringBuiltinsAssembler {
    1322             :  public:
    1323             :   explicit StringMatchSearchAssembler(compiler::CodeAssemblerState* state)
    1324             :       : StringBuiltinsAssembler(state) {}
    1325             : 
    1326             :  protected:
    1327             :   enum Variant { kMatch, kSearch };
    1328             : 
    1329         112 :   void Generate(Variant variant, const char* method_name,
    1330             :                 TNode<Object> receiver, TNode<Object> maybe_regexp,
    1331             :                 TNode<Context> context) {
    1332         224 :     Label call_regexp_match_search(this);
    1333             : 
    1334             :     Builtins::Name builtin;
    1335             :     Handle<Symbol> symbol;
    1336             :     DescriptorIndexAndName property_to_check;
    1337         112 :     if (variant == kMatch) {
    1338             :       builtin = Builtins::kRegExpMatchFast;
    1339          56 :       symbol = isolate()->factory()->match_symbol();
    1340             :       property_to_check =
    1341             :           DescriptorIndexAndName{JSRegExp::kSymbolMatchFunctionDescriptorIndex,
    1342             :                                  RootIndex::kmatch_symbol};
    1343             :     } else {
    1344             :       builtin = Builtins::kRegExpSearchFast;
    1345          56 :       symbol = isolate()->factory()->search_symbol();
    1346             :       property_to_check =
    1347             :           DescriptorIndexAndName{JSRegExp::kSymbolSearchFunctionDescriptorIndex,
    1348             :                                  RootIndex::ksearch_symbol};
    1349             :     }
    1350             : 
    1351         112 :     RequireObjectCoercible(context, receiver, method_name);
    1352             : 
    1353         336 :     MaybeCallFunctionAtSymbol(
    1354             :         context, maybe_regexp, receiver, symbol, property_to_check,
    1355         336 :         [=] { Return(CallBuiltin(builtin, context, maybe_regexp, receiver)); },
    1356         112 :         [=](Node* fn) {
    1357         336 :           Callable call_callable = CodeFactory::Call(isolate());
    1358         224 :           Return(CallJS(call_callable, context, fn, maybe_regexp, receiver));
    1359         224 :         });
    1360             : 
    1361             :     // maybe_regexp is not a RegExp nor has [@@match / @@search] property.
    1362             :     {
    1363             :       RegExpBuiltinsAssembler regexp_asm(state());
    1364             : 
    1365         112 :       TNode<String> receiver_string = ToString_Inline(context, receiver);
    1366         112 :       TNode<Context> native_context = LoadNativeContext(context);
    1367         112 :       TNode<HeapObject> regexp_function = CAST(
    1368             :           LoadContextElement(native_context, Context::REGEXP_FUNCTION_INDEX));
    1369             :       TNode<Map> initial_map = CAST(LoadObjectField(
    1370             :           regexp_function, JSFunction::kPrototypeOrInitialMapOffset));
    1371             :       TNode<Object> regexp = regexp_asm.RegExpCreate(
    1372         112 :           context, initial_map, maybe_regexp, EmptyStringConstant());
    1373             : 
    1374         112 :       Label fast_path(this), slow_path(this);
    1375         112 :       regexp_asm.BranchIfFastRegExp(context, regexp, initial_map,
    1376         112 :                                     property_to_check, &fast_path, &slow_path);
    1377             : 
    1378         112 :       BIND(&fast_path);
    1379         224 :       Return(CallBuiltin(builtin, context, regexp, receiver_string));
    1380             : 
    1381         112 :       BIND(&slow_path);
    1382             :       {
    1383         112 :         TNode<Object> maybe_func = GetProperty(context, regexp, symbol);
    1384         112 :         Callable call_callable = CodeFactory::Call(isolate());
    1385         224 :         Return(CallJS(call_callable, context, maybe_func, regexp,
    1386         112 :                       receiver_string));
    1387             :       }
    1388             :     }
    1389         112 :   }
    1390             : };
    1391             : 
    1392             : // ES6 #sec-string.prototype.match
    1393         336 : TF_BUILTIN(StringPrototypeMatch, StringMatchSearchAssembler) {
    1394          56 :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
    1395          56 :   TNode<Object> maybe_regexp = CAST(Parameter(Descriptor::kRegexp));
    1396          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1397             : 
    1398          56 :   Generate(kMatch, "String.prototype.match", receiver, maybe_regexp, context);
    1399          56 : }
    1400             : 
    1401             : // ES #sec-string.prototype.matchAll
    1402         336 : TF_BUILTIN(StringPrototypeMatchAll, StringBuiltinsAssembler) {
    1403             :   char const* method_name = "String.prototype.matchAll";
    1404             : 
    1405          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1406          56 :   TNode<Object> maybe_regexp = CAST(Parameter(Descriptor::kRegexp));
    1407          56 :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
    1408          56 :   TNode<Context> native_context = LoadNativeContext(context);
    1409             : 
    1410             :   // 1. Let O be ? RequireObjectCoercible(this value).
    1411          56 :   RequireObjectCoercible(context, receiver, method_name);
    1412             : 
    1413             :   // 2. If regexp is neither undefined nor null, then
    1414             :   //   a. Let matcher be ? GetMethod(regexp, @@matchAll).
    1415             :   //   b. If matcher is not undefined, then
    1416             :   //     i. Return ? Call(matcher, regexp, « O »).
    1417          56 :   auto if_regexp_call = [&] {
    1418             :     // MaybeCallFunctionAtSymbol guarantees fast path is chosen only if
    1419             :     // maybe_regexp is a fast regexp and receiver is a string.
    1420          56 :     TNode<String> s = CAST(receiver);
    1421             : 
    1422             :     RegExpMatchAllAssembler regexp_asm(state());
    1423          56 :     regexp_asm.Generate(context, native_context, maybe_regexp, s);
    1424          56 :   };
    1425          56 :   auto if_generic_call = [=](Node* fn) {
    1426         168 :     Callable call_callable = CodeFactory::Call(isolate());
    1427         112 :     Return(CallJS(call_callable, context, fn, maybe_regexp, receiver));
    1428         112 :   };
    1429         224 :   MaybeCallFunctionAtSymbol(
    1430             :       context, maybe_regexp, receiver, isolate()->factory()->match_all_symbol(),
    1431             :       DescriptorIndexAndName{JSRegExp::kSymbolMatchAllFunctionDescriptorIndex,
    1432             :                              RootIndex::kmatch_all_symbol},
    1433          56 :       if_regexp_call, if_generic_call);
    1434             : 
    1435             :   RegExpMatchAllAssembler regexp_asm(state());
    1436             : 
    1437             :   // 3. Let S be ? ToString(O).
    1438          56 :   TNode<String> s = ToString_Inline(context, receiver);
    1439             : 
    1440             :   // 4. Let rx be ? RegExpCreate(R, "g").
    1441             :   TNode<Object> rx = regexp_asm.RegExpCreate(context, native_context,
    1442          56 :                                              maybe_regexp, StringConstant("g"));
    1443             : 
    1444             :   // 5. Return ? Invoke(rx, @@matchAll, « S »).
    1445          56 :   Callable callable = CodeFactory::Call(isolate());
    1446             :   TNode<Object> match_all_func =
    1447         112 :       GetProperty(context, rx, isolate()->factory()->match_all_symbol());
    1448         112 :   Return(CallJS(callable, context, match_all_func, rx, s));
    1449          56 : }
    1450             : 
    1451         112 : class StringPadAssembler : public StringBuiltinsAssembler {
    1452             :  public:
    1453             :   explicit StringPadAssembler(compiler::CodeAssemblerState* state)
    1454             :       : StringBuiltinsAssembler(state) {}
    1455             : 
    1456             :  protected:
    1457             :   enum Variant { kStart, kEnd };
    1458             : 
    1459         112 :   void Generate(Variant variant, const char* method_name, TNode<IntPtrT> argc,
    1460             :                 TNode<Context> context) {
    1461         112 :     CodeStubArguments arguments(this, argc);
    1462         112 :     TNode<Object> receiver = arguments.GetReceiver();
    1463             :     TNode<String> receiver_string =
    1464             :         ToThisString(context, receiver, method_name);
    1465         112 :     TNode<Smi> const string_length = LoadStringLengthAsSmi(receiver_string);
    1466             : 
    1467         112 :     TVARIABLE(String, var_fill_string, StringConstant(" "));
    1468         112 :     TVARIABLE(IntPtrT, var_fill_length, IntPtrConstant(1));
    1469             : 
    1470         112 :     Label check_fill(this), dont_pad(this), invalid_string_length(this),
    1471         112 :         pad(this);
    1472             : 
    1473             :     // If no max_length was provided, return the string.
    1474         336 :     GotoIf(IntPtrEqual(argc, IntPtrConstant(0)), &dont_pad);
    1475             : 
    1476             :     TNode<Number> const max_length =
    1477         224 :         ToLength_Inline(context, arguments.AtIndex(0));
    1478             :     CSA_ASSERT(this, IsNumberNormalized(max_length));
    1479             : 
    1480             :     // If max_length <= string_length, return the string.
    1481         224 :     GotoIfNot(TaggedIsSmi(max_length), &check_fill);
    1482         224 :     Branch(SmiLessThanOrEqual(CAST(max_length), string_length), &dont_pad,
    1483         112 :            &check_fill);
    1484             : 
    1485         112 :     BIND(&check_fill);
    1486             :     {
    1487         336 :       GotoIf(IntPtrEqual(argc, IntPtrConstant(1)), &pad);
    1488         224 :       Node* const fill = arguments.AtIndex(1);
    1489         224 :       GotoIf(IsUndefined(fill), &pad);
    1490             : 
    1491         224 :       var_fill_string = ToString_Inline(context, fill);
    1492         224 :       var_fill_length = LoadStringLengthAsWord(var_fill_string.value());
    1493         336 :       Branch(WordEqual(var_fill_length.value(), IntPtrConstant(0)), &dont_pad,
    1494         112 :              &pad);
    1495             :     }
    1496             : 
    1497         112 :     BIND(&pad);
    1498             :     {
    1499             :       CSA_ASSERT(this,
    1500             :                  IntPtrGreaterThan(var_fill_length.value(), IntPtrConstant(0)));
    1501             : 
    1502             :       // Throw if max_length is greater than String::kMaxLength.
    1503         224 :       GotoIfNot(TaggedIsSmi(max_length), &invalid_string_length);
    1504         112 :       TNode<Smi> smi_max_length = CAST(max_length);
    1505         112 :       GotoIfNot(
    1506         224 :           SmiLessThanOrEqual(smi_max_length, SmiConstant(String::kMaxLength)),
    1507         112 :           &invalid_string_length);
    1508             : 
    1509             :       CSA_ASSERT(this, SmiGreaterThan(smi_max_length, string_length));
    1510         112 :       TNode<Smi> const pad_length = SmiSub(smi_max_length, string_length);
    1511             : 
    1512         224 :       VARIABLE(var_pad, MachineRepresentation::kTagged);
    1513         112 :       Label single_char_fill(this), multi_char_fill(this), return_result(this);
    1514         336 :       Branch(IntPtrEqual(var_fill_length.value(), IntPtrConstant(1)),
    1515         112 :              &single_char_fill, &multi_char_fill);
    1516             : 
    1517             :       // Fast path for a single character fill.  No need to calculate number of
    1518             :       // repetitions or remainder.
    1519         112 :       BIND(&single_char_fill);
    1520             :       {
    1521         224 :         var_pad.Bind(CallBuiltin(Builtins::kStringRepeat, context,
    1522             :                                  static_cast<Node*>(var_fill_string.value()),
    1523         224 :                                  pad_length));
    1524         112 :         Goto(&return_result);
    1525             :       }
    1526         112 :       BIND(&multi_char_fill);
    1527             :       {
    1528             :         TNode<Int32T> const fill_length_word32 =
    1529         112 :             TruncateIntPtrToInt32(var_fill_length.value());
    1530         112 :         TNode<Int32T> const pad_length_word32 = SmiToInt32(pad_length);
    1531             :         TNode<Int32T> const repetitions_word32 =
    1532         112 :             Int32Div(pad_length_word32, fill_length_word32);
    1533             :         TNode<Int32T> const remaining_word32 =
    1534         112 :             Int32Mod(pad_length_word32, fill_length_word32);
    1535             : 
    1536         224 :         var_pad.Bind(CallBuiltin(Builtins::kStringRepeat, context,
    1537             :                                  var_fill_string.value(),
    1538         336 :                                  SmiFromInt32(repetitions_word32)));
    1539             : 
    1540         112 :         GotoIfNot(remaining_word32, &return_result);
    1541             :         {
    1542         224 :           Node* const remainder_string = CallBuiltin(
    1543             :               Builtins::kStringSubstring, context, var_fill_string.value(),
    1544         224 :               IntPtrConstant(0), ChangeInt32ToIntPtr(remaining_word32));
    1545         224 :           var_pad.Bind(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
    1546         336 :                                    var_pad.value(), remainder_string));
    1547         112 :           Goto(&return_result);
    1548             :         }
    1549             :       }
    1550         112 :       BIND(&return_result);
    1551             :       CSA_ASSERT(this,
    1552             :                  SmiEqual(pad_length, LoadStringLengthAsSmi(var_pad.value())));
    1553         336 :       arguments.PopAndReturn(
    1554             :           variant == kStart
    1555             :               ? CallBuiltin(Builtins::kStringAdd_CheckNone, context,
    1556          56 :                             var_pad.value(), receiver_string)
    1557             :               : CallBuiltin(Builtins::kStringAdd_CheckNone, context,
    1558         168 :                             receiver_string, var_pad.value()));
    1559             :     }
    1560         112 :     BIND(&dont_pad);
    1561         112 :     arguments.PopAndReturn(receiver_string);
    1562         112 :     BIND(&invalid_string_length);
    1563             :     {
    1564             :       CallRuntime(Runtime::kThrowInvalidStringLength, context);
    1565         112 :       Unreachable();
    1566             :     }
    1567         112 :   }
    1568             : };
    1569             : 
    1570         280 : TF_BUILTIN(StringPrototypePadEnd, StringPadAssembler) {
    1571             :   TNode<IntPtrT> argc =
    1572          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    1573          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1574             : 
    1575          56 :   Generate(kEnd, "String.prototype.padEnd", argc, context);
    1576          56 : }
    1577             : 
    1578         280 : TF_BUILTIN(StringPrototypePadStart, StringPadAssembler) {
    1579             :   TNode<IntPtrT> argc =
    1580          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    1581          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1582             : 
    1583          56 :   Generate(kStart, "String.prototype.padStart", argc, context);
    1584          56 : }
    1585             : 
    1586             : // ES6 #sec-string.prototype.search
    1587         336 : TF_BUILTIN(StringPrototypeSearch, StringMatchSearchAssembler) {
    1588          56 :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
    1589          56 :   TNode<Object> maybe_regexp = CAST(Parameter(Descriptor::kRegexp));
    1590          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1591          56 :   Generate(kSearch, "String.prototype.search", receiver, maybe_regexp, context);
    1592          56 : }
    1593             : 
    1594             : // ES6 section 21.1.3.18 String.prototype.slice ( start, end )
    1595         336 : TF_BUILTIN(StringPrototypeSlice, StringBuiltinsAssembler) {
    1596         112 :   Label out(this);
    1597             :   TVARIABLE(IntPtrT, var_start);
    1598             :   TVARIABLE(IntPtrT, var_end);
    1599             : 
    1600             :   const int kStart = 0;
    1601             :   const int kEnd = 1;
    1602             :   Node* argc =
    1603         112 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    1604          56 :   CodeStubArguments args(this, argc);
    1605         112 :   Node* const receiver = args.GetReceiver();
    1606          56 :   TNode<Object> start = args.GetOptionalArgumentValue(kStart);
    1607          56 :   TNode<Object> end = args.GetOptionalArgumentValue(kEnd);
    1608             :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1609             : 
    1610             :   // 1. Let O be ? RequireObjectCoercible(this value).
    1611          56 :   RequireObjectCoercible(context, receiver, "String.prototype.slice");
    1612             : 
    1613             :   // 2. Let S be ? ToString(O).
    1614             :   TNode<String> const subject_string =
    1615          56 :       CAST(CallBuiltin(Builtins::kToString, context, receiver));
    1616             : 
    1617             :   // 3. Let len be the number of elements in S.
    1618          56 :   TNode<IntPtrT> const length = LoadStringLengthAsWord(subject_string);
    1619             : 
    1620             :   // Convert {start} to a relative index.
    1621          56 :   var_start = ConvertToRelativeIndex(context, start, length);
    1622             : 
    1623             :   // 5. If end is undefined, let intEnd be len;
    1624             :   var_end = length;
    1625         112 :   GotoIf(IsUndefined(end), &out);
    1626             : 
    1627             :   // Convert {end} to a relative index.
    1628          56 :   var_end = ConvertToRelativeIndex(context, end, length);
    1629          56 :   Goto(&out);
    1630             : 
    1631          56 :   Label return_emptystring(this);
    1632          56 :   BIND(&out);
    1633             :   {
    1634         112 :     GotoIf(IntPtrLessThanOrEqual(var_end.value(), var_start.value()),
    1635          56 :            &return_emptystring);
    1636             :     TNode<String> const result =
    1637          56 :         SubString(subject_string, var_start.value(), var_end.value());
    1638          56 :     args.PopAndReturn(result);
    1639             :   }
    1640             : 
    1641          56 :   BIND(&return_emptystring);
    1642         112 :   args.PopAndReturn(EmptyStringConstant());
    1643          56 : }
    1644             : 
    1645          56 : TNode<JSArray> StringBuiltinsAssembler::StringToArray(
    1646             :     TNode<Context> context, TNode<String> subject_string,
    1647             :     TNode<Smi> subject_length, TNode<Number> limit_number) {
    1648             :   CSA_ASSERT(this, SmiGreaterThan(subject_length, SmiConstant(0)));
    1649             : 
    1650         112 :   Label done(this), call_runtime(this, Label::kDeferred),
    1651          56 :       fill_thehole_and_call_runtime(this, Label::kDeferred);
    1652             :   TVARIABLE(JSArray, result_array);
    1653             : 
    1654          56 :   TNode<Int32T> instance_type = LoadInstanceType(subject_string);
    1655         112 :   GotoIfNot(IsOneByteStringInstanceType(instance_type), &call_runtime);
    1656             : 
    1657             :   // Try to use cached one byte characters.
    1658             :   {
    1659             :     TNode<Smi> length_smi =
    1660         112 :         Select<Smi>(TaggedIsSmi(limit_number),
    1661          56 :                     [=] { return SmiMin(CAST(limit_number), subject_length); },
    1662         224 :                     [=] { return subject_length; });
    1663             :     TNode<IntPtrT> length = SmiToIntPtr(length_smi);
    1664             : 
    1665         112 :     ToDirectStringAssembler to_direct(state(), subject_string);
    1666          56 :     to_direct.TryToDirect(&call_runtime);
    1667          56 :     TNode<FixedArray> elements = CAST(AllocateFixedArray(
    1668             :         PACKED_ELEMENTS, length, AllocationFlag::kAllowLargeObjectAllocation));
    1669             :     // Don't allocate anything while {string_data} is live!
    1670             :     TNode<RawPtrT> string_data = UncheckedCast<RawPtrT>(
    1671          56 :         to_direct.PointerToData(&fill_thehole_and_call_runtime));
    1672          56 :     TNode<IntPtrT> string_data_offset = to_direct.offset();
    1673          56 :     TNode<Object> cache = LoadRoot(RootIndex::kSingleCharacterStringCache);
    1674             : 
    1675          56 :     BuildFastLoop(
    1676         112 :         IntPtrConstant(0), length,
    1677          56 :         [&](Node* index) {
    1678             :           // TODO(jkummerow): Implement a CSA version of DisallowHeapAllocation
    1679             :           // and use that to guard ToDirectStringAssembler.PointerToData().
    1680             :           CSA_ASSERT(this, WordEqual(to_direct.PointerToData(&call_runtime),
    1681             :                                      string_data));
    1682             :           TNode<Int32T> char_code =
    1683          56 :               UncheckedCast<Int32T>(Load(MachineType::Uint8(), string_data,
    1684         392 :                                          IntPtrAdd(index, string_data_offset)));
    1685         112 :           Node* code_index = ChangeUint32ToWord(char_code);
    1686         112 :           TNode<Object> entry = LoadFixedArrayElement(CAST(cache), code_index);
    1687             : 
    1688             :           // If we cannot find a char in the cache, fill the hole for the fixed
    1689             :           // array, and call runtime.
    1690         168 :           GotoIf(IsUndefined(entry), &fill_thehole_and_call_runtime);
    1691             : 
    1692         112 :           StoreFixedArrayElement(elements, index, entry);
    1693          56 :         },
    1694          56 :         1, ParameterMode::INTPTR_PARAMETERS, IndexAdvanceMode::kPost);
    1695             : 
    1696          56 :     TNode<Map> array_map = LoadJSArrayElementsMap(PACKED_ELEMENTS, context);
    1697         112 :     result_array = AllocateJSArray(array_map, elements, length_smi);
    1698          56 :     Goto(&done);
    1699             : 
    1700          56 :     BIND(&fill_thehole_and_call_runtime);
    1701             :     {
    1702         112 :       FillFixedArrayWithValue(PACKED_ELEMENTS, elements, IntPtrConstant(0),
    1703          56 :                               length, RootIndex::kTheHoleValue);
    1704          56 :       Goto(&call_runtime);
    1705             :     }
    1706             :   }
    1707             : 
    1708          56 :   BIND(&call_runtime);
    1709             :   {
    1710             :     result_array = CAST(CallRuntime(Runtime::kStringToArray, context,
    1711             :                                     subject_string, limit_number));
    1712          56 :     Goto(&done);
    1713             :   }
    1714             : 
    1715          56 :   BIND(&done);
    1716          56 :   return result_array.value();
    1717             : }
    1718             : 
    1719             : // ES6 section 21.1.3.19 String.prototype.split ( separator, limit )
    1720         280 : TF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
    1721             :   const int kSeparatorArg = 0;
    1722             :   const int kLimitArg = 1;
    1723             : 
    1724             :   Node* const argc =
    1725         112 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    1726          56 :   CodeStubArguments args(this, argc);
    1727             : 
    1728         112 :   Node* const receiver = args.GetReceiver();
    1729         112 :   Node* const separator = args.GetOptionalArgumentValue(kSeparatorArg);
    1730         112 :   Node* const limit = args.GetOptionalArgumentValue(kLimitArg);
    1731          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1732             : 
    1733          56 :   TNode<Smi> smi_zero = SmiConstant(0);
    1734             : 
    1735         112 :   RequireObjectCoercible(context, receiver, "String.prototype.split");
    1736             : 
    1737             :   // Redirect to splitter method if {separator[@@split]} is not undefined.
    1738             : 
    1739         280 :   MaybeCallFunctionAtSymbol(
    1740             :       context, separator, receiver, isolate()->factory()->split_symbol(),
    1741             :       DescriptorIndexAndName{JSRegExp::kSymbolSplitFunctionDescriptorIndex,
    1742             :                              RootIndex::ksplit_symbol},
    1743          56 :       [&]() {
    1744         224 :         args.PopAndReturn(CallBuiltin(Builtins::kRegExpSplit, context,
    1745         224 :                                       separator, receiver, limit));
    1746          56 :       },
    1747          56 :       [&](Node* fn) {
    1748         112 :         Callable call_callable = CodeFactory::Call(isolate());
    1749         224 :         args.PopAndReturn(
    1750         280 :             CallJS(call_callable, context, fn, separator, receiver, limit));
    1751         112 :       });
    1752             : 
    1753             :   // String and integer conversions.
    1754             : 
    1755         112 :   TNode<String> subject_string = ToString_Inline(context, receiver);
    1756             :   TNode<Number> limit_number = Select<Number>(
    1757         168 :       IsUndefined(limit), [=] { return NumberConstant(kMaxUInt32); },
    1758         280 :       [=] { return ToUint32(context, limit); });
    1759         168 :   Node* const separator_string = ToString_Inline(context, separator);
    1760             : 
    1761          56 :   Label return_empty_array(this);
    1762             : 
    1763             :   // Shortcut for {limit} == 0.
    1764          56 :   GotoIf(WordEqual<Object, Object>(limit_number, smi_zero),
    1765          56 :          &return_empty_array);
    1766             : 
    1767             :   // ECMA-262 says that if {separator} is undefined, the result should
    1768             :   // be an array of size 1 containing the entire string.
    1769             :   {
    1770          56 :     Label next(this);
    1771         168 :     GotoIfNot(IsUndefined(separator), &next);
    1772             : 
    1773             :     const ElementsKind kind = PACKED_ELEMENTS;
    1774         112 :     Node* const native_context = LoadNativeContext(context);
    1775          56 :     TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
    1776             : 
    1777          56 :     TNode<Smi> length = SmiConstant(1);
    1778          56 :     TNode<IntPtrT> capacity = IntPtrConstant(1);
    1779             :     TNode<JSArray> result = AllocateJSArray(kind, array_map, capacity, length);
    1780             : 
    1781          56 :     TNode<FixedArray> fixed_array = CAST(LoadElements(result));
    1782          56 :     StoreFixedArrayElement(fixed_array, 0, subject_string);
    1783             : 
    1784          56 :     args.PopAndReturn(result);
    1785             : 
    1786          56 :     BIND(&next);
    1787             :   }
    1788             : 
    1789             :   // If the separator string is empty then return the elements in the subject.
    1790             :   {
    1791          56 :     Label next(this);
    1792         112 :     GotoIfNot(SmiEqual(LoadStringLengthAsSmi(separator_string), smi_zero),
    1793          56 :               &next);
    1794             : 
    1795          56 :     TNode<Smi> subject_length = LoadStringLengthAsSmi(subject_string);
    1796         112 :     GotoIf(SmiEqual(subject_length, smi_zero), &return_empty_array);
    1797             : 
    1798             :     args.PopAndReturn(
    1799         112 :         StringToArray(context, subject_string, subject_length, limit_number));
    1800             : 
    1801          56 :     BIND(&next);
    1802             :   }
    1803             : 
    1804             :   Node* const result =
    1805             :       CallRuntime(Runtime::kStringSplit, context, subject_string,
    1806             :                   separator_string, limit_number);
    1807          56 :   args.PopAndReturn(result);
    1808             : 
    1809          56 :   BIND(&return_empty_array);
    1810             :   {
    1811             :     const ElementsKind kind = PACKED_ELEMENTS;
    1812         112 :     Node* const native_context = LoadNativeContext(context);
    1813          56 :     TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
    1814             : 
    1815             :     TNode<Smi> length = smi_zero;
    1816          56 :     TNode<IntPtrT> capacity = IntPtrConstant(0);
    1817             :     TNode<JSArray> result = AllocateJSArray(kind, array_map, capacity, length);
    1818             : 
    1819          56 :     args.PopAndReturn(result);
    1820             :   }
    1821          56 : }
    1822             : 
    1823             : // ES6 #sec-string.prototype.substr
    1824         280 : TF_BUILTIN(StringPrototypeSubstr, StringBuiltinsAssembler) {
    1825             :   const int kStartArg = 0;
    1826             :   const int kLengthArg = 1;
    1827             : 
    1828             :   Node* const argc =
    1829         112 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    1830          56 :   CodeStubArguments args(this, argc);
    1831             : 
    1832          56 :   TNode<Object> receiver = args.GetReceiver();
    1833          56 :   TNode<Object> start = args.GetOptionalArgumentValue(kStartArg);
    1834          56 :   TNode<Object> length = args.GetOptionalArgumentValue(kLengthArg);
    1835             :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1836             : 
    1837          56 :   Label out(this);
    1838             : 
    1839             :   TVARIABLE(IntPtrT, var_start);
    1840             :   TVARIABLE(Number, var_length);
    1841             : 
    1842          56 :   TNode<IntPtrT> const zero = IntPtrConstant(0);
    1843             : 
    1844             :   // Check that {receiver} is coercible to Object and convert it to a String.
    1845             :   TNode<String> const string =
    1846             :       ToThisString(context, receiver, "String.prototype.substr");
    1847             : 
    1848          56 :   TNode<IntPtrT> const string_length = LoadStringLengthAsWord(string);
    1849             : 
    1850             :   // Convert {start} to a relative index.
    1851          56 :   var_start = ConvertToRelativeIndex(context, start, string_length);
    1852             : 
    1853             :   // Conversions and bounds-checks for {length}.
    1854          56 :   Label if_issmi(this), if_isheapnumber(this, Label::kDeferred);
    1855             : 
    1856             :   // Default to {string_length} if {length} is undefined.
    1857             :   {
    1858          56 :     Label if_isundefined(this, Label::kDeferred), if_isnotundefined(this);
    1859         112 :     Branch(IsUndefined(length), &if_isundefined, &if_isnotundefined);
    1860             : 
    1861          56 :     BIND(&if_isundefined);
    1862         112 :     var_length = SmiTag(string_length);
    1863          56 :     Goto(&if_issmi);
    1864             : 
    1865          56 :     BIND(&if_isnotundefined);
    1866         112 :     var_length = ToInteger_Inline(context, length,
    1867             :                                   CodeStubAssembler::kTruncateMinusZero);
    1868             :   }
    1869             : 
    1870             :   TVARIABLE(IntPtrT, var_result_length);
    1871             : 
    1872         112 :   Branch(TaggedIsSmi(var_length.value()), &if_issmi, &if_isheapnumber);
    1873             : 
    1874             :   // Set {length} to min(max({length}, 0), {string_length} - {start}
    1875          56 :   BIND(&if_issmi);
    1876             :   {
    1877             :     TNode<IntPtrT> const positive_length =
    1878         112 :         IntPtrMax(SmiUntag(CAST(var_length.value())), zero);
    1879             :     TNode<IntPtrT> const minimal_length =
    1880             :         IntPtrSub(string_length, var_start.value());
    1881         112 :     var_result_length = IntPtrMin(positive_length, minimal_length);
    1882             : 
    1883         112 :     GotoIfNot(IntPtrLessThanOrEqual(var_result_length.value(), zero), &out);
    1884         112 :     args.PopAndReturn(EmptyStringConstant());
    1885             :   }
    1886             : 
    1887          56 :   BIND(&if_isheapnumber);
    1888             :   {
    1889             :     // If {length} is a heap number, it is definitely out of bounds. There are
    1890             :     // two cases according to the spec: if it is negative, "" is returned; if
    1891             :     // it is positive, then length is set to {string_length} - {start}.
    1892             : 
    1893             :     CSA_ASSERT(this, IsHeapNumber(CAST(var_length.value())));
    1894             : 
    1895          56 :     Label if_isnegative(this), if_ispositive(this);
    1896          56 :     TNode<Float64T> const float_zero = Float64Constant(0.);
    1897             :     TNode<Float64T> const length_float =
    1898          56 :         LoadHeapNumberValue(CAST(var_length.value()));
    1899         112 :     Branch(Float64LessThan(length_float, float_zero), &if_isnegative,
    1900          56 :            &if_ispositive);
    1901             : 
    1902          56 :     BIND(&if_isnegative);
    1903         112 :     args.PopAndReturn(EmptyStringConstant());
    1904             : 
    1905          56 :     BIND(&if_ispositive);
    1906             :     {
    1907             :       var_result_length = IntPtrSub(string_length, var_start.value());
    1908         112 :       GotoIfNot(IntPtrLessThanOrEqual(var_result_length.value(), zero), &out);
    1909         112 :       args.PopAndReturn(EmptyStringConstant());
    1910             :     }
    1911             :   }
    1912             : 
    1913          56 :   BIND(&out);
    1914             :   {
    1915             :     TNode<IntPtrT> const end =
    1916          56 :         IntPtrAdd(var_start.value(), var_result_length.value());
    1917         112 :     args.PopAndReturn(SubString(string, var_start.value(), end));
    1918             :   }
    1919          56 : }
    1920             : 
    1921         112 : TNode<Smi> StringBuiltinsAssembler::ToSmiBetweenZeroAnd(
    1922             :     SloppyTNode<Context> context, SloppyTNode<Object> value,
    1923             :     SloppyTNode<Smi> limit) {
    1924         224 :   Label out(this);
    1925             :   TVARIABLE(Smi, var_result);
    1926             : 
    1927             :   TNode<Number> const value_int =
    1928         112 :       ToInteger_Inline(context, value, CodeStubAssembler::kTruncateMinusZero);
    1929             : 
    1930         112 :   Label if_issmi(this), if_isnotsmi(this, Label::kDeferred);
    1931         224 :   Branch(TaggedIsSmi(value_int), &if_issmi, &if_isnotsmi);
    1932             : 
    1933         112 :   BIND(&if_issmi);
    1934             :   {
    1935         112 :     TNode<Smi> value_smi = CAST(value_int);
    1936         112 :     Label if_isinbounds(this), if_isoutofbounds(this, Label::kDeferred);
    1937         224 :     Branch(SmiAbove(value_smi, limit), &if_isoutofbounds, &if_isinbounds);
    1938             : 
    1939         112 :     BIND(&if_isinbounds);
    1940             :     {
    1941             :       var_result = CAST(value_int);
    1942         112 :       Goto(&out);
    1943             :     }
    1944             : 
    1945         112 :     BIND(&if_isoutofbounds);
    1946             :     {
    1947         112 :       TNode<Smi> const zero = SmiConstant(0);
    1948         112 :       var_result =
    1949             :           SelectConstant<Smi>(SmiLessThan(value_smi, zero), zero, limit);
    1950         112 :       Goto(&out);
    1951             :     }
    1952             :   }
    1953             : 
    1954         112 :   BIND(&if_isnotsmi);
    1955             :   {
    1956             :     // {value} is a heap number - in this case, it is definitely out of bounds.
    1957             :     TNode<HeapNumber> value_int_hn = CAST(value_int);
    1958             : 
    1959         112 :     TNode<Float64T> const float_zero = Float64Constant(0.);
    1960         112 :     TNode<Smi> const smi_zero = SmiConstant(0);
    1961         112 :     TNode<Float64T> const value_float = LoadHeapNumberValue(value_int_hn);
    1962         224 :     var_result = SelectConstant<Smi>(Float64LessThan(value_float, float_zero),
    1963             :                                      smi_zero, limit);
    1964         112 :     Goto(&out);
    1965             :   }
    1966             : 
    1967         112 :   BIND(&out);
    1968         112 :   return var_result.value();
    1969             : }
    1970             : 
    1971         280 : TF_BUILTIN(StringSubstring, CodeStubAssembler) {
    1972          56 :   TNode<String> string = CAST(Parameter(Descriptor::kString));
    1973          56 :   TNode<IntPtrT> from = UncheckedCast<IntPtrT>(Parameter(Descriptor::kFrom));
    1974          56 :   TNode<IntPtrT> to = UncheckedCast<IntPtrT>(Parameter(Descriptor::kTo));
    1975             : 
    1976         112 :   Return(SubString(string, from, to));
    1977          56 : }
    1978             : 
    1979             : // ES6 #sec-string.prototype.substring
    1980         280 : TF_BUILTIN(StringPrototypeSubstring, StringBuiltinsAssembler) {
    1981             :   const int kStartArg = 0;
    1982             :   const int kEndArg = 1;
    1983             : 
    1984             :   Node* const argc =
    1985         112 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    1986          56 :   CodeStubArguments args(this, argc);
    1987             : 
    1988          56 :   TNode<Object> receiver = args.GetReceiver();
    1989          56 :   TNode<Object> start = args.GetOptionalArgumentValue(kStartArg);
    1990          56 :   TNode<Object> end = args.GetOptionalArgumentValue(kEndArg);
    1991             :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    1992             : 
    1993          56 :   Label out(this);
    1994             : 
    1995             :   TVARIABLE(Smi, var_start);
    1996             :   TVARIABLE(Smi, var_end);
    1997             : 
    1998             :   // Check that {receiver} is coercible to Object and convert it to a String.
    1999             :   TNode<String> const string =
    2000             :       ToThisString(context, receiver, "String.prototype.substring");
    2001             : 
    2002          56 :   TNode<Smi> const length = LoadStringLengthAsSmi(string);
    2003             : 
    2004             :   // Conversion and bounds-checks for {start}.
    2005         112 :   var_start = ToSmiBetweenZeroAnd(context, start, length);
    2006             : 
    2007             :   // Conversion and bounds-checks for {end}.
    2008             :   {
    2009             :     var_end = length;
    2010         112 :     GotoIf(IsUndefined(end), &out);
    2011             : 
    2012         112 :     var_end = ToSmiBetweenZeroAnd(context, end, length);
    2013             : 
    2014          56 :     Label if_endislessthanstart(this);
    2015         112 :     Branch(SmiLessThan(var_end.value(), var_start.value()),
    2016          56 :            &if_endislessthanstart, &out);
    2017             : 
    2018          56 :     BIND(&if_endislessthanstart);
    2019             :     {
    2020             :       TNode<Smi> const tmp = var_end.value();
    2021             :       var_end = var_start.value();
    2022             :       var_start = tmp;
    2023          56 :       Goto(&out);
    2024             :     }
    2025             :   }
    2026             : 
    2027          56 :   BIND(&out);
    2028             :   {
    2029         112 :     args.PopAndReturn(SubString(string, SmiUntag(var_start.value()),
    2030         224 :                                 SmiUntag(var_end.value())));
    2031             :   }
    2032          56 : }
    2033             : 
    2034             : // ES6 #sec-string.prototype.trim
    2035         280 : TF_BUILTIN(StringPrototypeTrim, StringTrimAssembler) {
    2036             :   TNode<IntPtrT> argc =
    2037          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    2038          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    2039             : 
    2040          56 :   Generate(String::kTrim, "String.prototype.trim", argc, context);
    2041          56 : }
    2042             : 
    2043             : // https://github.com/tc39/proposal-string-left-right-trim
    2044         280 : TF_BUILTIN(StringPrototypeTrimStart, StringTrimAssembler) {
    2045             :   TNode<IntPtrT> argc =
    2046          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    2047          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    2048             : 
    2049          56 :   Generate(String::kTrimStart, "String.prototype.trimLeft", argc, context);
    2050          56 : }
    2051             : 
    2052             : // https://github.com/tc39/proposal-string-left-right-trim
    2053         280 : TF_BUILTIN(StringPrototypeTrimEnd, StringTrimAssembler) {
    2054             :   TNode<IntPtrT> argc =
    2055          56 :       ChangeInt32ToIntPtr(Parameter(Descriptor::kJSActualArgumentsCount));
    2056          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    2057             : 
    2058          56 :   Generate(String::kTrimEnd, "String.prototype.trimRight", argc, context);
    2059          56 : }
    2060             : 
    2061         168 : void StringTrimAssembler::Generate(String::TrimMode mode,
    2062             :                                    const char* method_name, TNode<IntPtrT> argc,
    2063             :                                    TNode<Context> context) {
    2064         336 :   Label return_emptystring(this), if_runtime(this);
    2065             : 
    2066         168 :   CodeStubArguments arguments(this, argc);
    2067         168 :   TNode<Object> receiver = arguments.GetReceiver();
    2068             : 
    2069             :   // Check that {receiver} is coercible to Object and convert it to a String.
    2070             :   TNode<String> const string = ToThisString(context, receiver, method_name);
    2071         168 :   TNode<IntPtrT> const string_length = LoadStringLengthAsWord(string);
    2072             : 
    2073         336 :   ToDirectStringAssembler to_direct(state(), string);
    2074         168 :   to_direct.TryToDirect(&if_runtime);
    2075             :   Node* const string_data = to_direct.PointerToData(&if_runtime);
    2076             :   Node* const instance_type = to_direct.instance_type();
    2077         336 :   Node* const is_stringonebyte = IsOneByteStringInstanceType(instance_type);
    2078             :   Node* const string_data_offset = to_direct.offset();
    2079             : 
    2080         168 :   TVARIABLE(IntPtrT, var_start, IntPtrConstant(0));
    2081         168 :   TVARIABLE(IntPtrT, var_end, IntPtrSub(string_length, IntPtrConstant(1)));
    2082             : 
    2083         168 :   if (mode == String::kTrimStart || mode == String::kTrim) {
    2084             :     ScanForNonWhiteSpaceOrLineTerminator(string_data, string_data_offset,
    2085             :                                          is_stringonebyte, &var_start,
    2086         112 :                                          string_length, 1, &return_emptystring);
    2087             :   }
    2088         168 :   if (mode == String::kTrimEnd || mode == String::kTrim) {
    2089             :     ScanForNonWhiteSpaceOrLineTerminator(
    2090             :         string_data, string_data_offset, is_stringonebyte, &var_end,
    2091         224 :         IntPtrConstant(-1), -1, &return_emptystring);
    2092             :   }
    2093             : 
    2094             :   arguments.PopAndReturn(
    2095         336 :       SubString(string, var_start.value(),
    2096         336 :                 IntPtrAdd(var_end.value(), IntPtrConstant(1))));
    2097             : 
    2098         168 :   BIND(&if_runtime);
    2099             :   arguments.PopAndReturn(
    2100         168 :       CallRuntime(Runtime::kStringTrim, context, string, SmiConstant(mode)));
    2101             : 
    2102         168 :   BIND(&return_emptystring);
    2103         336 :   arguments.PopAndReturn(EmptyStringConstant());
    2104         168 : }
    2105             : 
    2106         224 : void StringTrimAssembler::ScanForNonWhiteSpaceOrLineTerminator(
    2107             :     Node* const string_data, Node* const string_data_offset,
    2108             :     Node* const is_stringonebyte, Variable* const var_index, Node* const end,
    2109             :     int increment, Label* const if_none_found) {
    2110         448 :   Label if_stringisonebyte(this), out(this);
    2111             : 
    2112         224 :   GotoIf(is_stringonebyte, &if_stringisonebyte);
    2113             : 
    2114             :   // Two Byte String
    2115         224 :   BuildLoop(
    2116         224 :       var_index, end, increment, if_none_found, &out, [&](Node* const index) {
    2117         448 :         return Load(
    2118         224 :             MachineType::Uint16(), string_data,
    2119        1344 :             WordShl(IntPtrAdd(index, string_data_offset), IntPtrConstant(1)));
    2120         672 :       });
    2121             : 
    2122         224 :   BIND(&if_stringisonebyte);
    2123         224 :   BuildLoop(var_index, end, increment, if_none_found, &out,
    2124         224 :             [&](Node* const index) {
    2125         448 :               return Load(MachineType::Uint8(), string_data,
    2126         896 :                           IntPtrAdd(index, string_data_offset));
    2127         672 :             });
    2128             : 
    2129         224 :   BIND(&out);
    2130         224 : }
    2131             : 
    2132         448 : void StringTrimAssembler::BuildLoop(
    2133             :     Variable* const var_index, Node* const end, int increment,
    2134             :     Label* const if_none_found, Label* const out,
    2135             :     const std::function<Node*(Node*)>& get_character) {
    2136         896 :   Label loop(this, var_index);
    2137         448 :   Goto(&loop);
    2138         448 :   BIND(&loop);
    2139             :   {
    2140         448 :     Node* const index = var_index->value();
    2141         896 :     GotoIf(IntPtrEqual(index, end), if_none_found);
    2142             :     GotoIfNotWhiteSpaceOrLineTerminator(
    2143         448 :         UncheckedCast<Uint32T>(get_character(index)), out);
    2144         448 :     Increment(var_index, increment);
    2145         448 :     Goto(&loop);
    2146             :   }
    2147         448 : }
    2148             : 
    2149         452 : void StringTrimAssembler::GotoIfNotWhiteSpaceOrLineTerminator(
    2150             :     Node* const char_code, Label* const if_not_whitespace) {
    2151         904 :   Label out(this);
    2152             : 
    2153             :   // 0x0020 - SPACE (Intentionally out of order to fast path a commmon case)
    2154        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x0020)), &out);
    2155             : 
    2156             :   // 0x0009 - HORIZONTAL TAB
    2157        1356 :   GotoIf(Uint32LessThan(char_code, Int32Constant(0x0009)), if_not_whitespace);
    2158             :   // 0x000A - LINE FEED OR NEW LINE
    2159             :   // 0x000B - VERTICAL TAB
    2160             :   // 0x000C - FORMFEED
    2161             :   // 0x000D - HORIZONTAL TAB
    2162        1356 :   GotoIf(Uint32LessThanOrEqual(char_code, Int32Constant(0x000D)), &out);
    2163             : 
    2164             :   // Common Non-whitespace characters
    2165        1356 :   GotoIf(Uint32LessThan(char_code, Int32Constant(0x00A0)), if_not_whitespace);
    2166             : 
    2167             :   // 0x00A0 - NO-BREAK SPACE
    2168        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x00A0)), &out);
    2169             : 
    2170             :   // 0x1680 - Ogham Space Mark
    2171        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x1680)), &out);
    2172             : 
    2173             :   // 0x2000 - EN QUAD
    2174        1356 :   GotoIf(Uint32LessThan(char_code, Int32Constant(0x2000)), if_not_whitespace);
    2175             :   // 0x2001 - EM QUAD
    2176             :   // 0x2002 - EN SPACE
    2177             :   // 0x2003 - EM SPACE
    2178             :   // 0x2004 - THREE-PER-EM SPACE
    2179             :   // 0x2005 - FOUR-PER-EM SPACE
    2180             :   // 0x2006 - SIX-PER-EM SPACE
    2181             :   // 0x2007 - FIGURE SPACE
    2182             :   // 0x2008 - PUNCTUATION SPACE
    2183             :   // 0x2009 - THIN SPACE
    2184             :   // 0x200A - HAIR SPACE
    2185        1356 :   GotoIf(Uint32LessThanOrEqual(char_code, Int32Constant(0x200A)), &out);
    2186             : 
    2187             :   // 0x2028 - LINE SEPARATOR
    2188        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x2028)), &out);
    2189             :   // 0x2029 - PARAGRAPH SEPARATOR
    2190        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x2029)), &out);
    2191             :   // 0x202F - NARROW NO-BREAK SPACE
    2192        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x202F)), &out);
    2193             :   // 0x205F - MEDIUM MATHEMATICAL SPACE
    2194        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0x205F)), &out);
    2195             :   // 0xFEFF - BYTE ORDER MARK
    2196        1356 :   GotoIf(Word32Equal(char_code, Int32Constant(0xFEFF)), &out);
    2197             :   // 0x3000 - IDEOGRAPHIC SPACE
    2198        1356 :   Branch(Word32Equal(char_code, Int32Constant(0x3000)), &out,
    2199         452 :          if_not_whitespace);
    2200             : 
    2201         452 :   BIND(&out);
    2202         452 : }
    2203             : 
    2204             : // ES6 #sec-string.prototype.tostring
    2205         168 : TF_BUILTIN(StringPrototypeToString, CodeStubAssembler) {
    2206             :   Node* context = Parameter(Descriptor::kContext);
    2207             :   Node* receiver = Parameter(Descriptor::kReceiver);
    2208             : 
    2209          56 :   Node* result = ToThisValue(context, receiver, PrimitiveType::kString,
    2210          56 :                              "String.prototype.toString");
    2211          56 :   Return(result);
    2212          56 : }
    2213             : 
    2214             : // ES6 #sec-string.prototype.valueof
    2215         168 : TF_BUILTIN(StringPrototypeValueOf, CodeStubAssembler) {
    2216             :   Node* context = Parameter(Descriptor::kContext);
    2217             :   Node* receiver = Parameter(Descriptor::kReceiver);
    2218             : 
    2219          56 :   Node* result = ToThisValue(context, receiver, PrimitiveType::kString,
    2220          56 :                              "String.prototype.valueOf");
    2221          56 :   Return(result);
    2222          56 : }
    2223             : 
    2224         168 : TF_BUILTIN(StringPrototypeIterator, CodeStubAssembler) {
    2225             :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    2226             :   TNode<Object> receiver = CAST(Parameter(Descriptor::kReceiver));
    2227             : 
    2228             :   Node* string =
    2229          56 :       ToThisString(context, receiver, "String.prototype[Symbol.iterator]");
    2230             : 
    2231         112 :   Node* native_context = LoadNativeContext(context);
    2232         112 :   Node* map = LoadContextElement(native_context,
    2233          56 :                                  Context::INITIAL_STRING_ITERATOR_MAP_INDEX);
    2234         112 :   Node* iterator = Allocate(JSStringIterator::kSize);
    2235          56 :   StoreMapNoWriteBarrier(iterator, map);
    2236             :   StoreObjectFieldRoot(iterator, JSValue::kPropertiesOrHashOffset,
    2237          56 :                        RootIndex::kEmptyFixedArray);
    2238             :   StoreObjectFieldRoot(iterator, JSObject::kElementsOffset,
    2239          56 :                        RootIndex::kEmptyFixedArray);
    2240             :   StoreObjectFieldNoWriteBarrier(iterator, JSStringIterator::kStringOffset,
    2241          56 :                                  string);
    2242         112 :   Node* index = SmiConstant(0);
    2243             :   StoreObjectFieldNoWriteBarrier(iterator, JSStringIterator::kNextIndexOffset,
    2244          56 :                                  index);
    2245          56 :   Return(iterator);
    2246          56 : }
    2247             : 
    2248             : // Return the |word32| codepoint at {index}. Supports SeqStrings and
    2249             : // ExternalStrings.
    2250         280 : TNode<Int32T> StringBuiltinsAssembler::LoadSurrogatePairAt(
    2251             :     SloppyTNode<String> string, SloppyTNode<IntPtrT> length,
    2252             :     SloppyTNode<IntPtrT> index, UnicodeEncoding encoding) {
    2253         560 :   Label handle_surrogate_pair(this), return_result(this);
    2254             :   TVARIABLE(Int32T, var_result);
    2255             :   TVARIABLE(Int32T, var_trail);
    2256         280 :   var_result = StringCharCodeAt(string, index);
    2257         280 :   var_trail = Int32Constant(0);
    2258             : 
    2259        1400 :   GotoIf(Word32NotEqual(Word32And(var_result.value(), Int32Constant(0xFC00)),
    2260         840 :                         Int32Constant(0xD800)),
    2261         280 :          &return_result);
    2262         280 :   TNode<IntPtrT> next_index = IntPtrAdd(index, IntPtrConstant(1));
    2263             : 
    2264         560 :   GotoIfNot(IntPtrLessThan(next_index, length), &return_result);
    2265         560 :   var_trail = StringCharCodeAt(string, next_index);
    2266        1400 :   Branch(Word32Equal(Word32And(var_trail.value(), Int32Constant(0xFC00)),
    2267         840 :                      Int32Constant(0xDC00)),
    2268         280 :          &handle_surrogate_pair, &return_result);
    2269             : 
    2270         280 :   BIND(&handle_surrogate_pair);
    2271             :   {
    2272             :     TNode<Int32T> lead = var_result.value();
    2273             :     TNode<Int32T> trail = var_trail.value();
    2274             : 
    2275             :     // Check that this path is only taken if a surrogate pair is found
    2276             :     CSA_SLOW_ASSERT(this,
    2277             :                     Uint32GreaterThanOrEqual(lead, Int32Constant(0xD800)));
    2278             :     CSA_SLOW_ASSERT(this, Uint32LessThan(lead, Int32Constant(0xDC00)));
    2279             :     CSA_SLOW_ASSERT(this,
    2280             :                     Uint32GreaterThanOrEqual(trail, Int32Constant(0xDC00)));
    2281             :     CSA_SLOW_ASSERT(this, Uint32LessThan(trail, Int32Constant(0xE000)));
    2282             : 
    2283         280 :     switch (encoding) {
    2284             :       case UnicodeEncoding::UTF16:
    2285         504 :         var_result = Signed(Word32Or(
    2286             : // Need to swap the order for big-endian platforms
    2287             : #if V8_TARGET_BIG_ENDIAN
    2288             :             Word32Shl(lead, Int32Constant(16)), trail));
    2289             : #else
    2290         504 :             Word32Shl(trail, Int32Constant(16)), lead));
    2291             : #endif
    2292         168 :         break;
    2293             : 
    2294             :       case UnicodeEncoding::UTF32: {
    2295             :         // Convert UTF16 surrogate pair into |word32| code point, encoded as
    2296             :         // UTF32.
    2297             :         TNode<Int32T> surrogate_offset =
    2298         112 :             Int32Constant(0x10000 - (0xD800 << 10) - 0xDC00);
    2299             : 
    2300             :         // (lead << 10) + trail + SURROGATE_OFFSET
    2301         448 :         var_result = Signed(Int32Add(Word32Shl(lead, Int32Constant(10)),
    2302             :                                      Int32Add(trail, surrogate_offset)));
    2303             :         break;
    2304             :       }
    2305             :     }
    2306         280 :     Goto(&return_result);
    2307             :   }
    2308             : 
    2309         280 :   BIND(&return_result);
    2310         280 :   return var_result.value();
    2311             : }
    2312             : 
    2313             : // ES6 #sec-%stringiteratorprototype%.next
    2314         280 : TF_BUILTIN(StringIteratorPrototypeNext, StringBuiltinsAssembler) {
    2315         112 :   VARIABLE(var_value, MachineRepresentation::kTagged);
    2316         112 :   VARIABLE(var_done, MachineRepresentation::kTagged);
    2317             : 
    2318         112 :   var_value.Bind(UndefinedConstant());
    2319         112 :   var_done.Bind(TrueConstant());
    2320             : 
    2321          56 :   Label throw_bad_receiver(this), next_codepoint(this), return_result(this);
    2322             : 
    2323             :   Node* context = Parameter(Descriptor::kContext);
    2324             :   Node* iterator = Parameter(Descriptor::kReceiver);
    2325             : 
    2326         112 :   GotoIf(TaggedIsSmi(iterator), &throw_bad_receiver);
    2327          56 :   GotoIfNot(
    2328         168 :       InstanceTypeEqual(LoadInstanceType(iterator), JS_STRING_ITERATOR_TYPE),
    2329          56 :       &throw_bad_receiver);
    2330             : 
    2331             :   Node* string = LoadObjectField(iterator, JSStringIterator::kStringOffset);
    2332             :   TNode<IntPtrT> position = SmiUntag(
    2333          56 :       CAST(LoadObjectField(iterator, JSStringIterator::kNextIndexOffset)));
    2334          56 :   TNode<IntPtrT> length = LoadStringLengthAsWord(string);
    2335             : 
    2336         112 :   Branch(IntPtrLessThan(position, length), &next_codepoint, &return_result);
    2337             : 
    2338          56 :   BIND(&next_codepoint);
    2339             :   {
    2340             :     UnicodeEncoding encoding = UnicodeEncoding::UTF16;
    2341          56 :     TNode<Int32T> ch = LoadSurrogatePairAt(string, length, position, encoding);
    2342          56 :     TNode<String> value = StringFromSingleCodePoint(ch, encoding);
    2343          56 :     var_value.Bind(value);
    2344          56 :     TNode<IntPtrT> length = LoadStringLengthAsWord(value);
    2345             :     StoreObjectFieldNoWriteBarrier(iterator, JSStringIterator::kNextIndexOffset,
    2346         112 :                                    SmiTag(Signed(IntPtrAdd(position, length))));
    2347         112 :     var_done.Bind(FalseConstant());
    2348          56 :     Goto(&return_result);
    2349             :   }
    2350             : 
    2351          56 :   BIND(&return_result);
    2352             :   {
    2353             :     Node* result =
    2354          56 :         AllocateJSIteratorResult(context, var_value.value(), var_done.value());
    2355          56 :     Return(result);
    2356             :   }
    2357             : 
    2358          56 :   BIND(&throw_bad_receiver);
    2359             :   {
    2360             :     // The {receiver} is not a valid JSGeneratorObject.
    2361             :     ThrowTypeError(context, MessageTemplate::kIncompatibleMethodReceiver,
    2362         112 :                    StringConstant("String Iterator.prototype.next"), iterator);
    2363             :   }
    2364          56 : }
    2365             : 
    2366         112 : void StringBuiltinsAssembler::BranchIfStringPrimitiveWithNoCustomIteration(
    2367             :     TNode<Object> object, TNode<Context> context, Label* if_true,
    2368             :     Label* if_false) {
    2369         224 :   GotoIf(TaggedIsSmi(object), if_false);
    2370         224 :   GotoIfNot(IsString(CAST(object)), if_false);
    2371             : 
    2372             :   // Check that the String iterator hasn't been modified in a way that would
    2373             :   // affect iteration.
    2374         224 :   Node* protector_cell = LoadRoot(RootIndex::kStringIteratorProtector);
    2375             :   DCHECK(isolate()->heap()->string_iterator_protector()->IsPropertyCell());
    2376         112 :   Branch(WordEqual(LoadObjectField(protector_cell, PropertyCell::kValueOffset),
    2377         112 :                    SmiConstant(Isolate::kProtectorValid)),
    2378         112 :          if_true, if_false);
    2379         112 : }
    2380             : 
    2381             : // This function assumes StringPrimitiveWithNoCustomIteration is true.
    2382          56 : TNode<JSArray> StringBuiltinsAssembler::StringToList(TNode<Context> context,
    2383             :                                                      TNode<String> string) {
    2384             :   const ElementsKind kind = PACKED_ELEMENTS;
    2385          56 :   const TNode<IntPtrT> length = LoadStringLengthAsWord(string);
    2386             : 
    2387             :   TNode<Map> array_map =
    2388         112 :       LoadJSArrayElementsMap(kind, LoadNativeContext(context));
    2389             :   TNode<JSArray> array =
    2390             :       AllocateJSArray(kind, array_map, length, SmiTag(length), nullptr,
    2391          56 :                       INTPTR_PARAMETERS, kAllowLargeObjectAllocation);
    2392             :   TNode<FixedArrayBase> elements = LoadElements(array);
    2393             : 
    2394             :   const int first_element_offset = FixedArray::kHeaderSize - kHeapObjectTag;
    2395             :   TNode<IntPtrT> first_to_element_offset =
    2396         112 :       ElementOffsetFromIndex(IntPtrConstant(0), kind, INTPTR_PARAMETERS, 0);
    2397             :   TNode<IntPtrT> first_offset =
    2398          56 :       IntPtrAdd(first_to_element_offset, IntPtrConstant(first_element_offset));
    2399             :   TVARIABLE(IntPtrT, var_offset, first_offset);
    2400          56 :   TVARIABLE(IntPtrT, var_position, IntPtrConstant(0));
    2401         168 :   Label done(this), next_codepoint(this, {&var_position, &var_offset});
    2402             : 
    2403          56 :   Goto(&next_codepoint);
    2404             : 
    2405          56 :   BIND(&next_codepoint);
    2406             :   {
    2407             :     // Loop condition.
    2408         112 :     GotoIfNot(IntPtrLessThan(var_position.value(), length), &done);
    2409             :     const UnicodeEncoding encoding = UnicodeEncoding::UTF16;
    2410             :     TNode<Int32T> ch =
    2411          56 :         LoadSurrogatePairAt(string, length, var_position.value(), encoding);
    2412          56 :     TNode<String> value = StringFromSingleCodePoint(ch, encoding);
    2413             : 
    2414          56 :     Store(elements, var_offset.value(), value);
    2415             : 
    2416             :     // Increment the position.
    2417          56 :     TNode<IntPtrT> ch_length = LoadStringLengthAsWord(value);
    2418             :     var_position = IntPtrAdd(var_position.value(), ch_length);
    2419             :     // Increment the array offset and continue the loop.
    2420          56 :     var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(kTaggedSize));
    2421          56 :     Goto(&next_codepoint);
    2422             :   }
    2423             : 
    2424          56 :   BIND(&done);
    2425             :   TNode<IntPtrT> new_length = IntPtrDiv(
    2426         112 :       IntPtrSub(var_offset.value(), first_offset), IntPtrConstant(kTaggedSize));
    2427             :   CSA_ASSERT(this, IntPtrGreaterThanOrEqual(new_length, IntPtrConstant(0)));
    2428             :   CSA_ASSERT(this, IntPtrGreaterThanOrEqual(length, new_length));
    2429         112 :   StoreObjectFieldNoWriteBarrier(array, JSArray::kLengthOffset,
    2430             :                                  SmiTag(new_length));
    2431             : 
    2432          56 :   return UncheckedCast<JSArray>(array);
    2433             : }
    2434             : 
    2435         280 : TF_BUILTIN(StringToList, StringBuiltinsAssembler) {
    2436          56 :   TNode<Context> context = CAST(Parameter(Descriptor::kContext));
    2437          56 :   TNode<String> string = CAST(Parameter(Descriptor::kSource));
    2438         112 :   Return(StringToList(context, string));
    2439          56 : }
    2440             : 
    2441             : }  // namespace internal
    2442       59456 : }  // namespace v8

Generated by: LCOV version 1.10