Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_INTL_SUPPORT
6 : #error Internationalization is expected to be enabled.
7 : #endif // V8_INTL_SUPPORT
8 :
9 : #include "src/builtins/builtins-iterator-gen.h"
10 : #include "src/builtins/builtins-utils-gen.h"
11 : #include "src/code-stub-assembler.h"
12 : #include "src/objects-inl.h"
13 : #include "src/objects.h"
14 : #include "src/objects/js-list-format-inl.h"
15 : #include "src/objects/js-list-format.h"
16 :
17 : namespace v8 {
18 : namespace internal {
19 :
20 : template <class T>
21 : using TNode = compiler::TNode<T>;
22 :
23 224 : class IntlBuiltinsAssembler : public CodeStubAssembler {
24 : public:
25 : explicit IntlBuiltinsAssembler(compiler::CodeAssemblerState* state)
26 224 : : CodeStubAssembler(state) {}
27 :
28 : void ListFormatCommon(TNode<Context> context, TNode<Int32T> argc,
29 : Runtime::FunctionId format_func_id,
30 : const char* method_name);
31 :
32 : TNode<JSArray> AllocateEmptyJSArray(TNode<Context> context);
33 : };
34 :
35 224 : TF_BUILTIN(StringToLowerCaseIntl, IntlBuiltinsAssembler) {
36 : Node* const string = Parameter(Descriptor::kString);
37 : Node* const context = Parameter(Descriptor::kContext);
38 :
39 : CSA_ASSERT(this, IsString(string));
40 :
41 56 : Label call_c(this), return_string(this), runtime(this, Label::kDeferred);
42 :
43 : // Early exit on empty strings.
44 56 : TNode<Uint32T> const length = LoadStringLengthAsWord32(string);
45 112 : GotoIf(Word32Equal(length, Uint32Constant(0)), &return_string);
46 :
47 : // Unpack strings if possible, and bail to runtime unless we get a one-byte
48 : // flat string.
49 : ToDirectStringAssembler to_direct(
50 112 : state(), string, ToDirectStringAssembler::kDontUnpackSlicedStrings);
51 56 : to_direct.TryToDirect(&runtime);
52 :
53 : Node* const instance_type = to_direct.instance_type();
54 : CSA_ASSERT(this,
55 : Word32BinaryNot(IsIndirectStringInstanceType(instance_type)));
56 112 : GotoIfNot(IsOneByteStringInstanceType(instance_type), &runtime);
57 :
58 : // For short strings, do the conversion in CSA through the lookup table.
59 :
60 112 : Node* const dst = AllocateSeqOneByteString(context, length);
61 :
62 : const int kMaxShortStringLength = 24; // Determined empirically.
63 112 : GotoIf(Uint32GreaterThan(length, Uint32Constant(kMaxShortStringLength)),
64 56 : &call_c);
65 :
66 : {
67 56 : Node* const dst_ptr = PointerToSeqStringData(dst);
68 168 : VARIABLE(var_cursor, MachineType::PointerRepresentation(),
69 : IntPtrConstant(0));
70 :
71 : Node* const start_address = to_direct.PointerToData(&call_c);
72 : TNode<IntPtrT> const end_address =
73 112 : Signed(IntPtrAdd(start_address, ChangeUint32ToWord(length)));
74 :
75 : Node* const to_lower_table_addr =
76 112 : ExternalConstant(ExternalReference::intl_to_latin1_lower_table());
77 :
78 168 : VARIABLE(var_did_change, MachineRepresentation::kWord32, Int32Constant(0));
79 :
80 112 : VariableList push_vars({&var_cursor, &var_did_change}, zone());
81 56 : BuildFastLoop(push_vars, start_address, end_address,
82 280 : [=, &var_cursor, &var_did_change](Node* current) {
83 392 : Node* c = Load(MachineType::Uint8(), current);
84 : Node* lower =
85 112 : Load(MachineType::Uint8(), to_lower_table_addr,
86 112 : ChangeInt32ToIntPtr(c));
87 112 : StoreNoWriteBarrier(MachineRepresentation::kWord8, dst_ptr,
88 56 : var_cursor.value(), lower);
89 :
90 224 : var_did_change.Bind(Word32Or(Word32NotEqual(c, lower),
91 280 : var_did_change.value()));
92 :
93 56 : Increment(&var_cursor);
94 56 : },
95 56 : kCharSize, INTPTR_PARAMETERS, IndexAdvanceMode::kPost);
96 :
97 : // Return the original string if it remained unchanged in order to preserve
98 : // e.g. internalization and private symbols (such as the preserved object
99 : // hash) on the source string.
100 112 : GotoIfNot(var_did_change.value(), &return_string);
101 :
102 56 : Return(dst);
103 : }
104 :
105 : // Call into C for case conversion. The signature is:
106 : // String ConvertOneByteToLower(String src, String dst);
107 56 : BIND(&call_c);
108 : {
109 : Node* const src = to_direct.string();
110 :
111 : Node* const function_addr =
112 112 : ExternalConstant(ExternalReference::intl_convert_one_byte_to_lower());
113 :
114 : MachineType type_tagged = MachineType::AnyTagged();
115 :
116 : Node* const result = CallCFunction(function_addr, type_tagged,
117 : std::make_pair(type_tagged, src),
118 56 : std::make_pair(type_tagged, dst));
119 :
120 56 : Return(result);
121 : }
122 :
123 56 : BIND(&return_string);
124 56 : Return(string);
125 :
126 56 : BIND(&runtime);
127 : {
128 : Node* const result = CallRuntime(Runtime::kStringToLowerCaseIntl,
129 112 : NoContextConstant(), string);
130 56 : Return(result);
131 : }
132 56 : }
133 :
134 224 : TF_BUILTIN(StringPrototypeToLowerCaseIntl, IntlBuiltinsAssembler) {
135 : TNode<Object> maybe_string = CAST(Parameter(Descriptor::kReceiver));
136 : TNode<Context> context = CAST(Parameter(Descriptor::kContext));
137 :
138 : TNode<String> string =
139 112 : ToThisString(context, maybe_string, "String.prototype.toLowerCase");
140 :
141 112 : Return(CallBuiltin(Builtins::kStringToLowerCaseIntl, context, string));
142 56 : }
143 :
144 112 : void IntlBuiltinsAssembler::ListFormatCommon(TNode<Context> context,
145 : TNode<Int32T> argc,
146 : Runtime::FunctionId format_func_id,
147 : const char* method_name) {
148 336 : CodeStubArguments args(this, ChangeInt32ToIntPtr(argc));
149 :
150 : // Label has_list(this);
151 : // 1. Let lf be this value.
152 : // 2. If Type(lf) is not Object, throw a TypeError exception.
153 112 : TNode<Object> receiver = args.GetReceiver();
154 :
155 : // 3. If lf does not have an [[InitializedListFormat]] internal slot, throw a
156 : // TypeError exception.
157 : ThrowIfNotInstanceType(context, receiver, JS_INTL_LIST_FORMAT_TYPE,
158 112 : method_name);
159 : TNode<JSListFormat> list_format = CAST(receiver);
160 :
161 : // 4. If list is not provided or is undefined, then
162 112 : TNode<Object> list = args.GetOptionalArgumentValue(0);
163 112 : Label has_list(this);
164 : {
165 224 : GotoIfNot(IsUndefined(list), &has_list);
166 112 : if (format_func_id == Runtime::kFormatList) {
167 : // a. Return an empty String.
168 112 : args.PopAndReturn(EmptyStringConstant());
169 : } else {
170 : DCHECK_EQ(format_func_id, Runtime::kFormatListToParts);
171 : // a. Return an empty Array.
172 112 : args.PopAndReturn(AllocateEmptyJSArray(context));
173 : }
174 : }
175 112 : BIND(&has_list);
176 : {
177 : // 5. Let x be ? IterableToList(list).
178 : TNode<Object> x =
179 112 : CallBuiltin(Builtins::kIterableToListWithSymbolLookup, context, list);
180 :
181 : // 6. Return ? FormatList(lf, x).
182 112 : args.PopAndReturn(CallRuntime(format_func_id, context, list_format, x));
183 : }
184 112 : }
185 :
186 56 : TNode<JSArray> IntlBuiltinsAssembler::AllocateEmptyJSArray(
187 : TNode<Context> context) {
188 : return CodeStubAssembler::AllocateJSArray(
189 : PACKED_ELEMENTS,
190 112 : LoadJSArrayElementsMap(PACKED_ELEMENTS, LoadNativeContext(context)),
191 224 : SmiConstant(0), SmiConstant(0));
192 : }
193 :
194 224 : TF_BUILTIN(ListFormatPrototypeFormat, IntlBuiltinsAssembler) {
195 56 : ListFormatCommon(
196 : CAST(Parameter(Descriptor::kContext)),
197 : UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)),
198 56 : Runtime::kFormatList, "Intl.ListFormat.prototype.format");
199 56 : }
200 :
201 224 : TF_BUILTIN(ListFormatPrototypeFormatToParts, IntlBuiltinsAssembler) {
202 56 : ListFormatCommon(
203 : CAST(Parameter(Descriptor::kContext)),
204 : UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)),
205 56 : Runtime::kFormatListToParts, "Intl.ListFormat.prototype.formatToParts");
206 56 : }
207 :
208 : } // namespace internal
209 59480 : } // namespace v8
|