11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/builtins/builtins-string-gen.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/base/strings.h"
81cb0ef41Sopenharmony_ci#include "src/builtins/builtins-regexp-gen.h"
91cb0ef41Sopenharmony_ci#include "src/builtins/builtins-utils-gen.h"
101cb0ef41Sopenharmony_ci#include "src/builtins/builtins.h"
111cb0ef41Sopenharmony_ci#include "src/codegen/code-factory.h"
121cb0ef41Sopenharmony_ci#include "src/execution/protectors.h"
131cb0ef41Sopenharmony_ci#include "src/heap/factory-inl.h"
141cb0ef41Sopenharmony_ci#include "src/heap/heap-inl.h"
151cb0ef41Sopenharmony_ci#include "src/logging/counters.h"
161cb0ef41Sopenharmony_ci#include "src/objects/objects.h"
171cb0ef41Sopenharmony_ci#include "src/objects/property-cell.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cinamespace v8 {
201cb0ef41Sopenharmony_cinamespace internal {
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciTNode<RawPtrT> StringBuiltinsAssembler::DirectStringData(
231cb0ef41Sopenharmony_ci    TNode<String> string, TNode<Word32T> string_instance_type) {
241cb0ef41Sopenharmony_ci  // Compute the effective offset of the first character.
251cb0ef41Sopenharmony_ci  TVARIABLE(RawPtrT, var_data);
261cb0ef41Sopenharmony_ci  Label if_sequential(this), if_external(this), if_join(this);
271cb0ef41Sopenharmony_ci  Branch(Word32Equal(Word32And(string_instance_type,
281cb0ef41Sopenharmony_ci                               Int32Constant(kStringRepresentationMask)),
291cb0ef41Sopenharmony_ci                     Int32Constant(kSeqStringTag)),
301cb0ef41Sopenharmony_ci         &if_sequential, &if_external);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  BIND(&if_sequential);
331cb0ef41Sopenharmony_ci  {
341cb0ef41Sopenharmony_ci    var_data = RawPtrAdd(
351cb0ef41Sopenharmony_ci        ReinterpretCast<RawPtrT>(BitcastTaggedToWord(string)),
361cb0ef41Sopenharmony_ci        IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag));
371cb0ef41Sopenharmony_ci    Goto(&if_join);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  BIND(&if_external);
411cb0ef41Sopenharmony_ci  {
421cb0ef41Sopenharmony_ci    var_data = LoadExternalStringResourceDataPtr(CAST(string));
431cb0ef41Sopenharmony_ci    Goto(&if_join);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  BIND(&if_join);
471cb0ef41Sopenharmony_ci  return var_data.value();
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_citemplate <typename SubjectChar, typename PatternChar>
511cb0ef41Sopenharmony_ciTNode<IntPtrT> StringBuiltinsAssembler::CallSearchStringRaw(
521cb0ef41Sopenharmony_ci    const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
531cb0ef41Sopenharmony_ci    const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
541cb0ef41Sopenharmony_ci    const TNode<IntPtrT> start_position) {
551cb0ef41Sopenharmony_ci  const TNode<ExternalReference> function_addr = ExternalConstant(
561cb0ef41Sopenharmony_ci      ExternalReference::search_string_raw<SubjectChar, PatternChar>());
571cb0ef41Sopenharmony_ci  const TNode<ExternalReference> isolate_ptr =
581cb0ef41Sopenharmony_ci      ExternalConstant(ExternalReference::isolate_address(isolate()));
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  MachineType type_ptr = MachineType::Pointer();
611cb0ef41Sopenharmony_ci  MachineType type_intptr = MachineType::IntPtr();
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const TNode<IntPtrT> result = UncheckedCast<IntPtrT>(CallCFunction(
641cb0ef41Sopenharmony_ci      function_addr, type_intptr, std::make_pair(type_ptr, isolate_ptr),
651cb0ef41Sopenharmony_ci      std::make_pair(type_ptr, subject_ptr),
661cb0ef41Sopenharmony_ci      std::make_pair(type_intptr, subject_length),
671cb0ef41Sopenharmony_ci      std::make_pair(type_ptr, search_ptr),
681cb0ef41Sopenharmony_ci      std::make_pair(type_intptr, search_length),
691cb0ef41Sopenharmony_ci      std::make_pair(type_intptr, start_position)));
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  return result;
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ciTNode<IntPtrT> StringBuiltinsAssembler::SearchOneByteStringInTwoByteString(
741cb0ef41Sopenharmony_ci    const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
751cb0ef41Sopenharmony_ci    const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
761cb0ef41Sopenharmony_ci    const TNode<IntPtrT> start_position) {
771cb0ef41Sopenharmony_ci  return CallSearchStringRaw<const base::uc16, const uint8_t>(
781cb0ef41Sopenharmony_ci      subject_ptr, subject_length, search_ptr, search_length, start_position);
791cb0ef41Sopenharmony_ci}
801cb0ef41Sopenharmony_ciTNode<IntPtrT> StringBuiltinsAssembler::SearchOneByteStringInOneByteString(
811cb0ef41Sopenharmony_ci    const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
821cb0ef41Sopenharmony_ci    const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
831cb0ef41Sopenharmony_ci    const TNode<IntPtrT> start_position) {
841cb0ef41Sopenharmony_ci  return CallSearchStringRaw<const uint8_t, const uint8_t>(
851cb0ef41Sopenharmony_ci      subject_ptr, subject_length, search_ptr, search_length, start_position);
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ciTNode<IntPtrT> StringBuiltinsAssembler::SearchTwoByteStringInTwoByteString(
881cb0ef41Sopenharmony_ci    const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
891cb0ef41Sopenharmony_ci    const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
901cb0ef41Sopenharmony_ci    const TNode<IntPtrT> start_position) {
911cb0ef41Sopenharmony_ci  return CallSearchStringRaw<const base::uc16, const base::uc16>(
921cb0ef41Sopenharmony_ci      subject_ptr, subject_length, search_ptr, search_length, start_position);
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ciTNode<IntPtrT> StringBuiltinsAssembler::SearchTwoByteStringInOneByteString(
951cb0ef41Sopenharmony_ci    const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
961cb0ef41Sopenharmony_ci    const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> search_length,
971cb0ef41Sopenharmony_ci    const TNode<IntPtrT> start_position) {
981cb0ef41Sopenharmony_ci  return CallSearchStringRaw<const uint8_t, const base::uc16>(
991cb0ef41Sopenharmony_ci      subject_ptr, subject_length, search_ptr, search_length, start_position);
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ciTNode<IntPtrT> StringBuiltinsAssembler::SearchOneByteInOneByteString(
1021cb0ef41Sopenharmony_ci    const TNode<RawPtrT> subject_ptr, const TNode<IntPtrT> subject_length,
1031cb0ef41Sopenharmony_ci    const TNode<RawPtrT> search_ptr, const TNode<IntPtrT> start_position) {
1041cb0ef41Sopenharmony_ci  const TNode<RawPtrT> subject_start_ptr =
1051cb0ef41Sopenharmony_ci      RawPtrAdd(subject_ptr, start_position);
1061cb0ef41Sopenharmony_ci  const TNode<IntPtrT> search_byte =
1071cb0ef41Sopenharmony_ci      ChangeInt32ToIntPtr(Load<Uint8T>(search_ptr));
1081cb0ef41Sopenharmony_ci  const TNode<UintPtrT> search_length =
1091cb0ef41Sopenharmony_ci      Unsigned(IntPtrSub(subject_length, start_position));
1101cb0ef41Sopenharmony_ci  const TNode<ExternalReference> memchr =
1111cb0ef41Sopenharmony_ci      ExternalConstant(ExternalReference::libc_memchr_function());
1121cb0ef41Sopenharmony_ci  const TNode<RawPtrT> result_address = UncheckedCast<RawPtrT>(
1131cb0ef41Sopenharmony_ci      CallCFunction(memchr, MachineType::Pointer(),
1141cb0ef41Sopenharmony_ci                    std::make_pair(MachineType::Pointer(), subject_start_ptr),
1151cb0ef41Sopenharmony_ci                    std::make_pair(MachineType::IntPtr(), search_byte),
1161cb0ef41Sopenharmony_ci                    std::make_pair(MachineType::UintPtr(), search_length)));
1171cb0ef41Sopenharmony_ci  return Select<IntPtrT>(
1181cb0ef41Sopenharmony_ci      WordEqual(result_address, IntPtrConstant(0)),
1191cb0ef41Sopenharmony_ci      [=] { return IntPtrConstant(-1); },
1201cb0ef41Sopenharmony_ci      [=] {
1211cb0ef41Sopenharmony_ci        return IntPtrAdd(RawPtrSub(result_address, subject_start_ptr),
1221cb0ef41Sopenharmony_ci                         start_position);
1231cb0ef41Sopenharmony_ci      });
1241cb0ef41Sopenharmony_ci}
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::GenerateStringEqual(TNode<String> left,
1271cb0ef41Sopenharmony_ci                                                  TNode<String> right) {
1281cb0ef41Sopenharmony_ci  TVARIABLE(String, var_left, left);
1291cb0ef41Sopenharmony_ci  TVARIABLE(String, var_right, right);
1301cb0ef41Sopenharmony_ci  Label if_equal(this), if_notequal(this), if_indirect(this, Label::kDeferred),
1311cb0ef41Sopenharmony_ci      restart(this, {&var_left, &var_right});
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(left);
1341cb0ef41Sopenharmony_ci  TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(right);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  // Strings with different lengths cannot be equal.
1371cb0ef41Sopenharmony_ci  GotoIf(WordNotEqual(lhs_length, rhs_length), &if_notequal);
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  Goto(&restart);
1401cb0ef41Sopenharmony_ci  BIND(&restart);
1411cb0ef41Sopenharmony_ci  TNode<String> lhs = var_left.value();
1421cb0ef41Sopenharmony_ci  TNode<String> rhs = var_right.value();
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  TNode<Uint16T> lhs_instance_type = LoadInstanceType(lhs);
1451cb0ef41Sopenharmony_ci  TNode<Uint16T> rhs_instance_type = LoadInstanceType(rhs);
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  StringEqual_Core(lhs, lhs_instance_type, rhs, rhs_instance_type, lhs_length,
1481cb0ef41Sopenharmony_ci                   &if_equal, &if_notequal, &if_indirect);
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  BIND(&if_indirect);
1511cb0ef41Sopenharmony_ci  {
1521cb0ef41Sopenharmony_ci    // Try to unwrap indirect strings, restart the above attempt on success.
1531cb0ef41Sopenharmony_ci    MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
1541cb0ef41Sopenharmony_ci                              rhs_instance_type, &restart);
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci    TailCallRuntime(Runtime::kStringEqual, NoContextConstant(), lhs, rhs);
1571cb0ef41Sopenharmony_ci  }
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  BIND(&if_equal);
1601cb0ef41Sopenharmony_ci  Return(TrueConstant());
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci  BIND(&if_notequal);
1631cb0ef41Sopenharmony_ci  Return(FalseConstant());
1641cb0ef41Sopenharmony_ci}
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::StringEqual_Core(
1671cb0ef41Sopenharmony_ci    TNode<String> lhs, TNode<Word32T> lhs_instance_type, TNode<String> rhs,
1681cb0ef41Sopenharmony_ci    TNode<Word32T> rhs_instance_type, TNode<IntPtrT> length, Label* if_equal,
1691cb0ef41Sopenharmony_ci    Label* if_not_equal, Label* if_indirect) {
1701cb0ef41Sopenharmony_ci  CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(lhs), length));
1711cb0ef41Sopenharmony_ci  CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(rhs), length));
1721cb0ef41Sopenharmony_ci  // Fast check to see if {lhs} and {rhs} refer to the same String object.
1731cb0ef41Sopenharmony_ci  GotoIf(TaggedEqual(lhs, rhs), if_equal);
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  // Combine the instance types into a single 16-bit value, so we can check
1761cb0ef41Sopenharmony_ci  // both of them at once.
1771cb0ef41Sopenharmony_ci  TNode<Word32T> both_instance_types = Word32Or(
1781cb0ef41Sopenharmony_ci      lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  // Check if both {lhs} and {rhs} are internalized. Since we already know
1811cb0ef41Sopenharmony_ci  // that they're not the same object, they're not equal in that case.
1821cb0ef41Sopenharmony_ci  int const kBothInternalizedMask =
1831cb0ef41Sopenharmony_ci      kIsNotInternalizedMask | (kIsNotInternalizedMask << 8);
1841cb0ef41Sopenharmony_ci  int const kBothInternalizedTag = kInternalizedTag | (kInternalizedTag << 8);
1851cb0ef41Sopenharmony_ci  GotoIf(Word32Equal(Word32And(both_instance_types,
1861cb0ef41Sopenharmony_ci                               Int32Constant(kBothInternalizedMask)),
1871cb0ef41Sopenharmony_ci                     Int32Constant(kBothInternalizedTag)),
1881cb0ef41Sopenharmony_ci         if_not_equal);
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci  // Check if both {lhs} and {rhs} are direct strings, and that in case of
1911cb0ef41Sopenharmony_ci  // ExternalStrings the data pointer is cached.
1921cb0ef41Sopenharmony_ci  STATIC_ASSERT(kUncachedExternalStringTag != 0);
1931cb0ef41Sopenharmony_ci  STATIC_ASSERT(kIsIndirectStringTag != 0);
1941cb0ef41Sopenharmony_ci  int const kBothDirectStringMask =
1951cb0ef41Sopenharmony_ci      kIsIndirectStringMask | kUncachedExternalStringMask |
1961cb0ef41Sopenharmony_ci      ((kIsIndirectStringMask | kUncachedExternalStringMask) << 8);
1971cb0ef41Sopenharmony_ci  GotoIfNot(Word32Equal(Word32And(both_instance_types,
1981cb0ef41Sopenharmony_ci                                  Int32Constant(kBothDirectStringMask)),
1991cb0ef41Sopenharmony_ci                        Int32Constant(0)),
2001cb0ef41Sopenharmony_ci            if_indirect);
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci  // Dispatch based on the {lhs} and {rhs} string encoding.
2031cb0ef41Sopenharmony_ci  int const kBothStringEncodingMask =
2041cb0ef41Sopenharmony_ci      kStringEncodingMask | (kStringEncodingMask << 8);
2051cb0ef41Sopenharmony_ci  int const kOneOneByteStringTag = kOneByteStringTag | (kOneByteStringTag << 8);
2061cb0ef41Sopenharmony_ci  int const kTwoTwoByteStringTag = kTwoByteStringTag | (kTwoByteStringTag << 8);
2071cb0ef41Sopenharmony_ci  int const kOneTwoByteStringTag = kOneByteStringTag | (kTwoByteStringTag << 8);
2081cb0ef41Sopenharmony_ci  Label if_oneonebytestring(this), if_twotwobytestring(this),
2091cb0ef41Sopenharmony_ci      if_onetwobytestring(this), if_twoonebytestring(this);
2101cb0ef41Sopenharmony_ci  TNode<Word32T> masked_instance_types =
2111cb0ef41Sopenharmony_ci      Word32And(both_instance_types, Int32Constant(kBothStringEncodingMask));
2121cb0ef41Sopenharmony_ci  GotoIf(
2131cb0ef41Sopenharmony_ci      Word32Equal(masked_instance_types, Int32Constant(kOneOneByteStringTag)),
2141cb0ef41Sopenharmony_ci      &if_oneonebytestring);
2151cb0ef41Sopenharmony_ci  GotoIf(
2161cb0ef41Sopenharmony_ci      Word32Equal(masked_instance_types, Int32Constant(kTwoTwoByteStringTag)),
2171cb0ef41Sopenharmony_ci      &if_twotwobytestring);
2181cb0ef41Sopenharmony_ci  Branch(
2191cb0ef41Sopenharmony_ci      Word32Equal(masked_instance_types, Int32Constant(kOneTwoByteStringTag)),
2201cb0ef41Sopenharmony_ci      &if_onetwobytestring, &if_twoonebytestring);
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci  BIND(&if_oneonebytestring);
2231cb0ef41Sopenharmony_ci  StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
2241cb0ef41Sopenharmony_ci                   rhs_instance_type, MachineType::Uint8(), length, if_equal,
2251cb0ef41Sopenharmony_ci                   if_not_equal);
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  BIND(&if_twotwobytestring);
2281cb0ef41Sopenharmony_ci  StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
2291cb0ef41Sopenharmony_ci                   rhs_instance_type, MachineType::Uint16(), length, if_equal,
2301cb0ef41Sopenharmony_ci                   if_not_equal);
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci  BIND(&if_onetwobytestring);
2331cb0ef41Sopenharmony_ci  StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint8(), rhs,
2341cb0ef41Sopenharmony_ci                   rhs_instance_type, MachineType::Uint16(), length, if_equal,
2351cb0ef41Sopenharmony_ci                   if_not_equal);
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci  BIND(&if_twoonebytestring);
2381cb0ef41Sopenharmony_ci  StringEqual_Loop(lhs, lhs_instance_type, MachineType::Uint16(), rhs,
2391cb0ef41Sopenharmony_ci                   rhs_instance_type, MachineType::Uint8(), length, if_equal,
2401cb0ef41Sopenharmony_ci                   if_not_equal);
2411cb0ef41Sopenharmony_ci}
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::StringEqual_Loop(
2441cb0ef41Sopenharmony_ci    TNode<String> lhs, TNode<Word32T> lhs_instance_type, MachineType lhs_type,
2451cb0ef41Sopenharmony_ci    TNode<String> rhs, TNode<Word32T> rhs_instance_type, MachineType rhs_type,
2461cb0ef41Sopenharmony_ci    TNode<IntPtrT> length, Label* if_equal, Label* if_not_equal) {
2471cb0ef41Sopenharmony_ci  CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(lhs), length));
2481cb0ef41Sopenharmony_ci  CSA_DCHECK(this, WordEqual(LoadStringLengthAsWord(rhs), length));
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci  // Compute the effective offset of the first character.
2511cb0ef41Sopenharmony_ci  TNode<RawPtrT> lhs_data = DirectStringData(lhs, lhs_instance_type);
2521cb0ef41Sopenharmony_ci  TNode<RawPtrT> rhs_data = DirectStringData(rhs, rhs_instance_type);
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci  // Loop over the {lhs} and {rhs} strings to see if they are equal.
2551cb0ef41Sopenharmony_ci  TVARIABLE(IntPtrT, var_offset, IntPtrConstant(0));
2561cb0ef41Sopenharmony_ci  Label loop(this, &var_offset);
2571cb0ef41Sopenharmony_ci  Goto(&loop);
2581cb0ef41Sopenharmony_ci  BIND(&loop);
2591cb0ef41Sopenharmony_ci  {
2601cb0ef41Sopenharmony_ci    // If {offset} equals {end}, no difference was found, so the
2611cb0ef41Sopenharmony_ci    // strings are equal.
2621cb0ef41Sopenharmony_ci    GotoIf(WordEqual(var_offset.value(), length), if_equal);
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci    // Load the next characters from {lhs} and {rhs}.
2651cb0ef41Sopenharmony_ci    TNode<Word32T> lhs_value = UncheckedCast<Word32T>(
2661cb0ef41Sopenharmony_ci        Load(lhs_type, lhs_data,
2671cb0ef41Sopenharmony_ci             WordShl(var_offset.value(),
2681cb0ef41Sopenharmony_ci                     ElementSizeLog2Of(lhs_type.representation()))));
2691cb0ef41Sopenharmony_ci    TNode<Word32T> rhs_value = UncheckedCast<Word32T>(
2701cb0ef41Sopenharmony_ci        Load(rhs_type, rhs_data,
2711cb0ef41Sopenharmony_ci             WordShl(var_offset.value(),
2721cb0ef41Sopenharmony_ci                     ElementSizeLog2Of(rhs_type.representation()))));
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ci    // Check if the characters match.
2751cb0ef41Sopenharmony_ci    GotoIf(Word32NotEqual(lhs_value, rhs_value), if_not_equal);
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci    // Advance to next character.
2781cb0ef41Sopenharmony_ci    var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
2791cb0ef41Sopenharmony_ci    Goto(&loop);
2801cb0ef41Sopenharmony_ci  }
2811cb0ef41Sopenharmony_ci}
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::StringFromSingleUTF16EncodedCodePoint(
2841cb0ef41Sopenharmony_ci    TNode<Int32T> codepoint) {
2851cb0ef41Sopenharmony_ci  TVARIABLE(String, var_result, EmptyStringConstant());
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci  Label if_isword16(this), if_isword32(this), return_result(this);
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci  Branch(Uint32LessThan(codepoint, Int32Constant(0x10000)), &if_isword16,
2901cb0ef41Sopenharmony_ci         &if_isword32);
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ci  BIND(&if_isword16);
2931cb0ef41Sopenharmony_ci  {
2941cb0ef41Sopenharmony_ci    var_result = StringFromSingleCharCode(codepoint);
2951cb0ef41Sopenharmony_ci    Goto(&return_result);
2961cb0ef41Sopenharmony_ci  }
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ci  BIND(&if_isword32);
2991cb0ef41Sopenharmony_ci  {
3001cb0ef41Sopenharmony_ci    TNode<String> value = AllocateSeqTwoByteString(2);
3011cb0ef41Sopenharmony_ci    StoreNoWriteBarrier(
3021cb0ef41Sopenharmony_ci        MachineRepresentation::kWord32, value,
3031cb0ef41Sopenharmony_ci        IntPtrConstant(SeqTwoByteString::kHeaderSize - kHeapObjectTag),
3041cb0ef41Sopenharmony_ci        codepoint);
3051cb0ef41Sopenharmony_ci    var_result = value;
3061cb0ef41Sopenharmony_ci    Goto(&return_result);
3071cb0ef41Sopenharmony_ci  }
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci  BIND(&return_result);
3101cb0ef41Sopenharmony_ci  return var_result.value();
3111cb0ef41Sopenharmony_ci}
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::AllocateConsString(TNode<Uint32T> length,
3141cb0ef41Sopenharmony_ci                                                          TNode<String> left,
3151cb0ef41Sopenharmony_ci                                                          TNode<String> right) {
3161cb0ef41Sopenharmony_ci  // Added string can be a cons string.
3171cb0ef41Sopenharmony_ci  Comment("Allocating ConsString");
3181cb0ef41Sopenharmony_ci  TNode<Int32T> left_instance_type = LoadInstanceType(left);
3191cb0ef41Sopenharmony_ci  TNode<Int32T> right_instance_type = LoadInstanceType(right);
3201cb0ef41Sopenharmony_ci
3211cb0ef41Sopenharmony_ci  // Determine the resulting ConsString map to use depending on whether
3221cb0ef41Sopenharmony_ci  // any of {left} or {right} has two byte encoding.
3231cb0ef41Sopenharmony_ci  STATIC_ASSERT(kOneByteStringTag != 0);
3241cb0ef41Sopenharmony_ci  STATIC_ASSERT(kTwoByteStringTag == 0);
3251cb0ef41Sopenharmony_ci  TNode<Int32T> combined_instance_type =
3261cb0ef41Sopenharmony_ci      Word32And(left_instance_type, right_instance_type);
3271cb0ef41Sopenharmony_ci  TNode<Map> result_map = CAST(Select<Object>(
3281cb0ef41Sopenharmony_ci      IsSetWord32(combined_instance_type, kStringEncodingMask),
3291cb0ef41Sopenharmony_ci      [=] { return ConsOneByteStringMapConstant(); },
3301cb0ef41Sopenharmony_ci      [=] { return ConsStringMapConstant(); }));
3311cb0ef41Sopenharmony_ci  TNode<HeapObject> result = AllocateInNewSpace(ConsString::kSize);
3321cb0ef41Sopenharmony_ci  StoreMapNoWriteBarrier(result, result_map);
3331cb0ef41Sopenharmony_ci  StoreObjectFieldNoWriteBarrier(result, ConsString::kLengthOffset, length);
3341cb0ef41Sopenharmony_ci  StoreObjectFieldNoWriteBarrier(result, ConsString::kRawHashFieldOffset,
3351cb0ef41Sopenharmony_ci                                 Int32Constant(String::kEmptyHashField));
3361cb0ef41Sopenharmony_ci  StoreObjectFieldNoWriteBarrier(result, ConsString::kFirstOffset, left);
3371cb0ef41Sopenharmony_ci  StoreObjectFieldNoWriteBarrier(result, ConsString::kSecondOffset, right);
3381cb0ef41Sopenharmony_ci  return CAST(result);
3391cb0ef41Sopenharmony_ci}
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::StringAdd(
3421cb0ef41Sopenharmony_ci    TNode<ContextOrEmptyContext> context, TNode<String> left,
3431cb0ef41Sopenharmony_ci    TNode<String> right) {
3441cb0ef41Sopenharmony_ci  CSA_DCHECK(this, IsZeroOrContext(context));
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci  TVARIABLE(String, result);
3471cb0ef41Sopenharmony_ci  Label check_right(this), runtime(this, Label::kDeferred), cons(this),
3481cb0ef41Sopenharmony_ci      done(this, &result), done_native(this, &result);
3491cb0ef41Sopenharmony_ci  Counters* counters = isolate()->counters();
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci  TNode<Uint32T> left_length = LoadStringLengthAsWord32(left);
3521cb0ef41Sopenharmony_ci  GotoIfNot(Word32Equal(left_length, Uint32Constant(0)), &check_right);
3531cb0ef41Sopenharmony_ci  result = right;
3541cb0ef41Sopenharmony_ci  Goto(&done_native);
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci  BIND(&check_right);
3571cb0ef41Sopenharmony_ci  TNode<Uint32T> right_length = LoadStringLengthAsWord32(right);
3581cb0ef41Sopenharmony_ci  GotoIfNot(Word32Equal(right_length, Uint32Constant(0)), &cons);
3591cb0ef41Sopenharmony_ci  result = left;
3601cb0ef41Sopenharmony_ci  Goto(&done_native);
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci  BIND(&cons);
3631cb0ef41Sopenharmony_ci  {
3641cb0ef41Sopenharmony_ci    TNode<Uint32T> new_length = Uint32Add(left_length, right_length);
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci    // If new length is greater than String::kMaxLength, goto runtime to
3671cb0ef41Sopenharmony_ci    // throw. Note: we also need to invalidate the string length protector, so
3681cb0ef41Sopenharmony_ci    // can't just throw here directly.
3691cb0ef41Sopenharmony_ci    GotoIf(Uint32GreaterThan(new_length, Uint32Constant(String::kMaxLength)),
3701cb0ef41Sopenharmony_ci           &runtime);
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ci    TVARIABLE(String, var_left, left);
3731cb0ef41Sopenharmony_ci    TVARIABLE(String, var_right, right);
3741cb0ef41Sopenharmony_ci    Label non_cons(this, {&var_left, &var_right});
3751cb0ef41Sopenharmony_ci    Label slow(this, Label::kDeferred);
3761cb0ef41Sopenharmony_ci    GotoIf(Uint32LessThan(new_length, Uint32Constant(ConsString::kMinLength)),
3771cb0ef41Sopenharmony_ci           &non_cons);
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ci    result =
3801cb0ef41Sopenharmony_ci        AllocateConsString(new_length, var_left.value(), var_right.value());
3811cb0ef41Sopenharmony_ci    Goto(&done_native);
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci    BIND(&non_cons);
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci    Comment("Full string concatenate");
3861cb0ef41Sopenharmony_ci    TNode<Int32T> left_instance_type = LoadInstanceType(var_left.value());
3871cb0ef41Sopenharmony_ci    TNode<Int32T> right_instance_type = LoadInstanceType(var_right.value());
3881cb0ef41Sopenharmony_ci    // Compute intersection and difference of instance types.
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci    TNode<Int32T> ored_instance_types =
3911cb0ef41Sopenharmony_ci        Word32Or(left_instance_type, right_instance_type);
3921cb0ef41Sopenharmony_ci    TNode<Word32T> xored_instance_types =
3931cb0ef41Sopenharmony_ci        Word32Xor(left_instance_type, right_instance_type);
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci    // Check if both strings have the same encoding and both are sequential.
3961cb0ef41Sopenharmony_ci    GotoIf(IsSetWord32(xored_instance_types, kStringEncodingMask), &runtime);
3971cb0ef41Sopenharmony_ci    GotoIf(IsSetWord32(ored_instance_types, kStringRepresentationMask), &slow);
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ci    TNode<IntPtrT> word_left_length = Signed(ChangeUint32ToWord(left_length));
4001cb0ef41Sopenharmony_ci    TNode<IntPtrT> word_right_length = Signed(ChangeUint32ToWord(right_length));
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci    Label two_byte(this);
4031cb0ef41Sopenharmony_ci    GotoIf(Word32Equal(Word32And(ored_instance_types,
4041cb0ef41Sopenharmony_ci                                 Int32Constant(kStringEncodingMask)),
4051cb0ef41Sopenharmony_ci                       Int32Constant(kTwoByteStringTag)),
4061cb0ef41Sopenharmony_ci           &two_byte);
4071cb0ef41Sopenharmony_ci    // One-byte sequential string case
4081cb0ef41Sopenharmony_ci    result = AllocateSeqOneByteString(new_length);
4091cb0ef41Sopenharmony_ci    CopyStringCharacters(var_left.value(), result.value(), IntPtrConstant(0),
4101cb0ef41Sopenharmony_ci                         IntPtrConstant(0), word_left_length,
4111cb0ef41Sopenharmony_ci                         String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING);
4121cb0ef41Sopenharmony_ci    CopyStringCharacters(var_right.value(), result.value(), IntPtrConstant(0),
4131cb0ef41Sopenharmony_ci                         word_left_length, word_right_length,
4141cb0ef41Sopenharmony_ci                         String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING);
4151cb0ef41Sopenharmony_ci    Goto(&done_native);
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci    BIND(&two_byte);
4181cb0ef41Sopenharmony_ci    {
4191cb0ef41Sopenharmony_ci      // Two-byte sequential string case
4201cb0ef41Sopenharmony_ci      result = AllocateSeqTwoByteString(new_length);
4211cb0ef41Sopenharmony_ci      CopyStringCharacters(var_left.value(), result.value(), IntPtrConstant(0),
4221cb0ef41Sopenharmony_ci                           IntPtrConstant(0), word_left_length,
4231cb0ef41Sopenharmony_ci                           String::TWO_BYTE_ENCODING,
4241cb0ef41Sopenharmony_ci                           String::TWO_BYTE_ENCODING);
4251cb0ef41Sopenharmony_ci      CopyStringCharacters(var_right.value(), result.value(), IntPtrConstant(0),
4261cb0ef41Sopenharmony_ci                           word_left_length, word_right_length,
4271cb0ef41Sopenharmony_ci                           String::TWO_BYTE_ENCODING,
4281cb0ef41Sopenharmony_ci                           String::TWO_BYTE_ENCODING);
4291cb0ef41Sopenharmony_ci      Goto(&done_native);
4301cb0ef41Sopenharmony_ci    }
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ci    BIND(&slow);
4331cb0ef41Sopenharmony_ci    {
4341cb0ef41Sopenharmony_ci      // Try to unwrap indirect strings, restart the above attempt on success.
4351cb0ef41Sopenharmony_ci      MaybeDerefIndirectStrings(&var_left, left_instance_type, &var_right,
4361cb0ef41Sopenharmony_ci                                right_instance_type, &non_cons);
4371cb0ef41Sopenharmony_ci      Goto(&runtime);
4381cb0ef41Sopenharmony_ci    }
4391cb0ef41Sopenharmony_ci  }
4401cb0ef41Sopenharmony_ci  BIND(&runtime);
4411cb0ef41Sopenharmony_ci  {
4421cb0ef41Sopenharmony_ci    result = CAST(CallRuntime(Runtime::kStringAdd, context, left, right));
4431cb0ef41Sopenharmony_ci    Goto(&done);
4441cb0ef41Sopenharmony_ci  }
4451cb0ef41Sopenharmony_ci
4461cb0ef41Sopenharmony_ci  BIND(&done_native);
4471cb0ef41Sopenharmony_ci  {
4481cb0ef41Sopenharmony_ci    IncrementCounter(counters->string_add_native(), 1);
4491cb0ef41Sopenharmony_ci    Goto(&done);
4501cb0ef41Sopenharmony_ci  }
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci  BIND(&done);
4531cb0ef41Sopenharmony_ci  return result.value();
4541cb0ef41Sopenharmony_ci}
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::BranchIfCanDerefIndirectString(
4571cb0ef41Sopenharmony_ci    TNode<String> string, TNode<Int32T> instance_type, Label* can_deref,
4581cb0ef41Sopenharmony_ci    Label* cannot_deref) {
4591cb0ef41Sopenharmony_ci  TNode<Int32T> representation =
4601cb0ef41Sopenharmony_ci      Word32And(instance_type, Int32Constant(kStringRepresentationMask));
4611cb0ef41Sopenharmony_ci  GotoIf(Word32Equal(representation, Int32Constant(kThinStringTag)), can_deref);
4621cb0ef41Sopenharmony_ci  GotoIf(Word32NotEqual(representation, Int32Constant(kConsStringTag)),
4631cb0ef41Sopenharmony_ci         cannot_deref);
4641cb0ef41Sopenharmony_ci  // Cons string.
4651cb0ef41Sopenharmony_ci  TNode<String> rhs =
4661cb0ef41Sopenharmony_ci      LoadObjectField<String>(string, ConsString::kSecondOffset);
4671cb0ef41Sopenharmony_ci  GotoIf(IsEmptyString(rhs), can_deref);
4681cb0ef41Sopenharmony_ci  Goto(cannot_deref);
4691cb0ef41Sopenharmony_ci}
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::DerefIndirectString(TVariable<String>* var_string,
4721cb0ef41Sopenharmony_ci                                                  TNode<Int32T> instance_type) {
4731cb0ef41Sopenharmony_ci#ifdef DEBUG
4741cb0ef41Sopenharmony_ci  Label can_deref(this), cannot_deref(this);
4751cb0ef41Sopenharmony_ci  BranchIfCanDerefIndirectString(var_string->value(), instance_type, &can_deref,
4761cb0ef41Sopenharmony_ci                                 &cannot_deref);
4771cb0ef41Sopenharmony_ci  BIND(&cannot_deref);
4781cb0ef41Sopenharmony_ci  DebugBreak();  // Should be able to dereference string.
4791cb0ef41Sopenharmony_ci  Goto(&can_deref);
4801cb0ef41Sopenharmony_ci  BIND(&can_deref);
4811cb0ef41Sopenharmony_ci#endif  // DEBUG
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci  STATIC_ASSERT(static_cast<int>(ThinString::kActualOffset) ==
4841cb0ef41Sopenharmony_ci                static_cast<int>(ConsString::kFirstOffset));
4851cb0ef41Sopenharmony_ci  *var_string =
4861cb0ef41Sopenharmony_ci      LoadObjectField<String>(var_string->value(), ThinString::kActualOffset);
4871cb0ef41Sopenharmony_ci}
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::MaybeDerefIndirectString(
4901cb0ef41Sopenharmony_ci    TVariable<String>* var_string, TNode<Int32T> instance_type,
4911cb0ef41Sopenharmony_ci    Label* did_deref, Label* cannot_deref) {
4921cb0ef41Sopenharmony_ci  Label deref(this);
4931cb0ef41Sopenharmony_ci  BranchIfCanDerefIndirectString(var_string->value(), instance_type, &deref,
4941cb0ef41Sopenharmony_ci                                 cannot_deref);
4951cb0ef41Sopenharmony_ci
4961cb0ef41Sopenharmony_ci  BIND(&deref);
4971cb0ef41Sopenharmony_ci  {
4981cb0ef41Sopenharmony_ci    DerefIndirectString(var_string, instance_type);
4991cb0ef41Sopenharmony_ci    Goto(did_deref);
5001cb0ef41Sopenharmony_ci  }
5011cb0ef41Sopenharmony_ci}
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::MaybeDerefIndirectStrings(
5041cb0ef41Sopenharmony_ci    TVariable<String>* var_left, TNode<Int32T> left_instance_type,
5051cb0ef41Sopenharmony_ci    TVariable<String>* var_right, TNode<Int32T> right_instance_type,
5061cb0ef41Sopenharmony_ci    Label* did_something) {
5071cb0ef41Sopenharmony_ci  Label did_nothing_left(this), did_something_left(this),
5081cb0ef41Sopenharmony_ci      didnt_do_anything(this);
5091cb0ef41Sopenharmony_ci  MaybeDerefIndirectString(var_left, left_instance_type, &did_something_left,
5101cb0ef41Sopenharmony_ci                           &did_nothing_left);
5111cb0ef41Sopenharmony_ci
5121cb0ef41Sopenharmony_ci  BIND(&did_something_left);
5131cb0ef41Sopenharmony_ci  {
5141cb0ef41Sopenharmony_ci    MaybeDerefIndirectString(var_right, right_instance_type, did_something,
5151cb0ef41Sopenharmony_ci                             did_something);
5161cb0ef41Sopenharmony_ci  }
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ci  BIND(&did_nothing_left);
5191cb0ef41Sopenharmony_ci  {
5201cb0ef41Sopenharmony_ci    MaybeDerefIndirectString(var_right, right_instance_type, did_something,
5211cb0ef41Sopenharmony_ci                             &didnt_do_anything);
5221cb0ef41Sopenharmony_ci  }
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci  BIND(&didnt_do_anything);
5251cb0ef41Sopenharmony_ci  // Fall through if neither string was an indirect string.
5261cb0ef41Sopenharmony_ci}
5271cb0ef41Sopenharmony_ci
5281cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::DerefIndirectString(
5291cb0ef41Sopenharmony_ci    TNode<String> string, TNode<Int32T> instance_type, Label* cannot_deref) {
5301cb0ef41Sopenharmony_ci  Label deref(this);
5311cb0ef41Sopenharmony_ci  BranchIfCanDerefIndirectString(string, instance_type, &deref, cannot_deref);
5321cb0ef41Sopenharmony_ci  BIND(&deref);
5331cb0ef41Sopenharmony_ci  STATIC_ASSERT(static_cast<int>(ThinString::kActualOffset) ==
5341cb0ef41Sopenharmony_ci                static_cast<int>(ConsString::kFirstOffset));
5351cb0ef41Sopenharmony_ci  return LoadObjectField<String>(string, ThinString::kActualOffset);
5361cb0ef41Sopenharmony_ci}
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_ciTF_BUILTIN(StringAdd_CheckNone, StringBuiltinsAssembler) {
5391cb0ef41Sopenharmony_ci  auto left = Parameter<String>(Descriptor::kLeft);
5401cb0ef41Sopenharmony_ci  auto right = Parameter<String>(Descriptor::kRight);
5411cb0ef41Sopenharmony_ci  TNode<ContextOrEmptyContext> context =
5421cb0ef41Sopenharmony_ci      UncheckedParameter<ContextOrEmptyContext>(Descriptor::kContext);
5431cb0ef41Sopenharmony_ci  CSA_DCHECK(this, IsZeroOrContext(context));
5441cb0ef41Sopenharmony_ci  Return(StringAdd(context, left, right));
5451cb0ef41Sopenharmony_ci}
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ciTF_BUILTIN(SubString, StringBuiltinsAssembler) {
5481cb0ef41Sopenharmony_ci  auto string = Parameter<String>(Descriptor::kString);
5491cb0ef41Sopenharmony_ci  auto from = Parameter<Smi>(Descriptor::kFrom);
5501cb0ef41Sopenharmony_ci  auto to = Parameter<Smi>(Descriptor::kTo);
5511cb0ef41Sopenharmony_ci  Return(SubString(string, SmiUntag(from), SmiUntag(to)));
5521cb0ef41Sopenharmony_ci}
5531cb0ef41Sopenharmony_ci
5541cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::GenerateStringRelationalComparison(
5551cb0ef41Sopenharmony_ci    TNode<String> left, TNode<String> right, Operation op) {
5561cb0ef41Sopenharmony_ci  TVARIABLE(String, var_left, left);
5571cb0ef41Sopenharmony_ci  TVARIABLE(String, var_right, right);
5581cb0ef41Sopenharmony_ci
5591cb0ef41Sopenharmony_ci  Label if_less(this), if_equal(this), if_greater(this);
5601cb0ef41Sopenharmony_ci  Label restart(this, {&var_left, &var_right});
5611cb0ef41Sopenharmony_ci  Goto(&restart);
5621cb0ef41Sopenharmony_ci  BIND(&restart);
5631cb0ef41Sopenharmony_ci
5641cb0ef41Sopenharmony_ci  TNode<String> lhs = var_left.value();
5651cb0ef41Sopenharmony_ci  TNode<String> rhs = var_right.value();
5661cb0ef41Sopenharmony_ci  // Fast check to see if {lhs} and {rhs} refer to the same String object.
5671cb0ef41Sopenharmony_ci  GotoIf(TaggedEqual(lhs, rhs), &if_equal);
5681cb0ef41Sopenharmony_ci
5691cb0ef41Sopenharmony_ci  // Load instance types of {lhs} and {rhs}.
5701cb0ef41Sopenharmony_ci  TNode<Uint16T> lhs_instance_type = LoadInstanceType(lhs);
5711cb0ef41Sopenharmony_ci  TNode<Uint16T> rhs_instance_type = LoadInstanceType(rhs);
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci  // Combine the instance types into a single 16-bit value, so we can check
5741cb0ef41Sopenharmony_ci  // both of them at once.
5751cb0ef41Sopenharmony_ci  TNode<Int32T> both_instance_types = Word32Or(
5761cb0ef41Sopenharmony_ci      lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
5771cb0ef41Sopenharmony_ci
5781cb0ef41Sopenharmony_ci  // Check that both {lhs} and {rhs} are flat one-byte strings.
5791cb0ef41Sopenharmony_ci  int const kBothSeqOneByteStringMask =
5801cb0ef41Sopenharmony_ci      kStringEncodingMask | kStringRepresentationMask |
5811cb0ef41Sopenharmony_ci      ((kStringEncodingMask | kStringRepresentationMask) << 8);
5821cb0ef41Sopenharmony_ci  int const kBothSeqOneByteStringTag =
5831cb0ef41Sopenharmony_ci      kOneByteStringTag | kSeqStringTag |
5841cb0ef41Sopenharmony_ci      ((kOneByteStringTag | kSeqStringTag) << 8);
5851cb0ef41Sopenharmony_ci  Label if_bothonebyteseqstrings(this), if_notbothonebyteseqstrings(this);
5861cb0ef41Sopenharmony_ci  Branch(Word32Equal(Word32And(both_instance_types,
5871cb0ef41Sopenharmony_ci                               Int32Constant(kBothSeqOneByteStringMask)),
5881cb0ef41Sopenharmony_ci                     Int32Constant(kBothSeqOneByteStringTag)),
5891cb0ef41Sopenharmony_ci         &if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
5901cb0ef41Sopenharmony_ci
5911cb0ef41Sopenharmony_ci  BIND(&if_bothonebyteseqstrings);
5921cb0ef41Sopenharmony_ci  {
5931cb0ef41Sopenharmony_ci    // Load the length of {lhs} and {rhs}.
5941cb0ef41Sopenharmony_ci    TNode<IntPtrT> lhs_length = LoadStringLengthAsWord(lhs);
5951cb0ef41Sopenharmony_ci    TNode<IntPtrT> rhs_length = LoadStringLengthAsWord(rhs);
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_ci    // Determine the minimum length.
5981cb0ef41Sopenharmony_ci    TNode<IntPtrT> length = IntPtrMin(lhs_length, rhs_length);
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ci    // Compute the effective offset of the first character.
6011cb0ef41Sopenharmony_ci    TNode<IntPtrT> begin =
6021cb0ef41Sopenharmony_ci        IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag);
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ci    // Compute the first offset after the string from the length.
6051cb0ef41Sopenharmony_ci    TNode<IntPtrT> end = IntPtrAdd(begin, length);
6061cb0ef41Sopenharmony_ci
6071cb0ef41Sopenharmony_ci    // Loop over the {lhs} and {rhs} strings to see if they are equal.
6081cb0ef41Sopenharmony_ci    TVARIABLE(IntPtrT, var_offset, begin);
6091cb0ef41Sopenharmony_ci    Label loop(this, &var_offset);
6101cb0ef41Sopenharmony_ci    Goto(&loop);
6111cb0ef41Sopenharmony_ci    BIND(&loop);
6121cb0ef41Sopenharmony_ci    {
6131cb0ef41Sopenharmony_ci      // Check if {offset} equals {end}.
6141cb0ef41Sopenharmony_ci      Label if_done(this), if_notdone(this);
6151cb0ef41Sopenharmony_ci      Branch(WordEqual(var_offset.value(), end), &if_done, &if_notdone);
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci      BIND(&if_notdone);
6181cb0ef41Sopenharmony_ci      {
6191cb0ef41Sopenharmony_ci        // Load the next characters from {lhs} and {rhs}.
6201cb0ef41Sopenharmony_ci        TNode<Uint8T> lhs_value = Load<Uint8T>(lhs, var_offset.value());
6211cb0ef41Sopenharmony_ci        TNode<Uint8T> rhs_value = Load<Uint8T>(rhs, var_offset.value());
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ci        // Check if the characters match.
6241cb0ef41Sopenharmony_ci        Label if_valueissame(this), if_valueisnotsame(this);
6251cb0ef41Sopenharmony_ci        Branch(Word32Equal(lhs_value, rhs_value), &if_valueissame,
6261cb0ef41Sopenharmony_ci               &if_valueisnotsame);
6271cb0ef41Sopenharmony_ci
6281cb0ef41Sopenharmony_ci        BIND(&if_valueissame);
6291cb0ef41Sopenharmony_ci        {
6301cb0ef41Sopenharmony_ci          // Advance to next character.
6311cb0ef41Sopenharmony_ci          var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(1));
6321cb0ef41Sopenharmony_ci        }
6331cb0ef41Sopenharmony_ci        Goto(&loop);
6341cb0ef41Sopenharmony_ci
6351cb0ef41Sopenharmony_ci        BIND(&if_valueisnotsame);
6361cb0ef41Sopenharmony_ci        Branch(Uint32LessThan(lhs_value, rhs_value), &if_less, &if_greater);
6371cb0ef41Sopenharmony_ci      }
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci      BIND(&if_done);
6401cb0ef41Sopenharmony_ci      {
6411cb0ef41Sopenharmony_ci        // All characters up to the min length are equal, decide based on
6421cb0ef41Sopenharmony_ci        // string length.
6431cb0ef41Sopenharmony_ci        GotoIf(IntPtrEqual(lhs_length, rhs_length), &if_equal);
6441cb0ef41Sopenharmony_ci        Branch(IntPtrLessThan(lhs_length, rhs_length), &if_less, &if_greater);
6451cb0ef41Sopenharmony_ci      }
6461cb0ef41Sopenharmony_ci    }
6471cb0ef41Sopenharmony_ci  }
6481cb0ef41Sopenharmony_ci
6491cb0ef41Sopenharmony_ci  BIND(&if_notbothonebyteseqstrings);
6501cb0ef41Sopenharmony_ci  {
6511cb0ef41Sopenharmony_ci    // Try to unwrap indirect strings, restart the above attempt on success.
6521cb0ef41Sopenharmony_ci    MaybeDerefIndirectStrings(&var_left, lhs_instance_type, &var_right,
6531cb0ef41Sopenharmony_ci                              rhs_instance_type, &restart);
6541cb0ef41Sopenharmony_ci    // TODO(bmeurer): Add support for two byte string relational comparisons.
6551cb0ef41Sopenharmony_ci    switch (op) {
6561cb0ef41Sopenharmony_ci      case Operation::kLessThan:
6571cb0ef41Sopenharmony_ci        TailCallRuntime(Runtime::kStringLessThan, NoContextConstant(), lhs,
6581cb0ef41Sopenharmony_ci                        rhs);
6591cb0ef41Sopenharmony_ci        break;
6601cb0ef41Sopenharmony_ci      case Operation::kLessThanOrEqual:
6611cb0ef41Sopenharmony_ci        TailCallRuntime(Runtime::kStringLessThanOrEqual, NoContextConstant(),
6621cb0ef41Sopenharmony_ci                        lhs, rhs);
6631cb0ef41Sopenharmony_ci        break;
6641cb0ef41Sopenharmony_ci      case Operation::kGreaterThan:
6651cb0ef41Sopenharmony_ci        TailCallRuntime(Runtime::kStringGreaterThan, NoContextConstant(), lhs,
6661cb0ef41Sopenharmony_ci                        rhs);
6671cb0ef41Sopenharmony_ci        break;
6681cb0ef41Sopenharmony_ci      case Operation::kGreaterThanOrEqual:
6691cb0ef41Sopenharmony_ci        TailCallRuntime(Runtime::kStringGreaterThanOrEqual, NoContextConstant(),
6701cb0ef41Sopenharmony_ci                        lhs, rhs);
6711cb0ef41Sopenharmony_ci        break;
6721cb0ef41Sopenharmony_ci      default:
6731cb0ef41Sopenharmony_ci        UNREACHABLE();
6741cb0ef41Sopenharmony_ci    }
6751cb0ef41Sopenharmony_ci  }
6761cb0ef41Sopenharmony_ci
6771cb0ef41Sopenharmony_ci  BIND(&if_less);
6781cb0ef41Sopenharmony_ci  switch (op) {
6791cb0ef41Sopenharmony_ci    case Operation::kLessThan:
6801cb0ef41Sopenharmony_ci    case Operation::kLessThanOrEqual:
6811cb0ef41Sopenharmony_ci      Return(TrueConstant());
6821cb0ef41Sopenharmony_ci      break;
6831cb0ef41Sopenharmony_ci
6841cb0ef41Sopenharmony_ci    case Operation::kGreaterThan:
6851cb0ef41Sopenharmony_ci    case Operation::kGreaterThanOrEqual:
6861cb0ef41Sopenharmony_ci      Return(FalseConstant());
6871cb0ef41Sopenharmony_ci      break;
6881cb0ef41Sopenharmony_ci    default:
6891cb0ef41Sopenharmony_ci      UNREACHABLE();
6901cb0ef41Sopenharmony_ci  }
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ci  BIND(&if_equal);
6931cb0ef41Sopenharmony_ci  switch (op) {
6941cb0ef41Sopenharmony_ci    case Operation::kLessThan:
6951cb0ef41Sopenharmony_ci    case Operation::kGreaterThan:
6961cb0ef41Sopenharmony_ci      Return(FalseConstant());
6971cb0ef41Sopenharmony_ci      break;
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ci    case Operation::kLessThanOrEqual:
7001cb0ef41Sopenharmony_ci    case Operation::kGreaterThanOrEqual:
7011cb0ef41Sopenharmony_ci      Return(TrueConstant());
7021cb0ef41Sopenharmony_ci      break;
7031cb0ef41Sopenharmony_ci    default:
7041cb0ef41Sopenharmony_ci      UNREACHABLE();
7051cb0ef41Sopenharmony_ci  }
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci  BIND(&if_greater);
7081cb0ef41Sopenharmony_ci  switch (op) {
7091cb0ef41Sopenharmony_ci    case Operation::kLessThan:
7101cb0ef41Sopenharmony_ci    case Operation::kLessThanOrEqual:
7111cb0ef41Sopenharmony_ci      Return(FalseConstant());
7121cb0ef41Sopenharmony_ci      break;
7131cb0ef41Sopenharmony_ci
7141cb0ef41Sopenharmony_ci    case Operation::kGreaterThan:
7151cb0ef41Sopenharmony_ci    case Operation::kGreaterThanOrEqual:
7161cb0ef41Sopenharmony_ci      Return(TrueConstant());
7171cb0ef41Sopenharmony_ci      break;
7181cb0ef41Sopenharmony_ci    default:
7191cb0ef41Sopenharmony_ci      UNREACHABLE();
7201cb0ef41Sopenharmony_ci  }
7211cb0ef41Sopenharmony_ci}
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ciTF_BUILTIN(StringEqual, StringBuiltinsAssembler) {
7241cb0ef41Sopenharmony_ci  auto left = Parameter<String>(Descriptor::kLeft);
7251cb0ef41Sopenharmony_ci  auto right = Parameter<String>(Descriptor::kRight);
7261cb0ef41Sopenharmony_ci  GenerateStringEqual(left, right);
7271cb0ef41Sopenharmony_ci}
7281cb0ef41Sopenharmony_ci
7291cb0ef41Sopenharmony_ciTF_BUILTIN(StringLessThan, StringBuiltinsAssembler) {
7301cb0ef41Sopenharmony_ci  auto left = Parameter<String>(Descriptor::kLeft);
7311cb0ef41Sopenharmony_ci  auto right = Parameter<String>(Descriptor::kRight);
7321cb0ef41Sopenharmony_ci  GenerateStringRelationalComparison(left, right, Operation::kLessThan);
7331cb0ef41Sopenharmony_ci}
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ciTF_BUILTIN(StringLessThanOrEqual, StringBuiltinsAssembler) {
7361cb0ef41Sopenharmony_ci  auto left = Parameter<String>(Descriptor::kLeft);
7371cb0ef41Sopenharmony_ci  auto right = Parameter<String>(Descriptor::kRight);
7381cb0ef41Sopenharmony_ci  GenerateStringRelationalComparison(left, right, Operation::kLessThanOrEqual);
7391cb0ef41Sopenharmony_ci}
7401cb0ef41Sopenharmony_ci
7411cb0ef41Sopenharmony_ciTF_BUILTIN(StringGreaterThan, StringBuiltinsAssembler) {
7421cb0ef41Sopenharmony_ci  auto left = Parameter<String>(Descriptor::kLeft);
7431cb0ef41Sopenharmony_ci  auto right = Parameter<String>(Descriptor::kRight);
7441cb0ef41Sopenharmony_ci  GenerateStringRelationalComparison(left, right, Operation::kGreaterThan);
7451cb0ef41Sopenharmony_ci}
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_ciTF_BUILTIN(StringGreaterThanOrEqual, StringBuiltinsAssembler) {
7481cb0ef41Sopenharmony_ci  auto left = Parameter<String>(Descriptor::kLeft);
7491cb0ef41Sopenharmony_ci  auto right = Parameter<String>(Descriptor::kRight);
7501cb0ef41Sopenharmony_ci  GenerateStringRelationalComparison(left, right,
7511cb0ef41Sopenharmony_ci                                     Operation::kGreaterThanOrEqual);
7521cb0ef41Sopenharmony_ci}
7531cb0ef41Sopenharmony_ci
7541cb0ef41Sopenharmony_ciTF_BUILTIN(StringFromCodePointAt, StringBuiltinsAssembler) {
7551cb0ef41Sopenharmony_ci  auto receiver = Parameter<String>(Descriptor::kReceiver);
7561cb0ef41Sopenharmony_ci  auto position = UncheckedParameter<IntPtrT>(Descriptor::kPosition);
7571cb0ef41Sopenharmony_ci
7581cb0ef41Sopenharmony_ci  // TODO(sigurds) Figure out if passing length as argument pays off.
7591cb0ef41Sopenharmony_ci  TNode<IntPtrT> length = LoadStringLengthAsWord(receiver);
7601cb0ef41Sopenharmony_ci  // Load the character code at the {position} from the {receiver}.
7611cb0ef41Sopenharmony_ci  TNode<Int32T> code =
7621cb0ef41Sopenharmony_ci      LoadSurrogatePairAt(receiver, length, position, UnicodeEncoding::UTF16);
7631cb0ef41Sopenharmony_ci  // Create a String from the UTF16 encoded code point
7641cb0ef41Sopenharmony_ci  TNode<String> result = StringFromSingleUTF16EncodedCodePoint(code);
7651cb0ef41Sopenharmony_ci  Return(result);
7661cb0ef41Sopenharmony_ci}
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci// -----------------------------------------------------------------------------
7691cb0ef41Sopenharmony_ci// ES6 section 21.1 String Objects
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ci// ES6 #sec-string.fromcharcode
7721cb0ef41Sopenharmony_ciTF_BUILTIN(StringFromCharCode, StringBuiltinsAssembler) {
7731cb0ef41Sopenharmony_ci  // TODO(ishell): use constants from Descriptor once the JSFunction linkage
7741cb0ef41Sopenharmony_ci  // arguments are reordered.
7751cb0ef41Sopenharmony_ci  auto argc = UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount);
7761cb0ef41Sopenharmony_ci  auto context = Parameter<Context>(Descriptor::kContext);
7771cb0ef41Sopenharmony_ci
7781cb0ef41Sopenharmony_ci  CodeStubArguments arguments(this, argc);
7791cb0ef41Sopenharmony_ci  TNode<Uint32T> unsigned_argc =
7801cb0ef41Sopenharmony_ci      Unsigned(TruncateIntPtrToInt32(arguments.GetLengthWithoutReceiver()));
7811cb0ef41Sopenharmony_ci  // Check if we have exactly one argument (plus the implicit receiver), i.e.
7821cb0ef41Sopenharmony_ci  // if the parent frame is not an arguments adaptor frame.
7831cb0ef41Sopenharmony_ci  Label if_oneargument(this), if_notoneargument(this);
7841cb0ef41Sopenharmony_ci  Branch(IntPtrEqual(arguments.GetLengthWithoutReceiver(), IntPtrConstant(1)),
7851cb0ef41Sopenharmony_ci         &if_oneargument, &if_notoneargument);
7861cb0ef41Sopenharmony_ci
7871cb0ef41Sopenharmony_ci  BIND(&if_oneargument);
7881cb0ef41Sopenharmony_ci  {
7891cb0ef41Sopenharmony_ci    // Single argument case, perform fast single character string cache lookup
7901cb0ef41Sopenharmony_ci    // for one-byte code units, or fall back to creating a single character
7911cb0ef41Sopenharmony_ci    // string on the fly otherwise.
7921cb0ef41Sopenharmony_ci    TNode<Object> code = arguments.AtIndex(0);
7931cb0ef41Sopenharmony_ci    TNode<Word32T> code32 = TruncateTaggedToWord32(context, code);
7941cb0ef41Sopenharmony_ci    TNode<Int32T> code16 =
7951cb0ef41Sopenharmony_ci        Signed(Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit)));
7961cb0ef41Sopenharmony_ci    TNode<String> result = StringFromSingleCharCode(code16);
7971cb0ef41Sopenharmony_ci    arguments.PopAndReturn(result);
7981cb0ef41Sopenharmony_ci  }
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ci  TNode<Word32T> code16;
8011cb0ef41Sopenharmony_ci  BIND(&if_notoneargument);
8021cb0ef41Sopenharmony_ci  {
8031cb0ef41Sopenharmony_ci    Label two_byte(this);
8041cb0ef41Sopenharmony_ci    // Assume that the resulting string contains only one-byte characters.
8051cb0ef41Sopenharmony_ci    TNode<String> one_byte_result = AllocateSeqOneByteString(unsigned_argc);
8061cb0ef41Sopenharmony_ci
8071cb0ef41Sopenharmony_ci    TVARIABLE(IntPtrT, var_max_index, IntPtrConstant(0));
8081cb0ef41Sopenharmony_ci
8091cb0ef41Sopenharmony_ci    // Iterate over the incoming arguments, converting them to 8-bit character
8101cb0ef41Sopenharmony_ci    // codes. Stop if any of the conversions generates a code that doesn't fit
8111cb0ef41Sopenharmony_ci    // in 8 bits.
8121cb0ef41Sopenharmony_ci    CodeStubAssembler::VariableList vars({&var_max_index}, zone());
8131cb0ef41Sopenharmony_ci    arguments.ForEach(vars, [&](TNode<Object> arg) {
8141cb0ef41Sopenharmony_ci      TNode<Word32T> code32 = TruncateTaggedToWord32(context, arg);
8151cb0ef41Sopenharmony_ci      code16 = Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
8161cb0ef41Sopenharmony_ci
8171cb0ef41Sopenharmony_ci      GotoIf(
8181cb0ef41Sopenharmony_ci          Int32GreaterThan(code16, Int32Constant(String::kMaxOneByteCharCode)),
8191cb0ef41Sopenharmony_ci          &two_byte);
8201cb0ef41Sopenharmony_ci
8211cb0ef41Sopenharmony_ci      // The {code16} fits into the SeqOneByteString {one_byte_result}.
8221cb0ef41Sopenharmony_ci      TNode<IntPtrT> offset = ElementOffsetFromIndex(
8231cb0ef41Sopenharmony_ci          var_max_index.value(), UINT8_ELEMENTS,
8241cb0ef41Sopenharmony_ci          SeqOneByteString::kHeaderSize - kHeapObjectTag);
8251cb0ef41Sopenharmony_ci      StoreNoWriteBarrier(MachineRepresentation::kWord8, one_byte_result,
8261cb0ef41Sopenharmony_ci                          offset, code16);
8271cb0ef41Sopenharmony_ci      var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
8281cb0ef41Sopenharmony_ci    });
8291cb0ef41Sopenharmony_ci    arguments.PopAndReturn(one_byte_result);
8301cb0ef41Sopenharmony_ci
8311cb0ef41Sopenharmony_ci    BIND(&two_byte);
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci    // At least one of the characters in the string requires a 16-bit
8341cb0ef41Sopenharmony_ci    // representation.  Allocate a SeqTwoByteString to hold the resulting
8351cb0ef41Sopenharmony_ci    // string.
8361cb0ef41Sopenharmony_ci    TNode<String> two_byte_result = AllocateSeqTwoByteString(unsigned_argc);
8371cb0ef41Sopenharmony_ci
8381cb0ef41Sopenharmony_ci    // Copy the characters that have already been put in the 8-bit string into
8391cb0ef41Sopenharmony_ci    // their corresponding positions in the new 16-bit string.
8401cb0ef41Sopenharmony_ci    TNode<IntPtrT> zero = IntPtrConstant(0);
8411cb0ef41Sopenharmony_ci    CopyStringCharacters(one_byte_result, two_byte_result, zero, zero,
8421cb0ef41Sopenharmony_ci                         var_max_index.value(), String::ONE_BYTE_ENCODING,
8431cb0ef41Sopenharmony_ci                         String::TWO_BYTE_ENCODING);
8441cb0ef41Sopenharmony_ci
8451cb0ef41Sopenharmony_ci    // Write the character that caused the 8-bit to 16-bit fault.
8461cb0ef41Sopenharmony_ci    TNode<IntPtrT> max_index_offset =
8471cb0ef41Sopenharmony_ci        ElementOffsetFromIndex(var_max_index.value(), UINT16_ELEMENTS,
8481cb0ef41Sopenharmony_ci                               SeqTwoByteString::kHeaderSize - kHeapObjectTag);
8491cb0ef41Sopenharmony_ci    StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
8501cb0ef41Sopenharmony_ci                        max_index_offset, code16);
8511cb0ef41Sopenharmony_ci    var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
8521cb0ef41Sopenharmony_ci
8531cb0ef41Sopenharmony_ci    // Resume copying the passed-in arguments from the same place where the
8541cb0ef41Sopenharmony_ci    // 8-bit copy stopped, but this time copying over all of the characters
8551cb0ef41Sopenharmony_ci    // using a 16-bit representation.
8561cb0ef41Sopenharmony_ci    arguments.ForEach(
8571cb0ef41Sopenharmony_ci        vars,
8581cb0ef41Sopenharmony_ci        [&](TNode<Object> arg) {
8591cb0ef41Sopenharmony_ci          TNode<Word32T> code32 = TruncateTaggedToWord32(context, arg);
8601cb0ef41Sopenharmony_ci          TNode<Word32T> code16 =
8611cb0ef41Sopenharmony_ci              Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
8621cb0ef41Sopenharmony_ci
8631cb0ef41Sopenharmony_ci          TNode<IntPtrT> offset = ElementOffsetFromIndex(
8641cb0ef41Sopenharmony_ci              var_max_index.value(), UINT16_ELEMENTS,
8651cb0ef41Sopenharmony_ci              SeqTwoByteString::kHeaderSize - kHeapObjectTag);
8661cb0ef41Sopenharmony_ci          StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
8671cb0ef41Sopenharmony_ci                              offset, code16);
8681cb0ef41Sopenharmony_ci          var_max_index = IntPtrAdd(var_max_index.value(), IntPtrConstant(1));
8691cb0ef41Sopenharmony_ci        },
8701cb0ef41Sopenharmony_ci        var_max_index.value());
8711cb0ef41Sopenharmony_ci
8721cb0ef41Sopenharmony_ci    arguments.PopAndReturn(two_byte_result);
8731cb0ef41Sopenharmony_ci  }
8741cb0ef41Sopenharmony_ci}
8751cb0ef41Sopenharmony_ci
8761cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::MaybeCallFunctionAtSymbol(
8771cb0ef41Sopenharmony_ci    const TNode<Context> context, const TNode<Object> object,
8781cb0ef41Sopenharmony_ci    const TNode<Object> maybe_string, Handle<Symbol> symbol,
8791cb0ef41Sopenharmony_ci    DescriptorIndexNameValue additional_property_to_check,
8801cb0ef41Sopenharmony_ci    const NodeFunction0& regexp_call, const NodeFunction1& generic_call) {
8811cb0ef41Sopenharmony_ci  Label out(this);
8821cb0ef41Sopenharmony_ci  Label get_property_lookup(this);
8831cb0ef41Sopenharmony_ci
8841cb0ef41Sopenharmony_ci  // Smis have to go through the GetProperty lookup in case Number.prototype or
8851cb0ef41Sopenharmony_ci  // Object.prototype was modified.
8861cb0ef41Sopenharmony_ci  GotoIf(TaggedIsSmi(object), &get_property_lookup);
8871cb0ef41Sopenharmony_ci
8881cb0ef41Sopenharmony_ci  // Take the fast path for RegExps.
8891cb0ef41Sopenharmony_ci  // There's two conditions: {object} needs to be a fast regexp, and
8901cb0ef41Sopenharmony_ci  // {maybe_string} must be a string (we can't call ToString on the fast path
8911cb0ef41Sopenharmony_ci  // since it may mutate {object}).
8921cb0ef41Sopenharmony_ci  {
8931cb0ef41Sopenharmony_ci    Label stub_call(this), slow_lookup(this);
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_ci    TNode<HeapObject> heap_object = CAST(object);
8961cb0ef41Sopenharmony_ci
8971cb0ef41Sopenharmony_ci    GotoIf(TaggedIsSmi(maybe_string), &slow_lookup);
8981cb0ef41Sopenharmony_ci    GotoIfNot(IsString(CAST(maybe_string)), &slow_lookup);
8991cb0ef41Sopenharmony_ci
9001cb0ef41Sopenharmony_ci    // Note we don't run a full (= permissive) check here, because passing the
9011cb0ef41Sopenharmony_ci    // check implies calling the fast variants of target builtins, which assume
9021cb0ef41Sopenharmony_ci    // we've already made their appropriate fast path checks. This is not the
9031cb0ef41Sopenharmony_ci    // case though; e.g.: some of the target builtins access flag getters.
9041cb0ef41Sopenharmony_ci    // TODO(jgruber): Handle slow flag accesses on the fast path and make this
9051cb0ef41Sopenharmony_ci    // permissive.
9061cb0ef41Sopenharmony_ci    RegExpBuiltinsAssembler regexp_asm(state());
9071cb0ef41Sopenharmony_ci    regexp_asm.BranchIfFastRegExp(
9081cb0ef41Sopenharmony_ci        context, heap_object, LoadMap(heap_object),
9091cb0ef41Sopenharmony_ci        PrototypeCheckAssembler::kCheckPrototypePropertyConstness,
9101cb0ef41Sopenharmony_ci        additional_property_to_check, &stub_call, &slow_lookup);
9111cb0ef41Sopenharmony_ci
9121cb0ef41Sopenharmony_ci    BIND(&stub_call);
9131cb0ef41Sopenharmony_ci    // TODO(jgruber): Add a no-JS scope once it exists.
9141cb0ef41Sopenharmony_ci    regexp_call();
9151cb0ef41Sopenharmony_ci
9161cb0ef41Sopenharmony_ci    BIND(&slow_lookup);
9171cb0ef41Sopenharmony_ci    // Special case null and undefined to skip the property lookup.
9181cb0ef41Sopenharmony_ci    Branch(IsNullOrUndefined(heap_object), &out, &get_property_lookup);
9191cb0ef41Sopenharmony_ci  }
9201cb0ef41Sopenharmony_ci
9211cb0ef41Sopenharmony_ci  // Fall back to a slow lookup of {heap_object[symbol]}.
9221cb0ef41Sopenharmony_ci  //
9231cb0ef41Sopenharmony_ci  // The spec uses GetMethod({heap_object}, {symbol}), which has a few quirks:
9241cb0ef41Sopenharmony_ci  // * null values are turned into undefined, and
9251cb0ef41Sopenharmony_ci  // * an exception is thrown if the value is not undefined, null, or callable.
9261cb0ef41Sopenharmony_ci  // We handle the former by jumping to {out} for null values as well, while
9271cb0ef41Sopenharmony_ci  // the latter is already handled by the Call({maybe_func}) operation.
9281cb0ef41Sopenharmony_ci
9291cb0ef41Sopenharmony_ci  BIND(&get_property_lookup);
9301cb0ef41Sopenharmony_ci  const TNode<Object> maybe_func = GetProperty(context, object, symbol);
9311cb0ef41Sopenharmony_ci  GotoIf(IsUndefined(maybe_func), &out);
9321cb0ef41Sopenharmony_ci  GotoIf(IsNull(maybe_func), &out);
9331cb0ef41Sopenharmony_ci
9341cb0ef41Sopenharmony_ci  // Attempt to call the function.
9351cb0ef41Sopenharmony_ci  generic_call(maybe_func);
9361cb0ef41Sopenharmony_ci
9371cb0ef41Sopenharmony_ci  BIND(&out);
9381cb0ef41Sopenharmony_ci}
9391cb0ef41Sopenharmony_ci
9401cb0ef41Sopenharmony_ciconst TNode<Smi> StringBuiltinsAssembler::IndexOfDollarChar(
9411cb0ef41Sopenharmony_ci    const TNode<Context> context, const TNode<String> string) {
9421cb0ef41Sopenharmony_ci  const TNode<String> dollar_string = HeapConstant(
9431cb0ef41Sopenharmony_ci      isolate()->factory()->LookupSingleCharacterStringFromCode('$'));
9441cb0ef41Sopenharmony_ci  const TNode<Smi> dollar_ix = CAST(CallBuiltin(
9451cb0ef41Sopenharmony_ci      Builtin::kStringIndexOf, context, string, dollar_string, SmiConstant(0)));
9461cb0ef41Sopenharmony_ci  return dollar_ix;
9471cb0ef41Sopenharmony_ci}
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::GetSubstitution(
9501cb0ef41Sopenharmony_ci    TNode<Context> context, TNode<String> subject_string,
9511cb0ef41Sopenharmony_ci    TNode<Smi> match_start_index, TNode<Smi> match_end_index,
9521cb0ef41Sopenharmony_ci    TNode<String> replace_string) {
9531cb0ef41Sopenharmony_ci  CSA_DCHECK(this, TaggedIsPositiveSmi(match_start_index));
9541cb0ef41Sopenharmony_ci  CSA_DCHECK(this, TaggedIsPositiveSmi(match_end_index));
9551cb0ef41Sopenharmony_ci
9561cb0ef41Sopenharmony_ci  TVARIABLE(String, var_result, replace_string);
9571cb0ef41Sopenharmony_ci  Label runtime(this), out(this);
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_ci  // In this primitive implementation we simply look for the next '$' char in
9601cb0ef41Sopenharmony_ci  // {replace_string}. If it doesn't exist, we can simply return
9611cb0ef41Sopenharmony_ci  // {replace_string} itself. If it does, then we delegate to
9621cb0ef41Sopenharmony_ci  // String::GetSubstitution, passing in the index of the first '$' to avoid
9631cb0ef41Sopenharmony_ci  // repeated scanning work.
9641cb0ef41Sopenharmony_ci  // TODO(jgruber): Possibly extend this in the future to handle more complex
9651cb0ef41Sopenharmony_ci  // cases without runtime calls.
9661cb0ef41Sopenharmony_ci
9671cb0ef41Sopenharmony_ci  const TNode<Smi> dollar_index = IndexOfDollarChar(context, replace_string);
9681cb0ef41Sopenharmony_ci  Branch(SmiIsNegative(dollar_index), &out, &runtime);
9691cb0ef41Sopenharmony_ci
9701cb0ef41Sopenharmony_ci  BIND(&runtime);
9711cb0ef41Sopenharmony_ci  {
9721cb0ef41Sopenharmony_ci    CSA_DCHECK(this, TaggedIsPositiveSmi(dollar_index));
9731cb0ef41Sopenharmony_ci
9741cb0ef41Sopenharmony_ci    const TNode<Object> matched =
9751cb0ef41Sopenharmony_ci        CallBuiltin(Builtin::kStringSubstring, context, subject_string,
9761cb0ef41Sopenharmony_ci                    SmiUntag(match_start_index), SmiUntag(match_end_index));
9771cb0ef41Sopenharmony_ci    const TNode<String> replacement_string = CAST(
9781cb0ef41Sopenharmony_ci        CallRuntime(Runtime::kGetSubstitution, context, matched, subject_string,
9791cb0ef41Sopenharmony_ci                    match_start_index, replace_string, dollar_index));
9801cb0ef41Sopenharmony_ci    var_result = replacement_string;
9811cb0ef41Sopenharmony_ci
9821cb0ef41Sopenharmony_ci    Goto(&out);
9831cb0ef41Sopenharmony_ci  }
9841cb0ef41Sopenharmony_ci
9851cb0ef41Sopenharmony_ci  BIND(&out);
9861cb0ef41Sopenharmony_ci  return var_result.value();
9871cb0ef41Sopenharmony_ci}
9881cb0ef41Sopenharmony_ci
9891cb0ef41Sopenharmony_ci// ES6 #sec-string.prototype.replace
9901cb0ef41Sopenharmony_ciTF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
9911cb0ef41Sopenharmony_ci  Label out(this);
9921cb0ef41Sopenharmony_ci
9931cb0ef41Sopenharmony_ci  auto receiver = Parameter<Object>(Descriptor::kReceiver);
9941cb0ef41Sopenharmony_ci  const auto search = Parameter<Object>(Descriptor::kSearch);
9951cb0ef41Sopenharmony_ci  const auto replace = Parameter<Object>(Descriptor::kReplace);
9961cb0ef41Sopenharmony_ci  auto context = Parameter<Context>(Descriptor::kContext);
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ci  const TNode<Smi> smi_zero = SmiConstant(0);
9991cb0ef41Sopenharmony_ci
10001cb0ef41Sopenharmony_ci  RequireObjectCoercible(context, receiver, "String.prototype.replace");
10011cb0ef41Sopenharmony_ci
10021cb0ef41Sopenharmony_ci  // Redirect to replacer method if {search[@@replace]} is not undefined.
10031cb0ef41Sopenharmony_ci
10041cb0ef41Sopenharmony_ci  MaybeCallFunctionAtSymbol(
10051cb0ef41Sopenharmony_ci      context, search, receiver, isolate()->factory()->replace_symbol(),
10061cb0ef41Sopenharmony_ci      DescriptorIndexNameValue{JSRegExp::kSymbolReplaceFunctionDescriptorIndex,
10071cb0ef41Sopenharmony_ci                               RootIndex::kreplace_symbol,
10081cb0ef41Sopenharmony_ci                               Context::REGEXP_REPLACE_FUNCTION_INDEX},
10091cb0ef41Sopenharmony_ci      [=]() {
10101cb0ef41Sopenharmony_ci        Return(CallBuiltin(Builtin::kRegExpReplace, context, search, receiver,
10111cb0ef41Sopenharmony_ci                           replace));
10121cb0ef41Sopenharmony_ci      },
10131cb0ef41Sopenharmony_ci      [=](TNode<Object> fn) {
10141cb0ef41Sopenharmony_ci        Return(Call(context, fn, search, receiver, replace));
10151cb0ef41Sopenharmony_ci      });
10161cb0ef41Sopenharmony_ci
10171cb0ef41Sopenharmony_ci  // Convert {receiver} and {search} to strings.
10181cb0ef41Sopenharmony_ci
10191cb0ef41Sopenharmony_ci  const TNode<String> subject_string = ToString_Inline(context, receiver);
10201cb0ef41Sopenharmony_ci  const TNode<String> search_string = ToString_Inline(context, search);
10211cb0ef41Sopenharmony_ci
10221cb0ef41Sopenharmony_ci  const TNode<IntPtrT> subject_length = LoadStringLengthAsWord(subject_string);
10231cb0ef41Sopenharmony_ci  const TNode<IntPtrT> search_length = LoadStringLengthAsWord(search_string);
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci  // Fast-path single-char {search}, long cons {receiver}, and simple string
10261cb0ef41Sopenharmony_ci  // {replace}.
10271cb0ef41Sopenharmony_ci  {
10281cb0ef41Sopenharmony_ci    Label next(this);
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ci    GotoIfNot(WordEqual(search_length, IntPtrConstant(1)), &next);
10311cb0ef41Sopenharmony_ci    GotoIfNot(IntPtrGreaterThan(subject_length, IntPtrConstant(0xFF)), &next);
10321cb0ef41Sopenharmony_ci    GotoIf(TaggedIsSmi(replace), &next);
10331cb0ef41Sopenharmony_ci    GotoIfNot(IsString(CAST(replace)), &next);
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ci    TNode<String> replace_string = CAST(replace);
10361cb0ef41Sopenharmony_ci    const TNode<Uint16T> subject_instance_type =
10371cb0ef41Sopenharmony_ci        LoadInstanceType(subject_string);
10381cb0ef41Sopenharmony_ci    GotoIfNot(IsConsStringInstanceType(subject_instance_type), &next);
10391cb0ef41Sopenharmony_ci
10401cb0ef41Sopenharmony_ci    GotoIf(TaggedIsPositiveSmi(IndexOfDollarChar(context, replace_string)),
10411cb0ef41Sopenharmony_ci           &next);
10421cb0ef41Sopenharmony_ci
10431cb0ef41Sopenharmony_ci    // Searching by traversing a cons string tree and replace with cons of
10441cb0ef41Sopenharmony_ci    // slices works only when the replaced string is a single character, being
10451cb0ef41Sopenharmony_ci    // replaced by a simple string and only pays off for long strings.
10461cb0ef41Sopenharmony_ci    // TODO(jgruber): Reevaluate if this is still beneficial.
10471cb0ef41Sopenharmony_ci    // TODO(jgruber): TailCallRuntime when it correctly handles adapter frames.
10481cb0ef41Sopenharmony_ci    Return(CallRuntime(Runtime::kStringReplaceOneCharWithString, context,
10491cb0ef41Sopenharmony_ci                       subject_string, search_string, replace_string));
10501cb0ef41Sopenharmony_ci
10511cb0ef41Sopenharmony_ci    BIND(&next);
10521cb0ef41Sopenharmony_ci  }
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci  // TODO(jgruber): Extend StringIndexOf to handle two-byte strings and
10551cb0ef41Sopenharmony_ci  // longer substrings - we can handle up to 8 chars (one-byte) / 4 chars
10561cb0ef41Sopenharmony_ci  // (2-byte).
10571cb0ef41Sopenharmony_ci
10581cb0ef41Sopenharmony_ci  const TNode<Smi> match_start_index =
10591cb0ef41Sopenharmony_ci      CAST(CallBuiltin(Builtin::kStringIndexOf, context, subject_string,
10601cb0ef41Sopenharmony_ci                       search_string, smi_zero));
10611cb0ef41Sopenharmony_ci
10621cb0ef41Sopenharmony_ci  // Early exit if no match found.
10631cb0ef41Sopenharmony_ci  {
10641cb0ef41Sopenharmony_ci    Label next(this), return_subject(this);
10651cb0ef41Sopenharmony_ci
10661cb0ef41Sopenharmony_ci    GotoIfNot(SmiIsNegative(match_start_index), &next);
10671cb0ef41Sopenharmony_ci
10681cb0ef41Sopenharmony_ci    // The spec requires to perform ToString(replace) if the {replace} is not
10691cb0ef41Sopenharmony_ci    // callable even if we are going to exit here.
10701cb0ef41Sopenharmony_ci    // Since ToString() being applied to Smi does not have side effects for
10711cb0ef41Sopenharmony_ci    // numbers we can skip it.
10721cb0ef41Sopenharmony_ci    GotoIf(TaggedIsSmi(replace), &return_subject);
10731cb0ef41Sopenharmony_ci    GotoIf(IsCallableMap(LoadMap(CAST(replace))), &return_subject);
10741cb0ef41Sopenharmony_ci
10751cb0ef41Sopenharmony_ci    // TODO(jgruber): Could introduce ToStringSideeffectsStub which only
10761cb0ef41Sopenharmony_ci    // performs observable parts of ToString.
10771cb0ef41Sopenharmony_ci    ToString_Inline(context, replace);
10781cb0ef41Sopenharmony_ci    Goto(&return_subject);
10791cb0ef41Sopenharmony_ci
10801cb0ef41Sopenharmony_ci    BIND(&return_subject);
10811cb0ef41Sopenharmony_ci    Return(subject_string);
10821cb0ef41Sopenharmony_ci
10831cb0ef41Sopenharmony_ci    BIND(&next);
10841cb0ef41Sopenharmony_ci  }
10851cb0ef41Sopenharmony_ci
10861cb0ef41Sopenharmony_ci  const TNode<Smi> match_end_index =
10871cb0ef41Sopenharmony_ci      SmiAdd(match_start_index, SmiFromIntPtr(search_length));
10881cb0ef41Sopenharmony_ci
10891cb0ef41Sopenharmony_ci  TVARIABLE(String, var_result, EmptyStringConstant());
10901cb0ef41Sopenharmony_ci
10911cb0ef41Sopenharmony_ci  // Compute the prefix.
10921cb0ef41Sopenharmony_ci  {
10931cb0ef41Sopenharmony_ci    Label next(this);
10941cb0ef41Sopenharmony_ci
10951cb0ef41Sopenharmony_ci    GotoIf(SmiEqual(match_start_index, smi_zero), &next);
10961cb0ef41Sopenharmony_ci    const TNode<String> prefix =
10971cb0ef41Sopenharmony_ci        CAST(CallBuiltin(Builtin::kStringSubstring, context, subject_string,
10981cb0ef41Sopenharmony_ci                         IntPtrConstant(0), SmiUntag(match_start_index)));
10991cb0ef41Sopenharmony_ci    var_result = prefix;
11001cb0ef41Sopenharmony_ci
11011cb0ef41Sopenharmony_ci    Goto(&next);
11021cb0ef41Sopenharmony_ci    BIND(&next);
11031cb0ef41Sopenharmony_ci  }
11041cb0ef41Sopenharmony_ci
11051cb0ef41Sopenharmony_ci  // Compute the string to replace with.
11061cb0ef41Sopenharmony_ci
11071cb0ef41Sopenharmony_ci  Label if_iscallablereplace(this), if_notcallablereplace(this);
11081cb0ef41Sopenharmony_ci  GotoIf(TaggedIsSmi(replace), &if_notcallablereplace);
11091cb0ef41Sopenharmony_ci  Branch(IsCallableMap(LoadMap(CAST(replace))), &if_iscallablereplace,
11101cb0ef41Sopenharmony_ci         &if_notcallablereplace);
11111cb0ef41Sopenharmony_ci
11121cb0ef41Sopenharmony_ci  BIND(&if_iscallablereplace);
11131cb0ef41Sopenharmony_ci  {
11141cb0ef41Sopenharmony_ci    const TNode<Object> replacement =
11151cb0ef41Sopenharmony_ci        Call(context, replace, UndefinedConstant(), search_string,
11161cb0ef41Sopenharmony_ci             match_start_index, subject_string);
11171cb0ef41Sopenharmony_ci    const TNode<String> replacement_string =
11181cb0ef41Sopenharmony_ci        ToString_Inline(context, replacement);
11191cb0ef41Sopenharmony_ci    var_result = CAST(CallBuiltin(Builtin::kStringAdd_CheckNone, context,
11201cb0ef41Sopenharmony_ci                                  var_result.value(), replacement_string));
11211cb0ef41Sopenharmony_ci    Goto(&out);
11221cb0ef41Sopenharmony_ci  }
11231cb0ef41Sopenharmony_ci
11241cb0ef41Sopenharmony_ci  BIND(&if_notcallablereplace);
11251cb0ef41Sopenharmony_ci  {
11261cb0ef41Sopenharmony_ci    const TNode<String> replace_string = ToString_Inline(context, replace);
11271cb0ef41Sopenharmony_ci    const TNode<Object> replacement =
11281cb0ef41Sopenharmony_ci        GetSubstitution(context, subject_string, match_start_index,
11291cb0ef41Sopenharmony_ci                        match_end_index, replace_string);
11301cb0ef41Sopenharmony_ci    var_result = CAST(CallBuiltin(Builtin::kStringAdd_CheckNone, context,
11311cb0ef41Sopenharmony_ci                                  var_result.value(), replacement));
11321cb0ef41Sopenharmony_ci    Goto(&out);
11331cb0ef41Sopenharmony_ci  }
11341cb0ef41Sopenharmony_ci
11351cb0ef41Sopenharmony_ci  BIND(&out);
11361cb0ef41Sopenharmony_ci  {
11371cb0ef41Sopenharmony_ci    const TNode<Object> suffix =
11381cb0ef41Sopenharmony_ci        CallBuiltin(Builtin::kStringSubstring, context, subject_string,
11391cb0ef41Sopenharmony_ci                    SmiUntag(match_end_index), subject_length);
11401cb0ef41Sopenharmony_ci    const TNode<Object> result = CallBuiltin(
11411cb0ef41Sopenharmony_ci        Builtin::kStringAdd_CheckNone, context, var_result.value(), suffix);
11421cb0ef41Sopenharmony_ci    Return(result);
11431cb0ef41Sopenharmony_ci  }
11441cb0ef41Sopenharmony_ci}
11451cb0ef41Sopenharmony_ci
11461cb0ef41Sopenharmony_ci// ES #sec-string.prototype.matchAll
11471cb0ef41Sopenharmony_ciTF_BUILTIN(StringPrototypeMatchAll, StringBuiltinsAssembler) {
11481cb0ef41Sopenharmony_ci  char const* method_name = "String.prototype.matchAll";
11491cb0ef41Sopenharmony_ci
11501cb0ef41Sopenharmony_ci  auto context = Parameter<Context>(Descriptor::kContext);
11511cb0ef41Sopenharmony_ci  auto maybe_regexp = Parameter<Object>(Descriptor::kRegexp);
11521cb0ef41Sopenharmony_ci  auto receiver = Parameter<Object>(Descriptor::kReceiver);
11531cb0ef41Sopenharmony_ci  TNode<NativeContext> native_context = LoadNativeContext(context);
11541cb0ef41Sopenharmony_ci
11551cb0ef41Sopenharmony_ci  // 1. Let O be ? RequireObjectCoercible(this value).
11561cb0ef41Sopenharmony_ci  RequireObjectCoercible(context, receiver, method_name);
11571cb0ef41Sopenharmony_ci
11581cb0ef41Sopenharmony_ci  RegExpMatchAllAssembler regexp_asm(state());
11591cb0ef41Sopenharmony_ci  {
11601cb0ef41Sopenharmony_ci    Label fast(this), slow(this, Label::kDeferred),
11611cb0ef41Sopenharmony_ci        throw_exception(this, Label::kDeferred),
11621cb0ef41Sopenharmony_ci        throw_flags_exception(this, Label::kDeferred), next(this);
11631cb0ef41Sopenharmony_ci
11641cb0ef41Sopenharmony_ci    // 2. If regexp is neither undefined nor null, then
11651cb0ef41Sopenharmony_ci    //   a. Let isRegExp be ? IsRegExp(regexp).
11661cb0ef41Sopenharmony_ci    //   b. If isRegExp is true, then
11671cb0ef41Sopenharmony_ci    //     i. Let flags be ? Get(regexp, "flags").
11681cb0ef41Sopenharmony_ci    //    ii. Perform ? RequireObjectCoercible(flags).
11691cb0ef41Sopenharmony_ci    //   iii. If ? ToString(flags) does not contain "g", throw a
11701cb0ef41Sopenharmony_ci    //        TypeError exception.
11711cb0ef41Sopenharmony_ci    GotoIf(TaggedIsSmi(maybe_regexp), &next);
11721cb0ef41Sopenharmony_ci    TNode<HeapObject> heap_maybe_regexp = CAST(maybe_regexp);
11731cb0ef41Sopenharmony_ci    regexp_asm.BranchIfFastRegExpForMatch(context, heap_maybe_regexp, &fast,
11741cb0ef41Sopenharmony_ci                                          &slow);
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci    BIND(&fast);
11771cb0ef41Sopenharmony_ci    {
11781cb0ef41Sopenharmony_ci      TNode<BoolT> is_global = regexp_asm.FlagGetter(context, heap_maybe_regexp,
11791cb0ef41Sopenharmony_ci                                                     JSRegExp::kGlobal, true);
11801cb0ef41Sopenharmony_ci      Branch(is_global, &next, &throw_exception);
11811cb0ef41Sopenharmony_ci    }
11821cb0ef41Sopenharmony_ci
11831cb0ef41Sopenharmony_ci    BIND(&slow);
11841cb0ef41Sopenharmony_ci    {
11851cb0ef41Sopenharmony_ci      GotoIfNot(regexp_asm.IsRegExp(native_context, heap_maybe_regexp), &next);
11861cb0ef41Sopenharmony_ci
11871cb0ef41Sopenharmony_ci      TNode<Object> flags = GetProperty(context, heap_maybe_regexp,
11881cb0ef41Sopenharmony_ci                                        isolate()->factory()->flags_string());
11891cb0ef41Sopenharmony_ci      // TODO(syg): Implement a RequireObjectCoercible with more flexible error
11901cb0ef41Sopenharmony_ci      // messages.
11911cb0ef41Sopenharmony_ci      GotoIf(IsNullOrUndefined(flags), &throw_flags_exception);
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ci      TNode<String> flags_string = ToString_Inline(context, flags);
11941cb0ef41Sopenharmony_ci      TNode<String> global_char_string = StringConstant("g");
11951cb0ef41Sopenharmony_ci      TNode<Smi> global_ix =
11961cb0ef41Sopenharmony_ci          CAST(CallBuiltin(Builtin::kStringIndexOf, context, flags_string,
11971cb0ef41Sopenharmony_ci                           global_char_string, SmiConstant(0)));
11981cb0ef41Sopenharmony_ci      Branch(SmiEqual(global_ix, SmiConstant(-1)), &throw_exception, &next);
11991cb0ef41Sopenharmony_ci    }
12001cb0ef41Sopenharmony_ci
12011cb0ef41Sopenharmony_ci    BIND(&throw_exception);
12021cb0ef41Sopenharmony_ci    ThrowTypeError(context, MessageTemplate::kRegExpGlobalInvokedOnNonGlobal,
12031cb0ef41Sopenharmony_ci                   method_name);
12041cb0ef41Sopenharmony_ci
12051cb0ef41Sopenharmony_ci    BIND(&throw_flags_exception);
12061cb0ef41Sopenharmony_ci    ThrowTypeError(context,
12071cb0ef41Sopenharmony_ci                   MessageTemplate::kStringMatchAllNullOrUndefinedFlags);
12081cb0ef41Sopenharmony_ci
12091cb0ef41Sopenharmony_ci    BIND(&next);
12101cb0ef41Sopenharmony_ci  }
12111cb0ef41Sopenharmony_ci  //   a. Let matcher be ? GetMethod(regexp, @@matchAll).
12121cb0ef41Sopenharmony_ci  //   b. If matcher is not undefined, then
12131cb0ef41Sopenharmony_ci  //     i. Return ? Call(matcher, regexp, « O »).
12141cb0ef41Sopenharmony_ci  auto if_regexp_call = [&] {
12151cb0ef41Sopenharmony_ci    // MaybeCallFunctionAtSymbol guarantees fast path is chosen only if
12161cb0ef41Sopenharmony_ci    // maybe_regexp is a fast regexp and receiver is a string.
12171cb0ef41Sopenharmony_ci    TNode<String> s = CAST(receiver);
12181cb0ef41Sopenharmony_ci
12191cb0ef41Sopenharmony_ci    Return(
12201cb0ef41Sopenharmony_ci        RegExpPrototypeMatchAllImpl(context, native_context, maybe_regexp, s));
12211cb0ef41Sopenharmony_ci  };
12221cb0ef41Sopenharmony_ci  auto if_generic_call = [=](TNode<Object> fn) {
12231cb0ef41Sopenharmony_ci    Return(Call(context, fn, maybe_regexp, receiver));
12241cb0ef41Sopenharmony_ci  };
12251cb0ef41Sopenharmony_ci  MaybeCallFunctionAtSymbol(
12261cb0ef41Sopenharmony_ci      context, maybe_regexp, receiver, isolate()->factory()->match_all_symbol(),
12271cb0ef41Sopenharmony_ci      DescriptorIndexNameValue{JSRegExp::kSymbolMatchAllFunctionDescriptorIndex,
12281cb0ef41Sopenharmony_ci                               RootIndex::kmatch_all_symbol,
12291cb0ef41Sopenharmony_ci                               Context::REGEXP_MATCH_ALL_FUNCTION_INDEX},
12301cb0ef41Sopenharmony_ci      if_regexp_call, if_generic_call);
12311cb0ef41Sopenharmony_ci
12321cb0ef41Sopenharmony_ci  // 3. Let S be ? ToString(O).
12331cb0ef41Sopenharmony_ci  TNode<String> s = ToString_Inline(context, receiver);
12341cb0ef41Sopenharmony_ci
12351cb0ef41Sopenharmony_ci  // 4. Let rx be ? RegExpCreate(R, "g").
12361cb0ef41Sopenharmony_ci  TNode<Object> rx = regexp_asm.RegExpCreate(context, native_context,
12371cb0ef41Sopenharmony_ci                                             maybe_regexp, StringConstant("g"));
12381cb0ef41Sopenharmony_ci
12391cb0ef41Sopenharmony_ci  // 5. Return ? Invoke(rx, @@matchAll, « S »).
12401cb0ef41Sopenharmony_ci  TNode<Object> match_all_func =
12411cb0ef41Sopenharmony_ci      GetProperty(context, rx, isolate()->factory()->match_all_symbol());
12421cb0ef41Sopenharmony_ci  Return(Call(context, match_all_func, rx, s));
12431cb0ef41Sopenharmony_ci}
12441cb0ef41Sopenharmony_ci
12451cb0ef41Sopenharmony_ciTNode<JSArray> StringBuiltinsAssembler::StringToArray(
12461cb0ef41Sopenharmony_ci    TNode<NativeContext> context, TNode<String> subject_string,
12471cb0ef41Sopenharmony_ci    TNode<Smi> subject_length, TNode<Number> limit_number) {
12481cb0ef41Sopenharmony_ci  CSA_DCHECK(this, SmiGreaterThan(subject_length, SmiConstant(0)));
12491cb0ef41Sopenharmony_ci
12501cb0ef41Sopenharmony_ci  Label done(this), call_runtime(this, Label::kDeferred),
12511cb0ef41Sopenharmony_ci      fill_thehole_and_call_runtime(this, Label::kDeferred);
12521cb0ef41Sopenharmony_ci  TVARIABLE(JSArray, result_array);
12531cb0ef41Sopenharmony_ci
12541cb0ef41Sopenharmony_ci  TNode<Uint16T> instance_type = LoadInstanceType(subject_string);
12551cb0ef41Sopenharmony_ci  GotoIfNot(IsOneByteStringInstanceType(instance_type), &call_runtime);
12561cb0ef41Sopenharmony_ci
12571cb0ef41Sopenharmony_ci  // Try to use cached one byte characters.
12581cb0ef41Sopenharmony_ci  {
12591cb0ef41Sopenharmony_ci    TNode<Smi> length_smi =
12601cb0ef41Sopenharmony_ci        Select<Smi>(TaggedIsSmi(limit_number),
12611cb0ef41Sopenharmony_ci                    [=] { return SmiMin(CAST(limit_number), subject_length); },
12621cb0ef41Sopenharmony_ci                    [=] { return subject_length; });
12631cb0ef41Sopenharmony_ci    TNode<IntPtrT> length = SmiToIntPtr(length_smi);
12641cb0ef41Sopenharmony_ci
12651cb0ef41Sopenharmony_ci    ToDirectStringAssembler to_direct(state(), subject_string);
12661cb0ef41Sopenharmony_ci    to_direct.TryToDirect(&call_runtime);
12671cb0ef41Sopenharmony_ci
12681cb0ef41Sopenharmony_ci    // The extracted direct string may be two-byte even though the wrapping
12691cb0ef41Sopenharmony_ci    // string is one-byte.
12701cb0ef41Sopenharmony_ci    GotoIfNot(IsOneByteStringInstanceType(to_direct.instance_type()),
12711cb0ef41Sopenharmony_ci              &call_runtime);
12721cb0ef41Sopenharmony_ci
12731cb0ef41Sopenharmony_ci    TNode<FixedArray> elements = CAST(AllocateFixedArray(
12741cb0ef41Sopenharmony_ci        PACKED_ELEMENTS, length, AllocationFlag::kAllowLargeObjectAllocation));
12751cb0ef41Sopenharmony_ci    // Don't allocate anything while {string_data} is live!
12761cb0ef41Sopenharmony_ci    TNode<RawPtrT> string_data =
12771cb0ef41Sopenharmony_ci        to_direct.PointerToData(&fill_thehole_and_call_runtime);
12781cb0ef41Sopenharmony_ci    TNode<IntPtrT> string_data_offset = to_direct.offset();
12791cb0ef41Sopenharmony_ci    TNode<FixedArray> cache = SingleCharacterStringCacheConstant();
12801cb0ef41Sopenharmony_ci
12811cb0ef41Sopenharmony_ci    BuildFastLoop<IntPtrT>(
12821cb0ef41Sopenharmony_ci        IntPtrConstant(0), length,
12831cb0ef41Sopenharmony_ci        [&](TNode<IntPtrT> index) {
12841cb0ef41Sopenharmony_ci          // TODO(jkummerow): Implement a CSA version of
12851cb0ef41Sopenharmony_ci          // DisallowGarbageCollection and use that to guard
12861cb0ef41Sopenharmony_ci          // ToDirectStringAssembler.PointerToData().
12871cb0ef41Sopenharmony_ci          CSA_DCHECK(this, WordEqual(to_direct.PointerToData(&call_runtime),
12881cb0ef41Sopenharmony_ci                                     string_data));
12891cb0ef41Sopenharmony_ci          TNode<Int32T> char_code =
12901cb0ef41Sopenharmony_ci              UncheckedCast<Int32T>(Load(MachineType::Uint8(), string_data,
12911cb0ef41Sopenharmony_ci                                         IntPtrAdd(index, string_data_offset)));
12921cb0ef41Sopenharmony_ci          TNode<UintPtrT> code_index = ChangeUint32ToWord(char_code);
12931cb0ef41Sopenharmony_ci          TNode<Object> entry = LoadFixedArrayElement(cache, code_index);
12941cb0ef41Sopenharmony_ci
12951cb0ef41Sopenharmony_ci          // If we cannot find a char in the cache, fill the hole for the fixed
12961cb0ef41Sopenharmony_ci          // array, and call runtime.
12971cb0ef41Sopenharmony_ci          GotoIf(IsUndefined(entry), &fill_thehole_and_call_runtime);
12981cb0ef41Sopenharmony_ci
12991cb0ef41Sopenharmony_ci          StoreFixedArrayElement(elements, index, entry);
13001cb0ef41Sopenharmony_ci        },
13011cb0ef41Sopenharmony_ci        1, IndexAdvanceMode::kPost);
13021cb0ef41Sopenharmony_ci
13031cb0ef41Sopenharmony_ci    TNode<Map> array_map = LoadJSArrayElementsMap(PACKED_ELEMENTS, context);
13041cb0ef41Sopenharmony_ci    result_array = AllocateJSArray(array_map, elements, length_smi);
13051cb0ef41Sopenharmony_ci    Goto(&done);
13061cb0ef41Sopenharmony_ci
13071cb0ef41Sopenharmony_ci    BIND(&fill_thehole_and_call_runtime);
13081cb0ef41Sopenharmony_ci    {
13091cb0ef41Sopenharmony_ci      FillFixedArrayWithValue(PACKED_ELEMENTS, elements, IntPtrConstant(0),
13101cb0ef41Sopenharmony_ci                              length, RootIndex::kTheHoleValue);
13111cb0ef41Sopenharmony_ci      Goto(&call_runtime);
13121cb0ef41Sopenharmony_ci    }
13131cb0ef41Sopenharmony_ci  }
13141cb0ef41Sopenharmony_ci
13151cb0ef41Sopenharmony_ci  BIND(&call_runtime);
13161cb0ef41Sopenharmony_ci  {
13171cb0ef41Sopenharmony_ci    result_array = CAST(CallRuntime(Runtime::kStringToArray, context,
13181cb0ef41Sopenharmony_ci                                    subject_string, limit_number));
13191cb0ef41Sopenharmony_ci    Goto(&done);
13201cb0ef41Sopenharmony_ci  }
13211cb0ef41Sopenharmony_ci
13221cb0ef41Sopenharmony_ci  BIND(&done);
13231cb0ef41Sopenharmony_ci  return result_array.value();
13241cb0ef41Sopenharmony_ci}
13251cb0ef41Sopenharmony_ci
13261cb0ef41Sopenharmony_ci// ES6 section 21.1.3.19 String.prototype.split ( separator, limit )
13271cb0ef41Sopenharmony_ciTF_BUILTIN(StringPrototypeSplit, StringBuiltinsAssembler) {
13281cb0ef41Sopenharmony_ci  const int kSeparatorArg = 0;
13291cb0ef41Sopenharmony_ci  const int kLimitArg = 1;
13301cb0ef41Sopenharmony_ci
13311cb0ef41Sopenharmony_ci  const TNode<IntPtrT> argc = ChangeInt32ToIntPtr(
13321cb0ef41Sopenharmony_ci      UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount));
13331cb0ef41Sopenharmony_ci  CodeStubArguments args(this, argc);
13341cb0ef41Sopenharmony_ci
13351cb0ef41Sopenharmony_ci  TNode<Object> receiver = args.GetReceiver();
13361cb0ef41Sopenharmony_ci  const TNode<Object> separator = args.GetOptionalArgumentValue(kSeparatorArg);
13371cb0ef41Sopenharmony_ci  const TNode<Object> limit = args.GetOptionalArgumentValue(kLimitArg);
13381cb0ef41Sopenharmony_ci  auto context = Parameter<NativeContext>(Descriptor::kContext);
13391cb0ef41Sopenharmony_ci
13401cb0ef41Sopenharmony_ci  TNode<Smi> smi_zero = SmiConstant(0);
13411cb0ef41Sopenharmony_ci
13421cb0ef41Sopenharmony_ci  RequireObjectCoercible(context, receiver, "String.prototype.split");
13431cb0ef41Sopenharmony_ci
13441cb0ef41Sopenharmony_ci  // Redirect to splitter method if {separator[@@split]} is not undefined.
13451cb0ef41Sopenharmony_ci
13461cb0ef41Sopenharmony_ci  MaybeCallFunctionAtSymbol(
13471cb0ef41Sopenharmony_ci      context, separator, receiver, isolate()->factory()->split_symbol(),
13481cb0ef41Sopenharmony_ci      DescriptorIndexNameValue{JSRegExp::kSymbolSplitFunctionDescriptorIndex,
13491cb0ef41Sopenharmony_ci                               RootIndex::ksplit_symbol,
13501cb0ef41Sopenharmony_ci                               Context::REGEXP_SPLIT_FUNCTION_INDEX},
13511cb0ef41Sopenharmony_ci      [&]() {
13521cb0ef41Sopenharmony_ci        args.PopAndReturn(CallBuiltin(Builtin::kRegExpSplit, context, separator,
13531cb0ef41Sopenharmony_ci                                      receiver, limit));
13541cb0ef41Sopenharmony_ci      },
13551cb0ef41Sopenharmony_ci      [&](TNode<Object> fn) {
13561cb0ef41Sopenharmony_ci        args.PopAndReturn(Call(context, fn, separator, receiver, limit));
13571cb0ef41Sopenharmony_ci      });
13581cb0ef41Sopenharmony_ci
13591cb0ef41Sopenharmony_ci  // String and integer conversions.
13601cb0ef41Sopenharmony_ci
13611cb0ef41Sopenharmony_ci  TNode<String> subject_string = ToString_Inline(context, receiver);
13621cb0ef41Sopenharmony_ci  TNode<Number> limit_number = Select<Number>(
13631cb0ef41Sopenharmony_ci      IsUndefined(limit), [=] { return NumberConstant(kMaxUInt32); },
13641cb0ef41Sopenharmony_ci      [=] { return ToUint32(context, limit); });
13651cb0ef41Sopenharmony_ci  const TNode<String> separator_string = ToString_Inline(context, separator);
13661cb0ef41Sopenharmony_ci
13671cb0ef41Sopenharmony_ci  Label return_empty_array(this);
13681cb0ef41Sopenharmony_ci
13691cb0ef41Sopenharmony_ci  // Shortcut for {limit} == 0.
13701cb0ef41Sopenharmony_ci  GotoIf(TaggedEqual(limit_number, smi_zero), &return_empty_array);
13711cb0ef41Sopenharmony_ci
13721cb0ef41Sopenharmony_ci  // ECMA-262 says that if {separator} is undefined, the result should
13731cb0ef41Sopenharmony_ci  // be an array of size 1 containing the entire string.
13741cb0ef41Sopenharmony_ci  {
13751cb0ef41Sopenharmony_ci    Label next(this);
13761cb0ef41Sopenharmony_ci    GotoIfNot(IsUndefined(separator), &next);
13771cb0ef41Sopenharmony_ci
13781cb0ef41Sopenharmony_ci    const ElementsKind kind = PACKED_ELEMENTS;
13791cb0ef41Sopenharmony_ci    const TNode<NativeContext> native_context = LoadNativeContext(context);
13801cb0ef41Sopenharmony_ci    TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
13811cb0ef41Sopenharmony_ci
13821cb0ef41Sopenharmony_ci    TNode<Smi> length = SmiConstant(1);
13831cb0ef41Sopenharmony_ci    TNode<IntPtrT> capacity = IntPtrConstant(1);
13841cb0ef41Sopenharmony_ci    TNode<JSArray> result = AllocateJSArray(kind, array_map, capacity, length);
13851cb0ef41Sopenharmony_ci
13861cb0ef41Sopenharmony_ci    TNode<FixedArray> fixed_array = CAST(LoadElements(result));
13871cb0ef41Sopenharmony_ci    StoreFixedArrayElement(fixed_array, 0, subject_string);
13881cb0ef41Sopenharmony_ci
13891cb0ef41Sopenharmony_ci    args.PopAndReturn(result);
13901cb0ef41Sopenharmony_ci
13911cb0ef41Sopenharmony_ci    BIND(&next);
13921cb0ef41Sopenharmony_ci  }
13931cb0ef41Sopenharmony_ci
13941cb0ef41Sopenharmony_ci  // If the separator string is empty then return the elements in the subject.
13951cb0ef41Sopenharmony_ci  {
13961cb0ef41Sopenharmony_ci    Label next(this);
13971cb0ef41Sopenharmony_ci    GotoIfNot(SmiEqual(LoadStringLengthAsSmi(separator_string), smi_zero),
13981cb0ef41Sopenharmony_ci              &next);
13991cb0ef41Sopenharmony_ci
14001cb0ef41Sopenharmony_ci    TNode<Smi> subject_length = LoadStringLengthAsSmi(subject_string);
14011cb0ef41Sopenharmony_ci    GotoIf(SmiEqual(subject_length, smi_zero), &return_empty_array);
14021cb0ef41Sopenharmony_ci
14031cb0ef41Sopenharmony_ci    args.PopAndReturn(
14041cb0ef41Sopenharmony_ci        StringToArray(context, subject_string, subject_length, limit_number));
14051cb0ef41Sopenharmony_ci
14061cb0ef41Sopenharmony_ci    BIND(&next);
14071cb0ef41Sopenharmony_ci  }
14081cb0ef41Sopenharmony_ci
14091cb0ef41Sopenharmony_ci  const TNode<Object> result =
14101cb0ef41Sopenharmony_ci      CallRuntime(Runtime::kStringSplit, context, subject_string,
14111cb0ef41Sopenharmony_ci                  separator_string, limit_number);
14121cb0ef41Sopenharmony_ci  args.PopAndReturn(result);
14131cb0ef41Sopenharmony_ci
14141cb0ef41Sopenharmony_ci  BIND(&return_empty_array);
14151cb0ef41Sopenharmony_ci  {
14161cb0ef41Sopenharmony_ci    const ElementsKind kind = PACKED_ELEMENTS;
14171cb0ef41Sopenharmony_ci    const TNode<NativeContext> native_context = LoadNativeContext(context);
14181cb0ef41Sopenharmony_ci    TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
14191cb0ef41Sopenharmony_ci
14201cb0ef41Sopenharmony_ci    TNode<Smi> length = smi_zero;
14211cb0ef41Sopenharmony_ci    TNode<IntPtrT> capacity = IntPtrConstant(0);
14221cb0ef41Sopenharmony_ci    TNode<JSArray> result_array =
14231cb0ef41Sopenharmony_ci        AllocateJSArray(kind, array_map, capacity, length);
14241cb0ef41Sopenharmony_ci
14251cb0ef41Sopenharmony_ci    args.PopAndReturn(result_array);
14261cb0ef41Sopenharmony_ci  }
14271cb0ef41Sopenharmony_ci}
14281cb0ef41Sopenharmony_ci
14291cb0ef41Sopenharmony_ciTF_BUILTIN(StringSubstring, StringBuiltinsAssembler) {
14301cb0ef41Sopenharmony_ci  auto string = Parameter<String>(Descriptor::kString);
14311cb0ef41Sopenharmony_ci  auto from = UncheckedParameter<IntPtrT>(Descriptor::kFrom);
14321cb0ef41Sopenharmony_ci  auto to = UncheckedParameter<IntPtrT>(Descriptor::kTo);
14331cb0ef41Sopenharmony_ci
14341cb0ef41Sopenharmony_ci  Return(SubString(string, from, to));
14351cb0ef41Sopenharmony_ci}
14361cb0ef41Sopenharmony_ci
14371cb0ef41Sopenharmony_ci
14381cb0ef41Sopenharmony_ci// Return the |word32| codepoint at {index}. Supports SeqStrings and
14391cb0ef41Sopenharmony_ci// ExternalStrings.
14401cb0ef41Sopenharmony_ci// TODO(v8:9880): Use UintPtrT here.
14411cb0ef41Sopenharmony_ciTNode<Int32T> StringBuiltinsAssembler::LoadSurrogatePairAt(
14421cb0ef41Sopenharmony_ci    TNode<String> string, TNode<IntPtrT> length, TNode<IntPtrT> index,
14431cb0ef41Sopenharmony_ci    UnicodeEncoding encoding) {
14441cb0ef41Sopenharmony_ci  Label handle_surrogate_pair(this), return_result(this);
14451cb0ef41Sopenharmony_ci  TVARIABLE(Int32T, var_result);
14461cb0ef41Sopenharmony_ci  TVARIABLE(Int32T, var_trail);
14471cb0ef41Sopenharmony_ci  var_result = StringCharCodeAt(string, Unsigned(index));
14481cb0ef41Sopenharmony_ci  var_trail = Int32Constant(0);
14491cb0ef41Sopenharmony_ci
14501cb0ef41Sopenharmony_ci  GotoIf(Word32NotEqual(Word32And(var_result.value(), Int32Constant(0xFC00)),
14511cb0ef41Sopenharmony_ci                        Int32Constant(0xD800)),
14521cb0ef41Sopenharmony_ci         &return_result);
14531cb0ef41Sopenharmony_ci  TNode<IntPtrT> next_index = IntPtrAdd(index, IntPtrConstant(1));
14541cb0ef41Sopenharmony_ci
14551cb0ef41Sopenharmony_ci  GotoIfNot(IntPtrLessThan(next_index, length), &return_result);
14561cb0ef41Sopenharmony_ci  var_trail = StringCharCodeAt(string, Unsigned(next_index));
14571cb0ef41Sopenharmony_ci  Branch(Word32Equal(Word32And(var_trail.value(), Int32Constant(0xFC00)),
14581cb0ef41Sopenharmony_ci                     Int32Constant(0xDC00)),
14591cb0ef41Sopenharmony_ci         &handle_surrogate_pair, &return_result);
14601cb0ef41Sopenharmony_ci
14611cb0ef41Sopenharmony_ci  BIND(&handle_surrogate_pair);
14621cb0ef41Sopenharmony_ci  {
14631cb0ef41Sopenharmony_ci    TNode<Int32T> lead = var_result.value();
14641cb0ef41Sopenharmony_ci    TNode<Int32T> trail = var_trail.value();
14651cb0ef41Sopenharmony_ci
14661cb0ef41Sopenharmony_ci    // Check that this path is only taken if a surrogate pair is found
14671cb0ef41Sopenharmony_ci    CSA_SLOW_DCHECK(this,
14681cb0ef41Sopenharmony_ci                    Uint32GreaterThanOrEqual(lead, Int32Constant(0xD800)));
14691cb0ef41Sopenharmony_ci    CSA_SLOW_DCHECK(this, Uint32LessThan(lead, Int32Constant(0xDC00)));
14701cb0ef41Sopenharmony_ci    CSA_SLOW_DCHECK(this,
14711cb0ef41Sopenharmony_ci                    Uint32GreaterThanOrEqual(trail, Int32Constant(0xDC00)));
14721cb0ef41Sopenharmony_ci    CSA_SLOW_DCHECK(this, Uint32LessThan(trail, Int32Constant(0xE000)));
14731cb0ef41Sopenharmony_ci
14741cb0ef41Sopenharmony_ci    switch (encoding) {
14751cb0ef41Sopenharmony_ci      case UnicodeEncoding::UTF16:
14761cb0ef41Sopenharmony_ci        var_result = Word32Or(
14771cb0ef41Sopenharmony_ci// Need to swap the order for big-endian platforms
14781cb0ef41Sopenharmony_ci#if V8_TARGET_BIG_ENDIAN
14791cb0ef41Sopenharmony_ci            Word32Shl(lead, Int32Constant(16)), trail);
14801cb0ef41Sopenharmony_ci#else
14811cb0ef41Sopenharmony_ci            Word32Shl(trail, Int32Constant(16)), lead);
14821cb0ef41Sopenharmony_ci#endif
14831cb0ef41Sopenharmony_ci        break;
14841cb0ef41Sopenharmony_ci
14851cb0ef41Sopenharmony_ci      case UnicodeEncoding::UTF32: {
14861cb0ef41Sopenharmony_ci        // Convert UTF16 surrogate pair into |word32| code point, encoded as
14871cb0ef41Sopenharmony_ci        // UTF32.
14881cb0ef41Sopenharmony_ci        TNode<Int32T> surrogate_offset =
14891cb0ef41Sopenharmony_ci            Int32Constant(0x10000 - (0xD800 << 10) - 0xDC00);
14901cb0ef41Sopenharmony_ci
14911cb0ef41Sopenharmony_ci        // (lead << 10) + trail + SURROGATE_OFFSET
14921cb0ef41Sopenharmony_ci        var_result = Int32Add(Word32Shl(lead, Int32Constant(10)),
14931cb0ef41Sopenharmony_ci                              Int32Add(trail, surrogate_offset));
14941cb0ef41Sopenharmony_ci        break;
14951cb0ef41Sopenharmony_ci      }
14961cb0ef41Sopenharmony_ci    }
14971cb0ef41Sopenharmony_ci    Goto(&return_result);
14981cb0ef41Sopenharmony_ci  }
14991cb0ef41Sopenharmony_ci
15001cb0ef41Sopenharmony_ci  BIND(&return_result);
15011cb0ef41Sopenharmony_ci  return var_result.value();
15021cb0ef41Sopenharmony_ci}
15031cb0ef41Sopenharmony_ci
15041cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::BranchIfStringPrimitiveWithNoCustomIteration(
15051cb0ef41Sopenharmony_ci    TNode<Object> object, TNode<Context> context, Label* if_true,
15061cb0ef41Sopenharmony_ci    Label* if_false) {
15071cb0ef41Sopenharmony_ci  GotoIf(TaggedIsSmi(object), if_false);
15081cb0ef41Sopenharmony_ci  GotoIfNot(IsString(CAST(object)), if_false);
15091cb0ef41Sopenharmony_ci
15101cb0ef41Sopenharmony_ci  // Check that the String iterator hasn't been modified in a way that would
15111cb0ef41Sopenharmony_ci  // affect iteration.
15121cb0ef41Sopenharmony_ci  TNode<PropertyCell> protector_cell = StringIteratorProtectorConstant();
15131cb0ef41Sopenharmony_ci  DCHECK(isolate()->heap()->string_iterator_protector().IsPropertyCell());
15141cb0ef41Sopenharmony_ci  Branch(
15151cb0ef41Sopenharmony_ci      TaggedEqual(LoadObjectField(protector_cell, PropertyCell::kValueOffset),
15161cb0ef41Sopenharmony_ci                  SmiConstant(Protectors::kProtectorValid)),
15171cb0ef41Sopenharmony_ci      if_true, if_false);
15181cb0ef41Sopenharmony_ci}
15191cb0ef41Sopenharmony_ci
15201cb0ef41Sopenharmony_ci// Instantiate template due to shared library requirements.
15211cb0ef41Sopenharmony_citemplate V8_EXPORT_PRIVATE void StringBuiltinsAssembler::CopyStringCharacters(
15221cb0ef41Sopenharmony_ci    TNode<String> from_string, TNode<String> to_string,
15231cb0ef41Sopenharmony_ci    TNode<IntPtrT> from_index, TNode<IntPtrT> to_index,
15241cb0ef41Sopenharmony_ci    TNode<IntPtrT> character_count, String::Encoding from_encoding,
15251cb0ef41Sopenharmony_ci    String::Encoding to_encoding);
15261cb0ef41Sopenharmony_ci
15271cb0ef41Sopenharmony_citemplate V8_EXPORT_PRIVATE void StringBuiltinsAssembler::CopyStringCharacters(
15281cb0ef41Sopenharmony_ci    TNode<RawPtrT> from_string, TNode<String> to_string,
15291cb0ef41Sopenharmony_ci    TNode<IntPtrT> from_index, TNode<IntPtrT> to_index,
15301cb0ef41Sopenharmony_ci    TNode<IntPtrT> character_count, String::Encoding from_encoding,
15311cb0ef41Sopenharmony_ci    String::Encoding to_encoding);
15321cb0ef41Sopenharmony_ci
15331cb0ef41Sopenharmony_citemplate <typename T>
15341cb0ef41Sopenharmony_civoid StringBuiltinsAssembler::CopyStringCharacters(
15351cb0ef41Sopenharmony_ci    TNode<T> from_string, TNode<String> to_string, TNode<IntPtrT> from_index,
15361cb0ef41Sopenharmony_ci    TNode<IntPtrT> to_index, TNode<IntPtrT> character_count,
15371cb0ef41Sopenharmony_ci    String::Encoding from_encoding, String::Encoding to_encoding) {
15381cb0ef41Sopenharmony_ci  // from_string could be either a String or a RawPtrT in the case we pass in
15391cb0ef41Sopenharmony_ci  // faked sequential strings when handling external subject strings.
15401cb0ef41Sopenharmony_ci  bool from_one_byte = from_encoding == String::ONE_BYTE_ENCODING;
15411cb0ef41Sopenharmony_ci  bool to_one_byte = to_encoding == String::ONE_BYTE_ENCODING;
15421cb0ef41Sopenharmony_ci  DCHECK_IMPLIES(to_one_byte, from_one_byte);
15431cb0ef41Sopenharmony_ci  Comment("CopyStringCharacters ",
15441cb0ef41Sopenharmony_ci          from_one_byte ? "ONE_BYTE_ENCODING" : "TWO_BYTE_ENCODING", " -> ",
15451cb0ef41Sopenharmony_ci          to_one_byte ? "ONE_BYTE_ENCODING" : "TWO_BYTE_ENCODING");
15461cb0ef41Sopenharmony_ci
15471cb0ef41Sopenharmony_ci  ElementsKind from_kind = from_one_byte ? UINT8_ELEMENTS : UINT16_ELEMENTS;
15481cb0ef41Sopenharmony_ci  ElementsKind to_kind = to_one_byte ? UINT8_ELEMENTS : UINT16_ELEMENTS;
15491cb0ef41Sopenharmony_ci  STATIC_ASSERT(SeqOneByteString::kHeaderSize == SeqTwoByteString::kHeaderSize);
15501cb0ef41Sopenharmony_ci  int header_size = SeqOneByteString::kHeaderSize - kHeapObjectTag;
15511cb0ef41Sopenharmony_ci  TNode<IntPtrT> from_offset =
15521cb0ef41Sopenharmony_ci      ElementOffsetFromIndex(from_index, from_kind, header_size);
15531cb0ef41Sopenharmony_ci  TNode<IntPtrT> to_offset =
15541cb0ef41Sopenharmony_ci      ElementOffsetFromIndex(to_index, to_kind, header_size);
15551cb0ef41Sopenharmony_ci  TNode<IntPtrT> byte_count =
15561cb0ef41Sopenharmony_ci      ElementOffsetFromIndex(character_count, from_kind);
15571cb0ef41Sopenharmony_ci  TNode<IntPtrT> limit_offset = IntPtrAdd(from_offset, byte_count);
15581cb0ef41Sopenharmony_ci
15591cb0ef41Sopenharmony_ci  // Prepare the fast loop
15601cb0ef41Sopenharmony_ci  MachineType type =
15611cb0ef41Sopenharmony_ci      from_one_byte ? MachineType::Uint8() : MachineType::Uint16();
15621cb0ef41Sopenharmony_ci  MachineRepresentation rep = to_one_byte ? MachineRepresentation::kWord8
15631cb0ef41Sopenharmony_ci                                          : MachineRepresentation::kWord16;
15641cb0ef41Sopenharmony_ci  int from_increment = 1 << ElementsKindToShiftSize(from_kind);
15651cb0ef41Sopenharmony_ci  int to_increment = 1 << ElementsKindToShiftSize(to_kind);
15661cb0ef41Sopenharmony_ci
15671cb0ef41Sopenharmony_ci  TVARIABLE(IntPtrT, current_to_offset, to_offset);
15681cb0ef41Sopenharmony_ci  VariableList vars({&current_to_offset}, zone());
15691cb0ef41Sopenharmony_ci  int to_index_constant = 0, from_index_constant = 0;
15701cb0ef41Sopenharmony_ci  bool index_same = (from_encoding == to_encoding) &&
15711cb0ef41Sopenharmony_ci                    (from_index == to_index ||
15721cb0ef41Sopenharmony_ci                     (TryToInt32Constant(from_index, &from_index_constant) &&
15731cb0ef41Sopenharmony_ci                      TryToInt32Constant(to_index, &to_index_constant) &&
15741cb0ef41Sopenharmony_ci                      from_index_constant == to_index_constant));
15751cb0ef41Sopenharmony_ci  BuildFastLoop<IntPtrT>(
15761cb0ef41Sopenharmony_ci      vars, from_offset, limit_offset,
15771cb0ef41Sopenharmony_ci      [&](TNode<IntPtrT> offset) {
15781cb0ef41Sopenharmony_ci        StoreNoWriteBarrier(rep, to_string,
15791cb0ef41Sopenharmony_ci                            index_same ? offset : current_to_offset.value(),
15801cb0ef41Sopenharmony_ci                            Load(type, from_string, offset));
15811cb0ef41Sopenharmony_ci        if (!index_same) {
15821cb0ef41Sopenharmony_ci          Increment(&current_to_offset, to_increment);
15831cb0ef41Sopenharmony_ci        }
15841cb0ef41Sopenharmony_ci      },
15851cb0ef41Sopenharmony_ci      from_increment, IndexAdvanceMode::kPost);
15861cb0ef41Sopenharmony_ci}
15871cb0ef41Sopenharmony_ci
15881cb0ef41Sopenharmony_ci// A wrapper around CopyStringCharacters which determines the correct string
15891cb0ef41Sopenharmony_ci// encoding, allocates a corresponding sequential string, and then copies the
15901cb0ef41Sopenharmony_ci// given character range using CopyStringCharacters.
15911cb0ef41Sopenharmony_ci// |from_string| must be a sequential string.
15921cb0ef41Sopenharmony_ci// 0 <= |from_index| <= |from_index| + |character_count| < from_string.length.
15931cb0ef41Sopenharmony_citemplate <typename T>
15941cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::AllocAndCopyStringCharacters(
15951cb0ef41Sopenharmony_ci    TNode<T> from, TNode<Int32T> from_instance_type, TNode<IntPtrT> from_index,
15961cb0ef41Sopenharmony_ci    TNode<IntPtrT> character_count) {
15971cb0ef41Sopenharmony_ci  Label end(this), one_byte_sequential(this), two_byte_sequential(this);
15981cb0ef41Sopenharmony_ci  TVARIABLE(String, var_result);
15991cb0ef41Sopenharmony_ci
16001cb0ef41Sopenharmony_ci  Branch(IsOneByteStringInstanceType(from_instance_type), &one_byte_sequential,
16011cb0ef41Sopenharmony_ci         &two_byte_sequential);
16021cb0ef41Sopenharmony_ci
16031cb0ef41Sopenharmony_ci  // The subject string is a sequential one-byte string.
16041cb0ef41Sopenharmony_ci  BIND(&one_byte_sequential);
16051cb0ef41Sopenharmony_ci  {
16061cb0ef41Sopenharmony_ci    TNode<String> result = AllocateSeqOneByteString(
16071cb0ef41Sopenharmony_ci        Unsigned(TruncateIntPtrToInt32(character_count)));
16081cb0ef41Sopenharmony_ci    CopyStringCharacters<T>(from, result, from_index, IntPtrConstant(0),
16091cb0ef41Sopenharmony_ci                            character_count, String::ONE_BYTE_ENCODING,
16101cb0ef41Sopenharmony_ci                            String::ONE_BYTE_ENCODING);
16111cb0ef41Sopenharmony_ci    var_result = result;
16121cb0ef41Sopenharmony_ci    Goto(&end);
16131cb0ef41Sopenharmony_ci  }
16141cb0ef41Sopenharmony_ci
16151cb0ef41Sopenharmony_ci  // The subject string is a sequential two-byte string.
16161cb0ef41Sopenharmony_ci  BIND(&two_byte_sequential);
16171cb0ef41Sopenharmony_ci  {
16181cb0ef41Sopenharmony_ci    TNode<String> result = AllocateSeqTwoByteString(
16191cb0ef41Sopenharmony_ci        Unsigned(TruncateIntPtrToInt32(character_count)));
16201cb0ef41Sopenharmony_ci    CopyStringCharacters<T>(from, result, from_index, IntPtrConstant(0),
16211cb0ef41Sopenharmony_ci                            character_count, String::TWO_BYTE_ENCODING,
16221cb0ef41Sopenharmony_ci                            String::TWO_BYTE_ENCODING);
16231cb0ef41Sopenharmony_ci    var_result = result;
16241cb0ef41Sopenharmony_ci    Goto(&end);
16251cb0ef41Sopenharmony_ci  }
16261cb0ef41Sopenharmony_ci
16271cb0ef41Sopenharmony_ci  BIND(&end);
16281cb0ef41Sopenharmony_ci  return var_result.value();
16291cb0ef41Sopenharmony_ci}
16301cb0ef41Sopenharmony_ci
16311cb0ef41Sopenharmony_ci// TODO(v8:9880): Use UintPtrT here.
16321cb0ef41Sopenharmony_ciTNode<String> StringBuiltinsAssembler::SubString(TNode<String> string,
16331cb0ef41Sopenharmony_ci                                                 TNode<IntPtrT> from,
16341cb0ef41Sopenharmony_ci                                                 TNode<IntPtrT> to) {
16351cb0ef41Sopenharmony_ci  TVARIABLE(String, var_result);
16361cb0ef41Sopenharmony_ci  ToDirectStringAssembler to_direct(state(), string);
16371cb0ef41Sopenharmony_ci  Label end(this), runtime(this);
16381cb0ef41Sopenharmony_ci
16391cb0ef41Sopenharmony_ci  const TNode<IntPtrT> substr_length = IntPtrSub(to, from);
16401cb0ef41Sopenharmony_ci  const TNode<IntPtrT> string_length = LoadStringLengthAsWord(string);
16411cb0ef41Sopenharmony_ci
16421cb0ef41Sopenharmony_ci  // Begin dispatching based on substring length.
16431cb0ef41Sopenharmony_ci
16441cb0ef41Sopenharmony_ci  Label original_string_or_invalid_length(this);
16451cb0ef41Sopenharmony_ci  GotoIf(UintPtrGreaterThanOrEqual(substr_length, string_length),
16461cb0ef41Sopenharmony_ci         &original_string_or_invalid_length);
16471cb0ef41Sopenharmony_ci
16481cb0ef41Sopenharmony_ci  // A real substring (substr_length < string_length).
16491cb0ef41Sopenharmony_ci  Label empty(this);
16501cb0ef41Sopenharmony_ci  GotoIf(IntPtrEqual(substr_length, IntPtrConstant(0)), &empty);
16511cb0ef41Sopenharmony_ci
16521cb0ef41Sopenharmony_ci  Label single_char(this);
16531cb0ef41Sopenharmony_ci  GotoIf(IntPtrEqual(substr_length, IntPtrConstant(1)), &single_char);
16541cb0ef41Sopenharmony_ci
16551cb0ef41Sopenharmony_ci  // Deal with different string types: update the index if necessary
16561cb0ef41Sopenharmony_ci  // and extract the underlying string.
16571cb0ef41Sopenharmony_ci
16581cb0ef41Sopenharmony_ci  TNode<String> direct_string = to_direct.TryToDirect(&runtime);
16591cb0ef41Sopenharmony_ci  TNode<IntPtrT> offset = IntPtrAdd(from, to_direct.offset());
16601cb0ef41Sopenharmony_ci  const TNode<Int32T> instance_type = to_direct.instance_type();
16611cb0ef41Sopenharmony_ci
16621cb0ef41Sopenharmony_ci  // The subject string can only be external or sequential string of either
16631cb0ef41Sopenharmony_ci  // encoding at this point.
16641cb0ef41Sopenharmony_ci  Label external_string(this);
16651cb0ef41Sopenharmony_ci  {
16661cb0ef41Sopenharmony_ci    if (FLAG_string_slices) {
16671cb0ef41Sopenharmony_ci      Label next(this);
16681cb0ef41Sopenharmony_ci
16691cb0ef41Sopenharmony_ci      // Short slice.  Copy instead of slicing.
16701cb0ef41Sopenharmony_ci      GotoIf(IntPtrLessThan(substr_length,
16711cb0ef41Sopenharmony_ci                            IntPtrConstant(SlicedString::kMinLength)),
16721cb0ef41Sopenharmony_ci             &next);
16731cb0ef41Sopenharmony_ci
16741cb0ef41Sopenharmony_ci      // Allocate new sliced string.
16751cb0ef41Sopenharmony_ci
16761cb0ef41Sopenharmony_ci      Counters* counters = isolate()->counters();
16771cb0ef41Sopenharmony_ci      IncrementCounter(counters->sub_string_native(), 1);
16781cb0ef41Sopenharmony_ci
16791cb0ef41Sopenharmony_ci      Label one_byte_slice(this), two_byte_slice(this);
16801cb0ef41Sopenharmony_ci      Branch(IsOneByteStringInstanceType(to_direct.instance_type()),
16811cb0ef41Sopenharmony_ci             &one_byte_slice, &two_byte_slice);
16821cb0ef41Sopenharmony_ci
16831cb0ef41Sopenharmony_ci      BIND(&one_byte_slice);
16841cb0ef41Sopenharmony_ci      {
16851cb0ef41Sopenharmony_ci        var_result = AllocateSlicedOneByteString(
16861cb0ef41Sopenharmony_ci            Unsigned(TruncateIntPtrToInt32(substr_length)), direct_string,
16871cb0ef41Sopenharmony_ci            SmiTag(offset));
16881cb0ef41Sopenharmony_ci        Goto(&end);
16891cb0ef41Sopenharmony_ci      }
16901cb0ef41Sopenharmony_ci
16911cb0ef41Sopenharmony_ci      BIND(&two_byte_slice);
16921cb0ef41Sopenharmony_ci      {
16931cb0ef41Sopenharmony_ci        var_result = AllocateSlicedTwoByteString(
16941cb0ef41Sopenharmony_ci            Unsigned(TruncateIntPtrToInt32(substr_length)), direct_string,
16951cb0ef41Sopenharmony_ci            SmiTag(offset));
16961cb0ef41Sopenharmony_ci        Goto(&end);
16971cb0ef41Sopenharmony_ci      }
16981cb0ef41Sopenharmony_ci
16991cb0ef41Sopenharmony_ci      BIND(&next);
17001cb0ef41Sopenharmony_ci    }
17011cb0ef41Sopenharmony_ci
17021cb0ef41Sopenharmony_ci    // The subject string can only be external or sequential string of either
17031cb0ef41Sopenharmony_ci    // encoding at this point.
17041cb0ef41Sopenharmony_ci    GotoIf(to_direct.is_external(), &external_string);
17051cb0ef41Sopenharmony_ci
17061cb0ef41Sopenharmony_ci    var_result = AllocAndCopyStringCharacters(direct_string, instance_type,
17071cb0ef41Sopenharmony_ci                                              offset, substr_length);
17081cb0ef41Sopenharmony_ci
17091cb0ef41Sopenharmony_ci    Counters* counters = isolate()->counters();
17101cb0ef41Sopenharmony_ci    IncrementCounter(counters->sub_string_native(), 1);
17111cb0ef41Sopenharmony_ci
17121cb0ef41Sopenharmony_ci    Goto(&end);
17131cb0ef41Sopenharmony_ci  }
17141cb0ef41Sopenharmony_ci
17151cb0ef41Sopenharmony_ci  // Handle external string.
17161cb0ef41Sopenharmony_ci  BIND(&external_string);
17171cb0ef41Sopenharmony_ci  {
17181cb0ef41Sopenharmony_ci    const TNode<RawPtrT> fake_sequential_string =
17191cb0ef41Sopenharmony_ci        to_direct.PointerToString(&runtime);
17201cb0ef41Sopenharmony_ci
17211cb0ef41Sopenharmony_ci    var_result = AllocAndCopyStringCharacters(
17221cb0ef41Sopenharmony_ci        fake_sequential_string, instance_type, offset, substr_length);
17231cb0ef41Sopenharmony_ci
17241cb0ef41Sopenharmony_ci    Counters* counters = isolate()->counters();
17251cb0ef41Sopenharmony_ci    IncrementCounter(counters->sub_string_native(), 1);
17261cb0ef41Sopenharmony_ci
17271cb0ef41Sopenharmony_ci    Goto(&end);
17281cb0ef41Sopenharmony_ci  }
17291cb0ef41Sopenharmony_ci
17301cb0ef41Sopenharmony_ci  BIND(&empty);
17311cb0ef41Sopenharmony_ci  {
17321cb0ef41Sopenharmony_ci    var_result = EmptyStringConstant();
17331cb0ef41Sopenharmony_ci    Goto(&end);
17341cb0ef41Sopenharmony_ci  }
17351cb0ef41Sopenharmony_ci
17361cb0ef41Sopenharmony_ci  // Substrings of length 1 are generated through CharCodeAt and FromCharCode.
17371cb0ef41Sopenharmony_ci  BIND(&single_char);
17381cb0ef41Sopenharmony_ci  {
17391cb0ef41Sopenharmony_ci    TNode<Int32T> char_code = StringCharCodeAt(string, Unsigned(from));
17401cb0ef41Sopenharmony_ci    var_result = StringFromSingleCharCode(char_code);
17411cb0ef41Sopenharmony_ci    Goto(&end);
17421cb0ef41Sopenharmony_ci  }
17431cb0ef41Sopenharmony_ci
17441cb0ef41Sopenharmony_ci  BIND(&original_string_or_invalid_length);
17451cb0ef41Sopenharmony_ci  {
17461cb0ef41Sopenharmony_ci    CSA_DCHECK(this, IntPtrEqual(substr_length, string_length));
17471cb0ef41Sopenharmony_ci
17481cb0ef41Sopenharmony_ci    // Equal length - check if {from, to} == {0, str.length}.
17491cb0ef41Sopenharmony_ci    GotoIf(UintPtrGreaterThan(from, IntPtrConstant(0)), &runtime);
17501cb0ef41Sopenharmony_ci
17511cb0ef41Sopenharmony_ci    // Return the original string (substr_length == string_length).
17521cb0ef41Sopenharmony_ci
17531cb0ef41Sopenharmony_ci    Counters* counters = isolate()->counters();
17541cb0ef41Sopenharmony_ci    IncrementCounter(counters->sub_string_native(), 1);
17551cb0ef41Sopenharmony_ci
17561cb0ef41Sopenharmony_ci    var_result = string;
17571cb0ef41Sopenharmony_ci    Goto(&end);
17581cb0ef41Sopenharmony_ci  }
17591cb0ef41Sopenharmony_ci
17601cb0ef41Sopenharmony_ci  // Fall back to a runtime call.
17611cb0ef41Sopenharmony_ci  BIND(&runtime);
17621cb0ef41Sopenharmony_ci  {
17631cb0ef41Sopenharmony_ci    var_result =
17641cb0ef41Sopenharmony_ci        CAST(CallRuntime(Runtime::kStringSubstring, NoContextConstant(), string,
17651cb0ef41Sopenharmony_ci                         SmiTag(from), SmiTag(to)));
17661cb0ef41Sopenharmony_ci    Goto(&end);
17671cb0ef41Sopenharmony_ci  }
17681cb0ef41Sopenharmony_ci
17691cb0ef41Sopenharmony_ci  BIND(&end);
17701cb0ef41Sopenharmony_ci  return var_result.value();
17711cb0ef41Sopenharmony_ci}
17721cb0ef41Sopenharmony_ci
17731cb0ef41Sopenharmony_ci}  // namespace internal
17741cb0ef41Sopenharmony_ci}  // namespace v8
1775