[builtins] Port TFS StringRepeat to Torque.

Bug: v8:8996
Change-Id: I035c380768c324d8912aa8bc414d533dbf7f3e5a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1524640
Commit-Queue: Peter Wong <peter.wm.wong@gmail.com>
Reviewed-by: Simon Zünd <szuend@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60292}
This commit is contained in:
peterwmwong 2019-03-18 07:26:35 -05:00 committed by Commit Bot
parent d62cd2f745
commit a4ea15575f
4 changed files with 28 additions and 49 deletions

View File

@ -934,6 +934,7 @@ torque_files = [
"src/builtins/iterator.tq",
"src/builtins/string-endswith.tq",
"src/builtins/string-html.tq",
"src/builtins/string-repeat.tq",
"src/builtins/string-startswith.tq",
"src/builtins/typed-array.tq",
"src/builtins/typed-array-createtypedarray.tq",
@ -977,6 +978,7 @@ torque_namespaces = [
"object",
"string",
"string-html",
"string-repeat",
"test",
"typed-array",
"typed-array-createtypedarray",

View File

@ -113,7 +113,6 @@ namespace internal {
TFS(StringIndexOf, kReceiver, kSearchString, kPosition) \
TFC(StringLessThan, Compare) \
TFC(StringLessThanOrEqual, Compare) \
TFS(StringRepeat, kString, kCount) \
TFC(StringSubstring, StringSubstring) \
\
/* OrderedHashTable helpers */ \

View File

@ -1227,54 +1227,6 @@ TF_BUILTIN(StringPrototypeRepeat, StringBuiltinsAssembler) {
}
}
// Helper with less checks
TF_BUILTIN(StringRepeat, StringBuiltinsAssembler) {
Node* const context = Parameter(Descriptor::kContext);
Node* const string = Parameter(Descriptor::kString);
TNode<Smi> const count = CAST(Parameter(Descriptor::kCount));
CSA_ASSERT(this, IsString(string));
CSA_ASSERT(this, Word32BinaryNot(IsEmptyString(string)));
CSA_ASSERT(this, TaggedIsPositiveSmi(count));
// The string is repeated with the following algorithm:
// let n = count;
// let power_of_two_repeats = string;
// let result = "";
// while (true) {
// if (n & 1) result += s;
// n >>= 1;
// if (n === 0) return result;
// power_of_two_repeats += power_of_two_repeats;
// }
VARIABLE(var_result, MachineRepresentation::kTagged, EmptyStringConstant());
VARIABLE(var_temp, MachineRepresentation::kTagged, string);
TVARIABLE(Smi, var_count, count);
Label loop(this, {&var_count, &var_result, &var_temp}), return_result(this);
Goto(&loop);
BIND(&loop);
{
{
Label next(this);
GotoIfNot(SmiToInt32(SmiAnd(var_count.value(), SmiConstant(1))), &next);
var_result.Bind(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
var_result.value(), var_temp.value()));
Goto(&next);
BIND(&next);
}
var_count = SmiShr(var_count.value(), 1);
GotoIf(SmiEqual(var_count.value(), SmiConstant(0)), &return_result);
var_temp.Bind(CallBuiltin(Builtins::kStringAdd_CheckNone, context,
var_temp.value(), var_temp.value()));
Goto(&loop);
}
BIND(&return_result);
Return(var_result.value());
}
// ES6 #sec-string.prototype.replace
TF_BUILTIN(StringPrototypeReplace, StringBuiltinsAssembler) {
Label out(this);

View File

@ -0,0 +1,26 @@
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
namespace string_repeat {
builtin StringRepeat(implicit context: Context)(string: String, count: Smi):
String {
assert(count >= 0);
assert(string != kEmptyString);
let result: String = kEmptyString;
let powerOfTwoRepeats: String = string;
let n: intptr = Convert<intptr>(count);
while (true) {
if ((n & 1) == 1) result = result + powerOfTwoRepeats;
n = n >> 1;
if (n == 0) break;
powerOfTwoRepeats = powerOfTwoRepeats + powerOfTwoRepeats;
}
return result;
}
}