builtins-intl-gen.cc 7.04 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif  // V8_INTL_SUPPORT

9
#include "src/builtins/builtins-iterator-gen.h"
10
#include "src/builtins/builtins-utils-gen.h"
11
#include "src/codegen/code-stub-assembler.h"
12 13
#include "src/objects/js-list-format-inl.h"
#include "src/objects/js-list-format.h"
14 15
#include "src/objects/objects-inl.h"
#include "src/objects/objects.h"
16 17 18 19 20 21 22 23

namespace v8 {
namespace internal {

class IntlBuiltinsAssembler : public CodeStubAssembler {
 public:
  explicit IntlBuiltinsAssembler(compiler::CodeAssemblerState* state)
      : CodeStubAssembler(state) {}
24 25 26 27 28

  void ListFormatCommon(TNode<Context> context, TNode<Int32T> argc,
                        Runtime::FunctionId format_func_id,
                        const char* method_name);

29
  TNode<JSArray> AllocateEmptyJSArray(TNode<Context> context);
30 31 32 33 34 35 36 37 38 39

  TNode<IntPtrT> PointerToSeqStringData(TNode<String> seq_string) {
    CSA_ASSERT(this,
               IsSequentialStringInstanceType(LoadInstanceType(seq_string)));
    STATIC_ASSERT(SeqOneByteString::kHeaderSize ==
                  SeqTwoByteString::kHeaderSize);
    return IntPtrAdd(
        BitcastTaggedToWord(seq_string),
        IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag));
  }
40 41
};

42
TF_BUILTIN(StringToLowerCaseIntl, IntlBuiltinsAssembler) {
43
  const auto string = Parameter<String>(Descriptor::kString);
44 45 46 47

  Label call_c(this), return_string(this), runtime(this, Label::kDeferred);

  // Early exit on empty strings.
48
  const TNode<Uint32T> length = LoadStringLengthAsWord32(string);
49
  GotoIf(Word32Equal(length, Uint32Constant(0)), &return_string);
50 51 52 53 54 55 56

  // Unpack strings if possible, and bail to runtime unless we get a one-byte
  // flat string.
  ToDirectStringAssembler to_direct(
      state(), string, ToDirectStringAssembler::kDontUnpackSlicedStrings);
  to_direct.TryToDirect(&runtime);

57
  const TNode<Int32T> instance_type = to_direct.instance_type();
58 59 60 61 62 63
  CSA_ASSERT(this,
             Word32BinaryNot(IsIndirectStringInstanceType(instance_type)));
  GotoIfNot(IsOneByteStringInstanceType(instance_type), &runtime);

  // For short strings, do the conversion in CSA through the lookup table.

64
  const TNode<String> dst = AllocateSeqOneByteString(length);
65 66

  const int kMaxShortStringLength = 24;  // Determined empirically.
67 68
  GotoIf(Uint32GreaterThan(length, Uint32Constant(kMaxShortStringLength)),
         &call_c);
69 70

  {
71
    const TNode<IntPtrT> dst_ptr = PointerToSeqStringData(dst);
72
    TVARIABLE(IntPtrT, var_cursor, IntPtrConstant(0));
73

74
    const TNode<IntPtrT> start_address =
75
        ReinterpretCast<IntPtrT>(to_direct.PointerToData(&call_c));
76
    const TNode<IntPtrT> end_address =
77
        Signed(IntPtrAdd(start_address, ChangeUint32ToWord(length)));
78

79
    const TNode<ExternalReference> to_lower_table_addr =
80
        ExternalConstant(ExternalReference::intl_to_latin1_lower_table());
81

82
    TVARIABLE(Word32T, var_did_change, Int32Constant(0));
83 84

    VariableList push_vars({&var_cursor, &var_did_change}, zone());
85
    BuildFastLoop<IntPtrT>(
86
        push_vars, start_address, end_address,
87
        [&](TNode<IntPtrT> current) {
88 89 90 91 92 93
          TNode<Uint8T> c = Load<Uint8T>(current);
          TNode<Uint8T> lower =
              Load<Uint8T>(to_lower_table_addr, ChangeInt32ToIntPtr(c));
          StoreNoWriteBarrier(MachineRepresentation::kWord8, dst_ptr,
                              var_cursor.value(), lower);

94 95
          var_did_change =
              Word32Or(Word32NotEqual(c, lower), var_did_change.value());
96 97 98

          Increment(&var_cursor);
        },
99
        kCharSize, IndexAdvanceMode::kPost);
100

101 102 103 104 105
    // Return the original string if it remained unchanged in order to preserve
    // e.g. internalization and private symbols (such as the preserved object
    // hash) on the source string.
    GotoIfNot(var_did_change.value(), &return_string);

106 107 108 109
    Return(dst);
  }

  // Call into C for case conversion. The signature is:
110
  // String ConvertOneByteToLower(String src, String dst);
111 112
  BIND(&call_c);
  {
113
    const TNode<String> src = to_direct.string();
114

115
    const TNode<ExternalReference> function_addr =
116
        ExternalConstant(ExternalReference::intl_convert_one_byte_to_lower());
117 118 119

    MachineType type_tagged = MachineType::AnyTagged();

120 121 122
    const TNode<String> result = CAST(CallCFunction(
        function_addr, type_tagged, std::make_pair(type_tagged, src),
        std::make_pair(type_tagged, dst)));
123 124 125 126 127 128 129 130 131

    Return(result);
  }

  BIND(&return_string);
  Return(string);

  BIND(&runtime);
  {
132
    const TNode<Object> result = CallRuntime(Runtime::kStringToLowerCaseIntl,
133
                                             NoContextConstant(), string);
134 135 136 137
    Return(result);
  }
}

138
TF_BUILTIN(StringPrototypeToLowerCaseIntl, IntlBuiltinsAssembler) {
139 140
  auto maybe_string = Parameter<Object>(Descriptor::kReceiver);
  auto context = Parameter<Context>(Descriptor::kContext);
141

142
  TNode<String> string =
143 144 145 146 147
      ToThisString(context, maybe_string, "String.prototype.toLowerCase");

  Return(CallBuiltin(Builtins::kStringToLowerCaseIntl, context, string));
}

148 149 150 151
void IntlBuiltinsAssembler::ListFormatCommon(TNode<Context> context,
                                             TNode<Int32T> argc,
                                             Runtime::FunctionId format_func_id,
                                             const char* method_name) {
152
  CodeStubArguments args(this, argc);
153 154 155 156 157 158 159 160

  // Label has_list(this);
  // 1. Let lf be this value.
  // 2. If Type(lf) is not Object, throw a TypeError exception.
  TNode<Object> receiver = args.GetReceiver();

  // 3. If lf does not have an [[InitializedListFormat]] internal slot, throw a
  // TypeError exception.
161
  ThrowIfNotInstanceType(context, receiver, JS_LIST_FORMAT_TYPE, method_name);
162 163 164 165
  TNode<JSListFormat> list_format = CAST(receiver);

  TNode<Object> list = args.GetOptionalArgumentValue(0);
  {
166 167 168
    // 4. Let stringList be ? StringListFromIterable(list).
    TNode<Object> string_list =
        CallBuiltin(Builtins::kStringListFromIterable, context, list);
169

170 171 172
    // 6. Return ? FormatList(lf, stringList).
    args.PopAndReturn(
        CallRuntime(format_func_id, context, list_format, string_list));
173 174 175
  }
}

176 177
TNode<JSArray> IntlBuiltinsAssembler::AllocateEmptyJSArray(
    TNode<Context> context) {
178
  return CodeStubAssembler::AllocateJSArray(
179 180
      PACKED_ELEMENTS,
      LoadJSArrayElementsMap(PACKED_ELEMENTS, LoadNativeContext(context)),
181
      IntPtrConstant(0), SmiConstant(0));
182 183 184 185
}

TF_BUILTIN(ListFormatPrototypeFormat, IntlBuiltinsAssembler) {
  ListFormatCommon(
186 187
      Parameter<Context>(Descriptor::kContext),
      UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount),
188 189 190 191 192
      Runtime::kFormatList, "Intl.ListFormat.prototype.format");
}

TF_BUILTIN(ListFormatPrototypeFormatToParts, IntlBuiltinsAssembler) {
  ListFormatCommon(
193 194
      Parameter<Context>(Descriptor::kContext),
      UncheckedParameter<Int32T>(Descriptor::kJSActualArgumentsCount),
195 196 197
      Runtime::kFormatListToParts, "Intl.ListFormat.prototype.formatToParts");
}

198 199
}  // namespace internal
}  // namespace v8