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