Commit bfc3e8bc authored by Z Duong Nguyen-Huu's avatar Z Duong Nguyen-Huu Committed by Commit Bot

Port StringToList to Torque

Bug: v8:8996
Change-Id: I264781f35b7b98cd7c34fc39b9c2451ea6c58ad6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1606544Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarSimon Zünd <szuend@chromium.org>
Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#61498}
parent 2dd0db19
......@@ -2273,6 +2273,9 @@ extern macro CopyFixedArrayElements(
constexpr ElementsKind, FixedArray, constexpr ElementsKind, FixedArray, Smi,
Smi, Smi): void;
extern macro AllocateJSArray(
constexpr ElementsKind, Map, intptr, Smi,
constexpr AllocationFlags): JSArray;
extern macro AllocateJSArray(constexpr ElementsKind, Map, intptr, Smi): JSArray;
extern macro AllocateJSArray(constexpr ElementsKind, Map, Smi, Smi): JSArray;
extern macro AllocateJSArray(Map, FixedArrayBase, Smi): JSArray;
......
......@@ -991,8 +991,6 @@ namespace internal {
/* ES6 #sec-string.raw */ \
CPP(StringRaw) \
\
TFS(StringToList, kSource) \
\
/* Symbol */ \
/* ES #sec-symbol-constructor */ \
CPP(SymbolConstructor) \
......
......@@ -2178,65 +2178,5 @@ void StringBuiltinsAssembler::BranchIfStringPrimitiveWithNoCustomIteration(
if_true, if_false);
}
// This function assumes StringPrimitiveWithNoCustomIteration is true.
TNode<JSArray> StringBuiltinsAssembler::StringToList(TNode<Context> context,
TNode<String> string) {
const ElementsKind kind = PACKED_ELEMENTS;
const TNode<IntPtrT> length = LoadStringLengthAsWord(string);
TNode<Map> array_map =
LoadJSArrayElementsMap(kind, LoadNativeContext(context));
TNode<JSArray> array =
AllocateJSArray(kind, array_map, length, SmiTag(length), nullptr,
INTPTR_PARAMETERS, kAllowLargeObjectAllocation);
TNode<FixedArrayBase> elements = LoadElements(array);
const int first_element_offset = FixedArray::kHeaderSize - kHeapObjectTag;
TNode<IntPtrT> first_to_element_offset =
ElementOffsetFromIndex(IntPtrConstant(0), kind, INTPTR_PARAMETERS, 0);
TNode<IntPtrT> first_offset =
IntPtrAdd(first_to_element_offset, IntPtrConstant(first_element_offset));
TVARIABLE(IntPtrT, var_offset, first_offset);
TVARIABLE(IntPtrT, var_position, IntPtrConstant(0));
Label done(this), next_codepoint(this, {&var_position, &var_offset});
Goto(&next_codepoint);
BIND(&next_codepoint);
{
// Loop condition.
GotoIfNot(IntPtrLessThan(var_position.value(), length), &done);
const UnicodeEncoding encoding = UnicodeEncoding::UTF16;
TNode<Int32T> ch =
LoadSurrogatePairAt(string, length, var_position.value(), encoding);
TNode<String> value = StringFromSingleCodePoint(ch, encoding);
Store(elements, var_offset.value(), value);
// Increment the position.
TNode<IntPtrT> ch_length = LoadStringLengthAsWord(value);
var_position = IntPtrAdd(var_position.value(), ch_length);
// Increment the array offset and continue the loop.
var_offset = IntPtrAdd(var_offset.value(), IntPtrConstant(kTaggedSize));
Goto(&next_codepoint);
}
BIND(&done);
TNode<IntPtrT> new_length = IntPtrDiv(
IntPtrSub(var_offset.value(), first_offset), IntPtrConstant(kTaggedSize));
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(new_length, IntPtrConstant(0)));
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(length, new_length));
StoreObjectFieldNoWriteBarrier(array, JSArray::kLengthOffset,
SmiTag(new_length));
return UncheckedCast<JSArray>(array);
}
TF_BUILTIN(StringToList, StringBuiltinsAssembler) {
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
TNode<String> string = CAST(Parameter(Descriptor::kSource));
Return(StringToList(context, string));
}
} // namespace internal
} // namespace v8
......@@ -34,8 +34,6 @@ class StringBuiltinsAssembler : public CodeStubAssembler {
UnicodeEncoding encoding);
protected:
TNode<JSArray> StringToList(TNode<Context> context, TNode<String> string);
void StringEqual_Loop(Node* lhs, Node* lhs_instance_type,
MachineType lhs_type, Node* rhs,
Node* rhs_instance_type, MachineType rhs_type,
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-string-gen.h'
namespace string_iterator {
macro NewJSStringIterator(implicit context: Context)(
......@@ -26,11 +24,6 @@ namespace string_iterator {
return NewJSStringIterator(name, index);
}
extern macro StringBuiltinsAssembler::LoadSurrogatePairAt(
String, intptr, intptr, constexpr UnicodeEncoding): int32;
extern macro StringFromSingleCodePoint(int32, constexpr UnicodeEncoding):
String;
// ES6 #sec-%stringiteratorprototype%.next
transitioning javascript builtin StringIteratorPrototypeNext(
implicit context: Context)(receiver: Object): JSIteratorResult {
......@@ -45,8 +38,8 @@ namespace string_iterator {
}
// Move to next codepoint.
const encoding = UTF16;
const ch = LoadSurrogatePairAt(string, length, position, encoding);
const value: String = StringFromSingleCodePoint(ch, encoding);
const ch = string::LoadSurrogatePairAt(string, length, position, encoding);
const value: String = string::StringFromSingleCodePoint(ch, encoding);
iterator.next_index = SmiTag(position + value.length_intptr);
return NewJSIteratorResult(value, False);
}
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-string-gen.h'
namespace string {
// ES6 #sec-string.prototype.tostring
transitioning javascript builtin
......@@ -14,4 +16,38 @@ namespace string {
StringPrototypeValueOf(implicit context: Context)(receiver: Object): Object {
return ToThisValue(receiver, kString, 'String.prototype.valueOf');
}
extern macro StringBuiltinsAssembler::LoadSurrogatePairAt(
String, intptr, intptr, constexpr UnicodeEncoding): int32;
extern macro StringFromSingleCodePoint(int32, constexpr UnicodeEncoding):
String;
// This function assumes StringPrimitiveWithNoCustomIteration is true.
transitioning builtin StringToList(implicit context: Context)(string: String):
JSArray {
const kind = PACKED_ELEMENTS;
const stringLength: intptr = string.length_intptr;
const map: Map = LoadJSArrayElementsMap(kind, LoadNativeContext(context));
const array: JSArray = AllocateJSArray(
kind, map, stringLength, SmiTag(stringLength),
kAllowLargeObjectAllocation);
const elements = UnsafeCast<FixedArray>(array.elements);
const encoding = UTF16;
let arrayLength: Smi = 0;
let i: intptr = 0;
while (i < stringLength) {
const ch: int32 = LoadSurrogatePairAt(string, stringLength, i, encoding);
const value: String = StringFromSingleCodePoint(ch, encoding);
elements[arrayLength] = value;
// Increment and continue the loop.
i = i + value.length_intptr;
arrayLength++;
}
assert(arrayLength >= 0);
assert(SmiTag(stringLength) >= arrayLength);
array.length = arrayLength;
return array;
}
}
......@@ -1612,9 +1612,10 @@ class V8_EXPORT_PRIVATE CodeStubAssembler
}
TNode<JSArray> AllocateJSArray(ElementsKind kind, TNode<Map> array_map,
TNode<IntPtrT> capacity, TNode<Smi> length) {
TNode<IntPtrT> capacity, TNode<Smi> length,
AllocationFlags allocation_flags = kNone) {
return AllocateJSArray(kind, array_map, capacity, length, nullptr,
INTPTR_PARAMETERS);
INTPTR_PARAMETERS, allocation_flags);
}
// Allocate a JSArray and initialize the header fields.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment