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#ifndef V8_INTL_SUPPORT
61cb0ef41Sopenharmony_ci#error Internationalization is expected to be enabled.
71cb0ef41Sopenharmony_ci#endif  // V8_INTL_SUPPORT
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/builtins/builtins-iterator-gen.h"
101cb0ef41Sopenharmony_ci#include "src/builtins/builtins-utils-gen.h"
111cb0ef41Sopenharmony_ci#include "src/codegen/code-stub-assembler.h"
121cb0ef41Sopenharmony_ci#include "src/objects/js-list-format-inl.h"
131cb0ef41Sopenharmony_ci#include "src/objects/js-list-format.h"
141cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
151cb0ef41Sopenharmony_ci#include "src/objects/objects.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace v8 {
181cb0ef41Sopenharmony_cinamespace internal {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciclass IntlBuiltinsAssembler : public CodeStubAssembler {
211cb0ef41Sopenharmony_ci public:
221cb0ef41Sopenharmony_ci  explicit IntlBuiltinsAssembler(compiler::CodeAssemblerState* state)
231cb0ef41Sopenharmony_ci      : CodeStubAssembler(state) {}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  void ListFormatCommon(TNode<Context> context, TNode<Int32T> argc,
261cb0ef41Sopenharmony_ci                        Runtime::FunctionId format_func_id,
271cb0ef41Sopenharmony_ci                        const char* method_name);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  TNode<JSArray> AllocateEmptyJSArray(TNode<Context> context);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  TNode<IntPtrT> PointerToSeqStringData(TNode<String> seq_string) {
321cb0ef41Sopenharmony_ci    CSA_DCHECK(this,
331cb0ef41Sopenharmony_ci               IsSequentialStringInstanceType(LoadInstanceType(seq_string)));
341cb0ef41Sopenharmony_ci    STATIC_ASSERT(SeqOneByteString::kHeaderSize ==
351cb0ef41Sopenharmony_ci                  SeqTwoByteString::kHeaderSize);
361cb0ef41Sopenharmony_ci    return IntPtrAdd(
371cb0ef41Sopenharmony_ci        BitcastTaggedToWord(seq_string),
381cb0ef41Sopenharmony_ci        IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag));
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciTF_BUILTIN(StringToLowerCaseIntl, IntlBuiltinsAssembler) {
431cb0ef41Sopenharmony_ci  const auto string = Parameter<String>(Descriptor::kString);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  Label call_c(this), return_string(this), runtime(this, Label::kDeferred);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  // Early exit on empty strings.
481cb0ef41Sopenharmony_ci  const TNode<Uint32T> length = LoadStringLengthAsWord32(string);
491cb0ef41Sopenharmony_ci  GotoIf(Word32Equal(length, Uint32Constant(0)), &return_string);
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  // Unpack strings if possible, and bail to runtime unless we get a one-byte
521cb0ef41Sopenharmony_ci  // flat string.
531cb0ef41Sopenharmony_ci  ToDirectStringAssembler to_direct(
541cb0ef41Sopenharmony_ci      state(), string, ToDirectStringAssembler::kDontUnpackSlicedStrings);
551cb0ef41Sopenharmony_ci  to_direct.TryToDirect(&runtime);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  const TNode<Int32T> instance_type = to_direct.instance_type();
581cb0ef41Sopenharmony_ci  CSA_DCHECK(this,
591cb0ef41Sopenharmony_ci             Word32BinaryNot(IsIndirectStringInstanceType(instance_type)));
601cb0ef41Sopenharmony_ci  GotoIfNot(IsOneByteStringInstanceType(instance_type), &runtime);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  // For short strings, do the conversion in CSA through the lookup table.
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  const TNode<String> dst = AllocateSeqOneByteString(length);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  const int kMaxShortStringLength = 24;  // Determined empirically.
671cb0ef41Sopenharmony_ci  GotoIf(Uint32GreaterThan(length, Uint32Constant(kMaxShortStringLength)),
681cb0ef41Sopenharmony_ci         &call_c);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  {
711cb0ef41Sopenharmony_ci    const TNode<IntPtrT> dst_ptr = PointerToSeqStringData(dst);
721cb0ef41Sopenharmony_ci    TVARIABLE(IntPtrT, var_cursor, IntPtrConstant(0));
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    const TNode<IntPtrT> start_address =
751cb0ef41Sopenharmony_ci        ReinterpretCast<IntPtrT>(to_direct.PointerToData(&call_c));
761cb0ef41Sopenharmony_ci    const TNode<IntPtrT> end_address =
771cb0ef41Sopenharmony_ci        Signed(IntPtrAdd(start_address, ChangeUint32ToWord(length)));
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    const TNode<ExternalReference> to_lower_table_addr =
801cb0ef41Sopenharmony_ci        ExternalConstant(ExternalReference::intl_to_latin1_lower_table());
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    TVARIABLE(Word32T, var_did_change, Int32Constant(0));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    VariableList push_vars({&var_cursor, &var_did_change}, zone());
851cb0ef41Sopenharmony_ci    BuildFastLoop<IntPtrT>(
861cb0ef41Sopenharmony_ci        push_vars, start_address, end_address,
871cb0ef41Sopenharmony_ci        [&](TNode<IntPtrT> current) {
881cb0ef41Sopenharmony_ci          TNode<Uint8T> c = Load<Uint8T>(current);
891cb0ef41Sopenharmony_ci          TNode<Uint8T> lower =
901cb0ef41Sopenharmony_ci              Load<Uint8T>(to_lower_table_addr, ChangeInt32ToIntPtr(c));
911cb0ef41Sopenharmony_ci          StoreNoWriteBarrier(MachineRepresentation::kWord8, dst_ptr,
921cb0ef41Sopenharmony_ci                              var_cursor.value(), lower);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci          var_did_change =
951cb0ef41Sopenharmony_ci              Word32Or(Word32NotEqual(c, lower), var_did_change.value());
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci          Increment(&var_cursor);
981cb0ef41Sopenharmony_ci        },
991cb0ef41Sopenharmony_ci        kCharSize, IndexAdvanceMode::kPost);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci    // Return the original string if it remained unchanged in order to preserve
1021cb0ef41Sopenharmony_ci    // e.g. internalization and private symbols (such as the preserved object
1031cb0ef41Sopenharmony_ci    // hash) on the source string.
1041cb0ef41Sopenharmony_ci    GotoIfNot(var_did_change.value(), &return_string);
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci    Return(dst);
1071cb0ef41Sopenharmony_ci  }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  // Call into C for case conversion. The signature is:
1101cb0ef41Sopenharmony_ci  // String ConvertOneByteToLower(String src, String dst);
1111cb0ef41Sopenharmony_ci  BIND(&call_c);
1121cb0ef41Sopenharmony_ci  {
1131cb0ef41Sopenharmony_ci    const TNode<String> src = to_direct.string();
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci    const TNode<ExternalReference> function_addr =
1161cb0ef41Sopenharmony_ci        ExternalConstant(ExternalReference::intl_convert_one_byte_to_lower());
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci    MachineType type_tagged = MachineType::AnyTagged();
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci    const TNode<String> result = CAST(CallCFunction(
1211cb0ef41Sopenharmony_ci        function_addr, type_tagged, std::make_pair(type_tagged, src),
1221cb0ef41Sopenharmony_ci        std::make_pair(type_tagged, dst)));
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci    Return(result);
1251cb0ef41Sopenharmony_ci  }
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  BIND(&return_string);
1281cb0ef41Sopenharmony_ci  Return(string);
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  BIND(&runtime);
1311cb0ef41Sopenharmony_ci  {
1321cb0ef41Sopenharmony_ci    const TNode<Object> result = CallRuntime(Runtime::kStringToLowerCaseIntl,
1331cb0ef41Sopenharmony_ci                                             NoContextConstant(), string);
1341cb0ef41Sopenharmony_ci    Return(result);
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciTF_BUILTIN(StringPrototypeToLowerCaseIntl, IntlBuiltinsAssembler) {
1391cb0ef41Sopenharmony_ci  auto maybe_string = Parameter<Object>(Descriptor::kReceiver);
1401cb0ef41Sopenharmony_ci  auto context = Parameter<Context>(Descriptor::kContext);
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  TNode<String> string =
1431cb0ef41Sopenharmony_ci      ToThisString(context, maybe_string, "String.prototype.toLowerCase");
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  Return(CallBuiltin(Builtin::kStringToLowerCaseIntl, context, string));
1461cb0ef41Sopenharmony_ci}
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_civoid IntlBuiltinsAssembler::ListFormatCommon(TNode<Context> context,
1491cb0ef41Sopenharmony_ci                                             TNode<Int32T> argc,
1501cb0ef41Sopenharmony_ci                                             Runtime::FunctionId format_func_id,
1511cb0ef41Sopenharmony_ci                                             const char* method_name) {
1521cb0ef41Sopenharmony_ci  CodeStubArguments args(this, argc);
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  // Label has_list(this);
1551cb0ef41Sopenharmony_ci  // 1. Let lf be this value.
1561cb0ef41Sopenharmony_ci  // 2. If Type(lf) is not Object, throw a TypeError exception.
1571cb0ef41Sopenharmony_ci  TNode<Object> receiver = args.GetReceiver();
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  // 3. If lf does not have an [[InitializedListFormat]] internal slot, throw a
1601cb0ef41Sopenharmony_ci  // TypeError exception.
1611cb0ef41Sopenharmony_ci  ThrowIfNotInstanceType(context, receiver, JS_LIST_FORMAT_TYPE, method_name);
1621cb0ef41Sopenharmony_ci  TNode<JSListFormat> list_format = CAST(receiver);
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  TNode<Object> list = args.GetOptionalArgumentValue(0);
1651cb0ef41Sopenharmony_ci  {
1661cb0ef41Sopenharmony_ci    // 4. Let stringList be ? StringListFromIterable(list).
1671cb0ef41Sopenharmony_ci    TNode<Object> string_list =
1681cb0ef41Sopenharmony_ci        CallBuiltin(Builtin::kStringListFromIterable, context, list);
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci    // 6. Return ? FormatList(lf, stringList).
1711cb0ef41Sopenharmony_ci    args.PopAndReturn(
1721cb0ef41Sopenharmony_ci        CallRuntime(format_func_id, context, list_format, string_list));
1731cb0ef41Sopenharmony_ci  }
1741cb0ef41Sopenharmony_ci}
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ciTNode<JSArray> IntlBuiltinsAssembler::AllocateEmptyJSArray(
1771cb0ef41Sopenharmony_ci    TNode<Context> context) {
1781cb0ef41Sopenharmony_ci  return CodeStubAssembler::AllocateJSArray(
1791cb0ef41Sopenharmony_ci      PACKED_ELEMENTS,
1801cb0ef41Sopenharmony_ci      LoadJSArrayElementsMap(PACKED_ELEMENTS, LoadNativeContext(context)),
1811cb0ef41Sopenharmony_ci      IntPtrConstant(0), SmiConstant(0));
1821cb0ef41Sopenharmony_ci}
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ciTF_BUILTIN(ListFormatPrototypeFormat, IntlBuiltinsAssembler) {
1851cb0ef41Sopenharmony_ci  ListFormatCommon(
1861cb0ef41Sopenharmony_ci      Parameter<Context>(Descriptor::kContext),
1871cb0ef41Sopenharmony_ci      UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount),
1881cb0ef41Sopenharmony_ci      Runtime::kFormatList, "Intl.ListFormat.prototype.format");
1891cb0ef41Sopenharmony_ci}
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ciTF_BUILTIN(ListFormatPrototypeFormatToParts, IntlBuiltinsAssembler) {
1921cb0ef41Sopenharmony_ci  ListFormatCommon(
1931cb0ef41Sopenharmony_ci      Parameter<Context>(Descriptor::kContext),
1941cb0ef41Sopenharmony_ci      UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount),
1951cb0ef41Sopenharmony_ci      Runtime::kFormatListToParts, "Intl.ListFormat.prototype.formatToParts");
1961cb0ef41Sopenharmony_ci}
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci}  // namespace internal
1991cb0ef41Sopenharmony_ci}  // namespace v8
200