Commit f91e12cc authored by jkummerow's avatar jkummerow Committed by Commit bot

[cleanup] Refactor builtins-string.cc to use TF_BUILTIN macro

Review-Url: https://codereview.chromium.org/2614973003
Cr-Commit-Position: refs/heads/master@{#42134}
parent b606e1b6
......@@ -52,11 +52,17 @@ class StringBuiltinsAssembler : public CodeStubAssembler {
Int32Constant(kType)),
if_true, if_false);
}
};
namespace {
void GenerateStringEqual(ResultMode mode);
void GenerateStringRelationalComparison(RelationalComparisonMode mode);
Node* ToSmiBetweenZeroAnd(Node* context, Node* value, Node* limit);
void GenerateStringEqual(CodeStubAssembler* assembler, ResultMode mode) {
Node* LoadSurrogatePairAt(Node* string, Node* length, Node* index,
UnicodeEncoding encoding);
};
void StringBuiltinsAssembler::GenerateStringEqual(ResultMode mode) {
// Here's pseudo-code for the algorithm below in case of kDontNegateResult
// mode; for kNegateResult mode we properly negate the result.
//
......@@ -73,140 +79,89 @@ void GenerateStringEqual(CodeStubAssembler* assembler, ResultMode mode) {
// }
// return %StringEqual(lhs, rhs);
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
Node* lhs = assembler->Parameter(0);
Node* rhs = assembler->Parameter(1);
Node* context = assembler->Parameter(2);
Node* lhs = Parameter(0);
Node* rhs = Parameter(1);
Node* context = Parameter(2);
Label if_equal(assembler), if_notequal(assembler);
Label if_equal(this), if_notequal(this);
// Fast check to see if {lhs} and {rhs} refer to the same String object.
Label if_same(assembler), if_notsame(assembler);
assembler->Branch(assembler->WordEqual(lhs, rhs), &if_same, &if_notsame);
assembler->Bind(&if_same);
assembler->Goto(&if_equal);
assembler->Bind(&if_notsame);
GotoIf(WordEqual(lhs, rhs), &if_equal);
// Load the length of {lhs} and {rhs}.
Node* lhs_length = LoadStringLength(lhs);
Node* rhs_length = LoadStringLength(rhs);
// Strings with different lengths cannot be equal.
GotoIf(WordNotEqual(lhs_length, rhs_length), &if_notequal);
// Load instance types of {lhs} and {rhs}.
Node* lhs_instance_type = LoadInstanceType(lhs);
Node* rhs_instance_type = LoadInstanceType(rhs);
// Combine the instance types into a single 16-bit value, so we can check
// both of them at once.
Node* both_instance_types = Word32Or(
lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
// Check if both {lhs} and {rhs} are internalized. Since we already know
// that they're not the same object, they're not equal in that case.
int const kBothInternalizedMask =
kIsNotInternalizedMask | (kIsNotInternalizedMask << 8);
int const kBothInternalizedTag = kInternalizedTag | (kInternalizedTag << 8);
GotoIf(Word32Equal(Word32And(both_instance_types,
Int32Constant(kBothInternalizedMask)),
Int32Constant(kBothInternalizedTag)),
&if_notequal);
// Check that both {lhs} and {rhs} are flat one-byte strings.
int const kBothSeqOneByteStringMask =
kStringEncodingMask | kStringRepresentationMask |
((kStringEncodingMask | kStringRepresentationMask) << 8);
int const kBothSeqOneByteStringTag =
kOneByteStringTag | kSeqStringTag |
((kOneByteStringTag | kSeqStringTag) << 8);
Label if_bothonebyteseqstrings(this), if_notbothonebyteseqstrings(this);
Branch(Word32Equal(Word32And(both_instance_types,
Int32Constant(kBothSeqOneByteStringMask)),
Int32Constant(kBothSeqOneByteStringTag)),
&if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
Bind(&if_bothonebyteseqstrings);
{
// The {lhs} and {rhs} don't refer to the exact same String object.
// Compute the effective offset of the first character.
Node* begin =
IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag);
// Compute the first offset after the string from the length.
Node* end = IntPtrAdd(begin, SmiUntag(lhs_length));
// Loop over the {lhs} and {rhs} strings to see if they are equal.
Variable var_offset(this, MachineType::PointerRepresentation());
Label loop(this, &var_offset);
var_offset.Bind(begin);
Goto(&loop);
Bind(&loop);
{
// If {offset} equals {end}, no difference was found, so the
// strings are equal.
Node* offset = var_offset.value();
GotoIf(WordEqual(offset, end), &if_equal);
// Load the length of {lhs} and {rhs}.
Node* lhs_length = assembler->LoadStringLength(lhs);
Node* rhs_length = assembler->LoadStringLength(rhs);
// Load the next characters from {lhs} and {rhs}.
Node* lhs_value = Load(MachineType::Uint8(), lhs, offset);
Node* rhs_value = Load(MachineType::Uint8(), rhs, offset);
// Check if the lengths of {lhs} and {rhs} are equal.
Label if_lengthisequal(assembler), if_lengthisnotequal(assembler);
assembler->Branch(assembler->WordEqual(lhs_length, rhs_length),
&if_lengthisequal, &if_lengthisnotequal);
// Check if the characters match.
GotoIf(Word32NotEqual(lhs_value, rhs_value), &if_notequal);
assembler->Bind(&if_lengthisequal);
{
// Load instance types of {lhs} and {rhs}.
Node* lhs_instance_type = assembler->LoadInstanceType(lhs);
Node* rhs_instance_type = assembler->LoadInstanceType(rhs);
// Combine the instance types into a single 16-bit value, so we can check
// both of them at once.
Node* both_instance_types = assembler->Word32Or(
lhs_instance_type,
assembler->Word32Shl(rhs_instance_type, assembler->Int32Constant(8)));
// Check if both {lhs} and {rhs} are internalized.
int const kBothInternalizedMask =
kIsNotInternalizedMask | (kIsNotInternalizedMask << 8);
int const kBothInternalizedTag =
kInternalizedTag | (kInternalizedTag << 8);
Label if_bothinternalized(assembler), if_notbothinternalized(assembler);
assembler->Branch(assembler->Word32Equal(
assembler->Word32And(both_instance_types,
assembler->Int32Constant(
kBothInternalizedMask)),
assembler->Int32Constant(kBothInternalizedTag)),
&if_bothinternalized, &if_notbothinternalized);
assembler->Bind(&if_bothinternalized);
{
// Fast negative check for internalized-to-internalized equality.
assembler->Goto(&if_notequal);
}
assembler->Bind(&if_notbothinternalized);
{
// Check that both {lhs} and {rhs} are flat one-byte strings.
int const kBothSeqOneByteStringMask =
kStringEncodingMask | kStringRepresentationMask |
((kStringEncodingMask | kStringRepresentationMask) << 8);
int const kBothSeqOneByteStringTag =
kOneByteStringTag | kSeqStringTag |
((kOneByteStringTag | kSeqStringTag) << 8);
Label if_bothonebyteseqstrings(assembler),
if_notbothonebyteseqstrings(assembler);
assembler->Branch(
assembler->Word32Equal(
assembler->Word32And(
both_instance_types,
assembler->Int32Constant(kBothSeqOneByteStringMask)),
assembler->Int32Constant(kBothSeqOneByteStringTag)),
&if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
assembler->Bind(&if_bothonebyteseqstrings);
{
// Compute the effective offset of the first character.
Node* begin = assembler->IntPtrConstant(
SeqOneByteString::kHeaderSize - kHeapObjectTag);
// Compute the first offset after the string from the length.
Node* end =
assembler->IntPtrAdd(begin, assembler->SmiUntag(lhs_length));
// Loop over the {lhs} and {rhs} strings to see if they are equal.
Variable var_offset(assembler, MachineType::PointerRepresentation());
Label loop(assembler, &var_offset);
var_offset.Bind(begin);
assembler->Goto(&loop);
assembler->Bind(&loop);
{
// Check if {offset} equals {end}.
Node* offset = var_offset.value();
Label if_done(assembler), if_notdone(assembler);
assembler->Branch(assembler->WordEqual(offset, end), &if_done,
&if_notdone);
assembler->Bind(&if_notdone);
{
// Load the next characters from {lhs} and {rhs}.
Node* lhs_value =
assembler->Load(MachineType::Uint8(), lhs, offset);
Node* rhs_value =
assembler->Load(MachineType::Uint8(), rhs, offset);
// Check if the characters match.
Label if_valueissame(assembler), if_valueisnotsame(assembler);
assembler->Branch(assembler->Word32Equal(lhs_value, rhs_value),
&if_valueissame, &if_valueisnotsame);
assembler->Bind(&if_valueissame);
{
// Advance to next character.
var_offset.Bind(
assembler->IntPtrAdd(offset, assembler->IntPtrConstant(1)));
}
assembler->Goto(&loop);
assembler->Bind(&if_valueisnotsame);
assembler->Goto(&if_notequal);
}
assembler->Bind(&if_done);
assembler->Goto(&if_equal);
}
// Advance to next character.
var_offset.Bind(IntPtrAdd(offset, IntPtrConstant(1)));
Goto(&loop);
}
}
assembler->Bind(&if_notbothonebyteseqstrings);
Bind(&if_notbothonebyteseqstrings);
{
// TODO(bmeurer): Add fast case support for flattened cons strings;
// also add support for two byte string equality checks.
......@@ -214,407 +169,328 @@ void GenerateStringEqual(CodeStubAssembler* assembler, ResultMode mode) {
(mode == ResultMode::kDontNegateResult)
? Runtime::kStringEqual
: Runtime::kStringNotEqual;
assembler->TailCallRuntime(function_id, context, lhs, rhs);
TailCallRuntime(function_id, context, lhs, rhs);
}
}
}
assembler->Bind(&if_lengthisnotequal);
{
// Mismatch in length of {lhs} and {rhs}, cannot be equal.
assembler->Goto(&if_notequal);
}
}
assembler->Bind(&if_equal);
assembler->Return(
assembler->BooleanConstant(mode == ResultMode::kDontNegateResult));
Bind(&if_equal);
Return(BooleanConstant(mode == ResultMode::kDontNegateResult));
assembler->Bind(&if_notequal);
assembler->Return(
assembler->BooleanConstant(mode == ResultMode::kNegateResult));
Bind(&if_notequal);
Return(BooleanConstant(mode == ResultMode::kNegateResult));
}
void StringBuiltinsAssembler::GenerateStringRelationalComparison(
RelationalComparisonMode mode) {
Node* lhs = Parameter(0);
Node* rhs = Parameter(1);
Node* context = Parameter(2);
void GenerateStringRelationalComparison(CodeStubAssembler* assembler,
RelationalComparisonMode mode) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
Label if_less(this), if_equal(this), if_greater(this);
Node* lhs = assembler->Parameter(0);
Node* rhs = assembler->Parameter(1);
Node* context = assembler->Parameter(2);
// Fast check to see if {lhs} and {rhs} refer to the same String object.
GotoIf(WordEqual(lhs, rhs), &if_equal);
// Load instance types of {lhs} and {rhs}.
Node* lhs_instance_type = LoadInstanceType(lhs);
Node* rhs_instance_type = LoadInstanceType(rhs);
// Combine the instance types into a single 16-bit value, so we can check
// both of them at once.
Node* both_instance_types = Word32Or(
lhs_instance_type, Word32Shl(rhs_instance_type, Int32Constant(8)));
// Check that both {lhs} and {rhs} are flat one-byte strings.
int const kBothSeqOneByteStringMask =
kStringEncodingMask | kStringRepresentationMask |
((kStringEncodingMask | kStringRepresentationMask) << 8);
int const kBothSeqOneByteStringTag =
kOneByteStringTag | kSeqStringTag |
((kOneByteStringTag | kSeqStringTag) << 8);
Label if_bothonebyteseqstrings(this), if_notbothonebyteseqstrings(this);
Branch(Word32Equal(Word32And(both_instance_types,
Int32Constant(kBothSeqOneByteStringMask)),
Int32Constant(kBothSeqOneByteStringTag)),
&if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
Bind(&if_bothonebyteseqstrings);
{
// Load the length of {lhs} and {rhs}.
Node* lhs_length = LoadStringLength(lhs);
Node* rhs_length = LoadStringLength(rhs);
Label if_less(assembler), if_equal(assembler), if_greater(assembler);
// Determine the minimum length.
Node* length = SmiMin(lhs_length, rhs_length);
// Fast check to see if {lhs} and {rhs} refer to the same String object.
Label if_same(assembler), if_notsame(assembler);
assembler->Branch(assembler->WordEqual(lhs, rhs), &if_same, &if_notsame);
// Compute the effective offset of the first character.
Node* begin =
IntPtrConstant(SeqOneByteString::kHeaderSize - kHeapObjectTag);
assembler->Bind(&if_same);
assembler->Goto(&if_equal);
// Compute the first offset after the string from the length.
Node* end = IntPtrAdd(begin, SmiUntag(length));
assembler->Bind(&if_notsame);
{
// Load instance types of {lhs} and {rhs}.
Node* lhs_instance_type = assembler->LoadInstanceType(lhs);
Node* rhs_instance_type = assembler->LoadInstanceType(rhs);
// Combine the instance types into a single 16-bit value, so we can check
// both of them at once.
Node* both_instance_types = assembler->Word32Or(
lhs_instance_type,
assembler->Word32Shl(rhs_instance_type, assembler->Int32Constant(8)));
// Check that both {lhs} and {rhs} are flat one-byte strings.
int const kBothSeqOneByteStringMask =
kStringEncodingMask | kStringRepresentationMask |
((kStringEncodingMask | kStringRepresentationMask) << 8);
int const kBothSeqOneByteStringTag =
kOneByteStringTag | kSeqStringTag |
((kOneByteStringTag | kSeqStringTag) << 8);
Label if_bothonebyteseqstrings(assembler),
if_notbothonebyteseqstrings(assembler);
assembler->Branch(assembler->Word32Equal(
assembler->Word32And(both_instance_types,
assembler->Int32Constant(
kBothSeqOneByteStringMask)),
assembler->Int32Constant(kBothSeqOneByteStringTag)),
&if_bothonebyteseqstrings, &if_notbothonebyteseqstrings);
assembler->Bind(&if_bothonebyteseqstrings);
// Loop over the {lhs} and {rhs} strings to see if they are equal.
Variable var_offset(this, MachineType::PointerRepresentation());
Label loop(this, &var_offset);
var_offset.Bind(begin);
Goto(&loop);
Bind(&loop);
{
// Load the length of {lhs} and {rhs}.
Node* lhs_length = assembler->LoadStringLength(lhs);
Node* rhs_length = assembler->LoadStringLength(rhs);
// Determine the minimum length.
Node* length = assembler->SmiMin(lhs_length, rhs_length);
// Compute the effective offset of the first character.
Node* begin = assembler->IntPtrConstant(SeqOneByteString::kHeaderSize -
kHeapObjectTag);
// Compute the first offset after the string from the length.
Node* end = assembler->IntPtrAdd(begin, assembler->SmiUntag(length));
// Loop over the {lhs} and {rhs} strings to see if they are equal.
Variable var_offset(assembler, MachineType::PointerRepresentation());
Label loop(assembler, &var_offset);
var_offset.Bind(begin);
assembler->Goto(&loop);
assembler->Bind(&loop);
// Check if {offset} equals {end}.
Node* offset = var_offset.value();
Label if_done(this), if_notdone(this);
Branch(WordEqual(offset, end), &if_done, &if_notdone);
Bind(&if_notdone);
{
// Check if {offset} equals {end}.
Node* offset = var_offset.value();
Label if_done(assembler), if_notdone(assembler);
assembler->Branch(assembler->WordEqual(offset, end), &if_done,
&if_notdone);
// Load the next characters from {lhs} and {rhs}.
Node* lhs_value = Load(MachineType::Uint8(), lhs, offset);
Node* rhs_value = Load(MachineType::Uint8(), rhs, offset);
assembler->Bind(&if_notdone);
{
// Load the next characters from {lhs} and {rhs}.
Node* lhs_value = assembler->Load(MachineType::Uint8(), lhs, offset);
Node* rhs_value = assembler->Load(MachineType::Uint8(), rhs, offset);
// Check if the characters match.
Label if_valueissame(assembler), if_valueisnotsame(assembler);
assembler->Branch(assembler->Word32Equal(lhs_value, rhs_value),
&if_valueissame, &if_valueisnotsame);
assembler->Bind(&if_valueissame);
{
// Advance to next character.
var_offset.Bind(
assembler->IntPtrAdd(offset, assembler->IntPtrConstant(1)));
}
assembler->Goto(&loop);
assembler->Bind(&if_valueisnotsame);
assembler->Branch(assembler->Uint32LessThan(lhs_value, rhs_value),
&if_less, &if_greater);
}
// Check if the characters match.
Label if_valueissame(this), if_valueisnotsame(this);
Branch(Word32Equal(lhs_value, rhs_value), &if_valueissame,
&if_valueisnotsame);
assembler->Bind(&if_done);
Bind(&if_valueissame);
{
// All characters up to the min length are equal, decide based on
// string length.
Label if_lengthisequal(assembler), if_lengthisnotequal(assembler);
assembler->Branch(assembler->SmiEqual(lhs_length, rhs_length),
&if_lengthisequal, &if_lengthisnotequal);
assembler->Bind(&if_lengthisequal);
assembler->Goto(&if_equal);
assembler->Bind(&if_lengthisnotequal);
assembler->BranchIfSmiLessThan(lhs_length, rhs_length, &if_less,
&if_greater);
// Advance to next character.
var_offset.Bind(IntPtrAdd(offset, IntPtrConstant(1)));
}
Goto(&loop);
Bind(&if_valueisnotsame);
Branch(Uint32LessThan(lhs_value, rhs_value), &if_less, &if_greater);
}
Bind(&if_done);
{
// All characters up to the min length are equal, decide based on
// string length.
GotoIf(SmiEqual(lhs_length, rhs_length), &if_equal);
BranchIfSmiLessThan(lhs_length, rhs_length, &if_less, &if_greater);
}
}
}
assembler->Bind(&if_notbothonebyteseqstrings);
Bind(&if_notbothonebyteseqstrings);
{
// TODO(bmeurer): Add fast case support for flattened cons strings;
// also add support for two byte string relational comparisons.
switch (mode) {
case RelationalComparisonMode::kLessThan:
assembler->TailCallRuntime(Runtime::kStringLessThan, context, lhs,
rhs);
TailCallRuntime(Runtime::kStringLessThan, context, lhs, rhs);
break;
case RelationalComparisonMode::kLessThanOrEqual:
assembler->TailCallRuntime(Runtime::kStringLessThanOrEqual, context,
lhs, rhs);
TailCallRuntime(Runtime::kStringLessThanOrEqual, context, lhs, rhs);
break;
case RelationalComparisonMode::kGreaterThan:
assembler->TailCallRuntime(Runtime::kStringGreaterThan, context, lhs,
rhs);
TailCallRuntime(Runtime::kStringGreaterThan, context, lhs, rhs);
break;
case RelationalComparisonMode::kGreaterThanOrEqual:
assembler->TailCallRuntime(Runtime::kStringGreaterThanOrEqual,
context, lhs, rhs);
TailCallRuntime(Runtime::kStringGreaterThanOrEqual, context, lhs,
rhs);
break;
}
}
}
assembler->Bind(&if_less);
switch (mode) {
case RelationalComparisonMode::kLessThan:
case RelationalComparisonMode::kLessThanOrEqual:
assembler->Return(assembler->BooleanConstant(true));
break;
Bind(&if_less);
switch (mode) {
case RelationalComparisonMode::kLessThan:
case RelationalComparisonMode::kLessThanOrEqual:
Return(BooleanConstant(true));
break;
case RelationalComparisonMode::kGreaterThan:
case RelationalComparisonMode::kGreaterThanOrEqual:
assembler->Return(assembler->BooleanConstant(false));
break;
case RelationalComparisonMode::kGreaterThan:
case RelationalComparisonMode::kGreaterThanOrEqual:
Return(BooleanConstant(false));
break;
}
assembler->Bind(&if_equal);
Bind(&if_equal);
switch (mode) {
case RelationalComparisonMode::kLessThan:
case RelationalComparisonMode::kGreaterThan:
assembler->Return(assembler->BooleanConstant(false));
Return(BooleanConstant(false));
break;
case RelationalComparisonMode::kLessThanOrEqual:
case RelationalComparisonMode::kGreaterThanOrEqual:
assembler->Return(assembler->BooleanConstant(true));
Return(BooleanConstant(true));
break;
}
assembler->Bind(&if_greater);
Bind(&if_greater);
switch (mode) {
case RelationalComparisonMode::kLessThan:
case RelationalComparisonMode::kLessThanOrEqual:
assembler->Return(assembler->BooleanConstant(false));
Return(BooleanConstant(false));
break;
case RelationalComparisonMode::kGreaterThan:
case RelationalComparisonMode::kGreaterThanOrEqual:
assembler->Return(assembler->BooleanConstant(true));
Return(BooleanConstant(true));
break;
}
}
} // namespace
// static
void Builtins::Generate_StringEqual(compiler::CodeAssemblerState* state) {
CodeStubAssembler assembler(state);
GenerateStringEqual(&assembler, ResultMode::kDontNegateResult);
TF_BUILTIN(StringEqual, StringBuiltinsAssembler) {
GenerateStringEqual(ResultMode::kDontNegateResult);
}
// static
void Builtins::Generate_StringNotEqual(compiler::CodeAssemblerState* state) {
CodeStubAssembler assembler(state);
GenerateStringEqual(&assembler, ResultMode::kNegateResult);
TF_BUILTIN(StringNotEqual, StringBuiltinsAssembler) {
GenerateStringEqual(ResultMode::kNegateResult);
}
// static
void Builtins::Generate_StringLessThan(compiler::CodeAssemblerState* state) {
CodeStubAssembler assembler(state);
GenerateStringRelationalComparison(&assembler,
RelationalComparisonMode::kLessThan);
TF_BUILTIN(StringLessThan, StringBuiltinsAssembler) {
GenerateStringRelationalComparison(RelationalComparisonMode::kLessThan);
}
// static
void Builtins::Generate_StringLessThanOrEqual(
compiler::CodeAssemblerState* state) {
CodeStubAssembler assembler(state);
TF_BUILTIN(StringLessThanOrEqual, StringBuiltinsAssembler) {
GenerateStringRelationalComparison(
&assembler, RelationalComparisonMode::kLessThanOrEqual);
RelationalComparisonMode::kLessThanOrEqual);
}
// static
void Builtins::Generate_StringGreaterThan(compiler::CodeAssemblerState* state) {
CodeStubAssembler assembler(state);
GenerateStringRelationalComparison(&assembler,
RelationalComparisonMode::kGreaterThan);
TF_BUILTIN(StringGreaterThan, StringBuiltinsAssembler) {
GenerateStringRelationalComparison(RelationalComparisonMode::kGreaterThan);
}
// static
void Builtins::Generate_StringGreaterThanOrEqual(
compiler::CodeAssemblerState* state) {
CodeStubAssembler assembler(state);
TF_BUILTIN(StringGreaterThanOrEqual, StringBuiltinsAssembler) {
GenerateStringRelationalComparison(
&assembler, RelationalComparisonMode::kGreaterThanOrEqual);
RelationalComparisonMode::kGreaterThanOrEqual);
}
// static
void Builtins::Generate_StringCharAt(compiler::CodeAssemblerState* state) {
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
Node* receiver = assembler.Parameter(0);
Node* position = assembler.Parameter(1);
TF_BUILTIN(StringCharAt, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* position = Parameter(1);
// Load the character code at the {position} from the {receiver}.
Node* code = assembler.StringCharCodeAt(receiver, position,
CodeStubAssembler::INTPTR_PARAMETERS);
Node* code = StringCharCodeAt(receiver, position,
CodeStubAssembler::INTPTR_PARAMETERS);
// And return the single character string with only that {code}
Node* result = assembler.StringFromCharCode(code);
assembler.Return(result);
Node* result = StringFromCharCode(code);
Return(result);
}
// static
void Builtins::Generate_StringCharCodeAt(compiler::CodeAssemblerState* state) {
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
Node* receiver = assembler.Parameter(0);
Node* position = assembler.Parameter(1);
TF_BUILTIN(StringCharCodeAt, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* position = Parameter(1);
// Load the character code at the {position} from the {receiver}.
Node* code = assembler.StringCharCodeAt(receiver, position,
CodeStubAssembler::INTPTR_PARAMETERS);
Node* code = StringCharCodeAt(receiver, position,
CodeStubAssembler::INTPTR_PARAMETERS);
// And return it as TaggedSigned value.
// TODO(turbofan): Allow builtins to return values untagged.
Node* result = assembler.SmiFromWord32(code);
assembler.Return(result);
Node* result = SmiFromWord32(code);
Return(result);
}
// -----------------------------------------------------------------------------
// ES6 section 21.1 String Objects
// ES6 section 21.1.2.1 String.fromCharCode ( ...codeUnits )
void Builtins::Generate_StringFromCharCode(
compiler::CodeAssemblerState* state) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
CodeStubAssembler assembler(state);
Node* argc = assembler.Parameter(BuiltinDescriptor::kArgumentsCount);
Node* context = assembler.Parameter(BuiltinDescriptor::kContext);
TF_BUILTIN(StringFromCharCode, CodeStubAssembler) {
Node* argc = Parameter(BuiltinDescriptor::kArgumentsCount);
Node* context = Parameter(BuiltinDescriptor::kContext);
CodeStubArguments arguments(&assembler, argc);
CodeStubArguments arguments(this, argc);
// From now on use word-size argc value.
argc = arguments.GetLength();
// Check if we have exactly one argument (plus the implicit receiver), i.e.
// if the parent frame is not an arguments adaptor frame.
Label if_oneargument(&assembler), if_notoneargument(&assembler);
assembler.Branch(assembler.WordEqual(argc, assembler.IntPtrConstant(1)),
&if_oneargument, &if_notoneargument);
Label if_oneargument(this), if_notoneargument(this);
Branch(WordEqual(argc, IntPtrConstant(1)), &if_oneargument,
&if_notoneargument);
assembler.Bind(&if_oneargument);
Bind(&if_oneargument);
{
// Single argument case, perform fast single character string cache lookup
// for one-byte code units, or fall back to creating a single character
// string on the fly otherwise.
Node* code = arguments.AtIndex(0);
Node* code32 = assembler.TruncateTaggedToWord32(context, code);
Node* code16 = assembler.Word32And(
code32, assembler.Int32Constant(String::kMaxUtf16CodeUnit));
Node* result = assembler.StringFromCharCode(code16);
Node* code32 = TruncateTaggedToWord32(context, code);
Node* code16 = Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
Node* result = StringFromCharCode(code16);
arguments.PopAndReturn(result);
}
Node* code16 = nullptr;
assembler.Bind(&if_notoneargument);
Bind(&if_notoneargument);
{
Label two_byte(&assembler);
Label two_byte(this);
// Assume that the resulting string contains only one-byte characters.
Node* one_byte_result = assembler.AllocateSeqOneByteString(context, argc);
Node* one_byte_result = AllocateSeqOneByteString(context, argc);
Variable max_index(&assembler, MachineType::PointerRepresentation());
max_index.Bind(assembler.IntPtrConstant(0));
Variable max_index(this, MachineType::PointerRepresentation());
max_index.Bind(IntPtrConstant(0));
// Iterate over the incoming arguments, converting them to 8-bit character
// codes. Stop if any of the conversions generates a code that doesn't fit
// in 8 bits.
CodeStubAssembler::VariableList vars({&max_index}, assembler.zone());
arguments.ForEach(vars, [&assembler, context, &two_byte, &max_index,
&code16, one_byte_result](Node* arg) {
Node* code32 = assembler.TruncateTaggedToWord32(context, arg);
code16 = assembler.Word32And(
code32, assembler.Int32Constant(String::kMaxUtf16CodeUnit));
assembler.GotoIf(
assembler.Int32GreaterThan(
code16, assembler.Int32Constant(String::kMaxOneByteCharCode)),
CodeStubAssembler::VariableList vars({&max_index}, zone());
arguments.ForEach(vars, [this, context, &two_byte, &max_index, &code16,
one_byte_result](Node* arg) {
Node* code32 = TruncateTaggedToWord32(context, arg);
code16 = Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
GotoIf(
Int32GreaterThan(code16, Int32Constant(String::kMaxOneByteCharCode)),
&two_byte);
// The {code16} fits into the SeqOneByteString {one_byte_result}.
Node* offset = assembler.ElementOffsetFromIndex(
Node* offset = ElementOffsetFromIndex(
max_index.value(), UINT8_ELEMENTS,
CodeStubAssembler::INTPTR_PARAMETERS,
SeqOneByteString::kHeaderSize - kHeapObjectTag);
assembler.StoreNoWriteBarrier(MachineRepresentation::kWord8,
one_byte_result, offset, code16);
max_index.Bind(
assembler.IntPtrAdd(max_index.value(), assembler.IntPtrConstant(1)));
StoreNoWriteBarrier(MachineRepresentation::kWord8, one_byte_result,
offset, code16);
max_index.Bind(IntPtrAdd(max_index.value(), IntPtrConstant(1)));
});
arguments.PopAndReturn(one_byte_result);
assembler.Bind(&two_byte);
Bind(&two_byte);
// At least one of the characters in the string requires a 16-bit
// representation. Allocate a SeqTwoByteString to hold the resulting
// string.
Node* two_byte_result = assembler.AllocateSeqTwoByteString(context, argc);
Node* two_byte_result = AllocateSeqTwoByteString(context, argc);
// Copy the characters that have already been put in the 8-bit string into
// their corresponding positions in the new 16-bit string.
Node* zero = assembler.IntPtrConstant(0);
assembler.CopyStringCharacters(one_byte_result, two_byte_result, zero, zero,
max_index.value(), String::ONE_BYTE_ENCODING,
String::TWO_BYTE_ENCODING,
CodeStubAssembler::INTPTR_PARAMETERS);
Node* zero = IntPtrConstant(0);
CopyStringCharacters(one_byte_result, two_byte_result, zero, zero,
max_index.value(), String::ONE_BYTE_ENCODING,
String::TWO_BYTE_ENCODING,
CodeStubAssembler::INTPTR_PARAMETERS);
// Write the character that caused the 8-bit to 16-bit fault.
Node* max_index_offset = assembler.ElementOffsetFromIndex(
max_index.value(), UINT16_ELEMENTS,
CodeStubAssembler::INTPTR_PARAMETERS,
SeqTwoByteString::kHeaderSize - kHeapObjectTag);
assembler.StoreNoWriteBarrier(MachineRepresentation::kWord16,
two_byte_result, max_index_offset, code16);
max_index.Bind(
assembler.IntPtrAdd(max_index.value(), assembler.IntPtrConstant(1)));
Node* max_index_offset =
ElementOffsetFromIndex(max_index.value(), UINT16_ELEMENTS,
CodeStubAssembler::INTPTR_PARAMETERS,
SeqTwoByteString::kHeaderSize - kHeapObjectTag);
StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
max_index_offset, code16);
max_index.Bind(IntPtrAdd(max_index.value(), IntPtrConstant(1)));
// Resume copying the passed-in arguments from the same place where the
// 8-bit copy stopped, but this time copying over all of the characters
// using a 16-bit representation.
arguments.ForEach(
vars,
[&assembler, context, two_byte_result, &max_index](Node* arg) {
Node* code32 = assembler.TruncateTaggedToWord32(context, arg);
Node* code16 = assembler.Word32And(
code32, assembler.Int32Constant(String::kMaxUtf16CodeUnit));
[this, context, two_byte_result, &max_index](Node* arg) {
Node* code32 = TruncateTaggedToWord32(context, arg);
Node* code16 =
Word32And(code32, Int32Constant(String::kMaxUtf16CodeUnit));
Node* offset = assembler.ElementOffsetFromIndex(
Node* offset = ElementOffsetFromIndex(
max_index.value(), UINT16_ELEMENTS,
CodeStubAssembler::INTPTR_PARAMETERS,
SeqTwoByteString::kHeaderSize - kHeapObjectTag);
assembler.StoreNoWriteBarrier(MachineRepresentation::kWord16,
two_byte_result, offset, code16);
max_index.Bind(assembler.IntPtrAdd(max_index.value(),
assembler.IntPtrConstant(1)));
StoreNoWriteBarrier(MachineRepresentation::kWord16, two_byte_result,
offset, code16);
max_index.Bind(IntPtrAdd(max_index.value(), IntPtrConstant(1)));
},
max_index.value());
......@@ -716,93 +592,79 @@ BUILTIN(StringFromCodePoint) {
}
// ES6 section 21.1.3.1 String.prototype.charAt ( pos )
void Builtins::Generate_StringPrototypeCharAt(
compiler::CodeAssemblerState* state) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
Node* receiver = assembler.Parameter(0);
Node* position = assembler.Parameter(1);
Node* context = assembler.Parameter(4);
TF_BUILTIN(StringPrototypeCharAt, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* position = Parameter(1);
Node* context = Parameter(4);
// Check that {receiver} is coercible to Object and convert it to a String.
receiver =
assembler.ToThisString(context, receiver, "String.prototype.charAt");
receiver = ToThisString(context, receiver, "String.prototype.charAt");
// Convert the {position} to a Smi and check that it's in bounds of the
// {receiver}.
{
Label return_emptystring(&assembler, Label::kDeferred);
position = assembler.ToInteger(context, position,
CodeStubAssembler::kTruncateMinusZero);
assembler.GotoUnless(assembler.TaggedIsSmi(position), &return_emptystring);
Label return_emptystring(this, Label::kDeferred);
position =
ToInteger(context, position, CodeStubAssembler::kTruncateMinusZero);
GotoUnless(TaggedIsSmi(position), &return_emptystring);
// Determine the actual length of the {receiver} String.
Node* receiver_length =
assembler.LoadObjectField(receiver, String::kLengthOffset);
Node* receiver_length = LoadObjectField(receiver, String::kLengthOffset);
// Return "" if the Smi {position} is outside the bounds of the {receiver}.
Label if_positioninbounds(&assembler);
assembler.Branch(assembler.SmiAboveOrEqual(position, receiver_length),
&return_emptystring, &if_positioninbounds);
Label if_positioninbounds(this);
Branch(SmiAboveOrEqual(position, receiver_length), &return_emptystring,
&if_positioninbounds);
assembler.Bind(&return_emptystring);
assembler.Return(assembler.EmptyStringConstant());
Bind(&return_emptystring);
Return(EmptyStringConstant());
assembler.Bind(&if_positioninbounds);
Bind(&if_positioninbounds);
}
// Load the character code at the {position} from the {receiver}.
Node* code = assembler.StringCharCodeAt(receiver, position);
Node* code = StringCharCodeAt(receiver, position);
// And return the single character string with only that {code}.
Node* result = assembler.StringFromCharCode(code);
assembler.Return(result);
Node* result = StringFromCharCode(code);
Return(result);
}
// ES6 section 21.1.3.2 String.prototype.charCodeAt ( pos )
void Builtins::Generate_StringPrototypeCharCodeAt(
compiler::CodeAssemblerState* state) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
Node* receiver = assembler.Parameter(0);
Node* position = assembler.Parameter(1);
Node* context = assembler.Parameter(4);
TF_BUILTIN(StringPrototypeCharCodeAt, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* position = Parameter(1);
Node* context = Parameter(4);
// Check that {receiver} is coercible to Object and convert it to a String.
receiver =
assembler.ToThisString(context, receiver, "String.prototype.charCodeAt");
receiver = ToThisString(context, receiver, "String.prototype.charCodeAt");
// Convert the {position} to a Smi and check that it's in bounds of the
// {receiver}.
{
Label return_nan(&assembler, Label::kDeferred);
position = assembler.ToInteger(context, position,
CodeStubAssembler::kTruncateMinusZero);
assembler.GotoUnless(assembler.TaggedIsSmi(position), &return_nan);
Label return_nan(this, Label::kDeferred);
position =
ToInteger(context, position, CodeStubAssembler::kTruncateMinusZero);
GotoUnless(TaggedIsSmi(position), &return_nan);
// Determine the actual length of the {receiver} String.
Node* receiver_length =
assembler.LoadObjectField(receiver, String::kLengthOffset);
Node* receiver_length = LoadObjectField(receiver, String::kLengthOffset);
// Return NaN if the Smi {position} is outside the bounds of the {receiver}.
Label if_positioninbounds(&assembler);
assembler.Branch(assembler.SmiAboveOrEqual(position, receiver_length),
&return_nan, &if_positioninbounds);
Label if_positioninbounds(this);
Branch(SmiAboveOrEqual(position, receiver_length), &return_nan,
&if_positioninbounds);
assembler.Bind(&return_nan);
assembler.Return(assembler.NaNConstant());
Bind(&return_nan);
Return(NaNConstant());
assembler.Bind(&if_positioninbounds);
Bind(&if_positioninbounds);
}
// Load the character at the {position} from the {receiver}.
Node* value = assembler.StringCharCodeAt(receiver, position);
Node* result = assembler.SmiFromWord32(value);
assembler.Return(result);
Node* value = StringCharCodeAt(receiver, position);
Node* result = SmiFromWord32(value);
Return(result);
}
// ES6 section 21.1.3.6
......@@ -1124,241 +986,220 @@ BUILTIN(StringPrototypeNormalize) {
}
// ES6 section B.2.3.1 String.prototype.substr ( start, length )
void Builtins::Generate_StringPrototypeSubstr(
compiler::CodeAssemblerState* state) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
CodeStubAssembler a(state);
TF_BUILTIN(StringPrototypeSubstr, CodeStubAssembler) {
Label out(this), handle_length(this);
Label out(&a), handle_length(&a);
Variable var_start(this, MachineRepresentation::kTagged);
Variable var_length(this, MachineRepresentation::kTagged);
Variable var_start(&a, MachineRepresentation::kTagged);
Variable var_length(&a, MachineRepresentation::kTagged);
Node* const receiver = Parameter(0);
Node* const start = Parameter(1);
Node* const length = Parameter(2);
Node* const context = Parameter(5);
Node* const receiver = a.Parameter(0);
Node* const start = a.Parameter(1);
Node* const length = a.Parameter(2);
Node* const context = a.Parameter(5);
Node* const zero = a.SmiConstant(Smi::kZero);
Node* const zero = SmiConstant(Smi::kZero);
// Check that {receiver} is coercible to Object and convert it to a String.
Node* const string =
a.ToThisString(context, receiver, "String.prototype.substr");
ToThisString(context, receiver, "String.prototype.substr");
Node* const string_length = a.LoadStringLength(string);
Node* const string_length = LoadStringLength(string);
// Conversions and bounds-checks for {start}.
{
Node* const start_int =
a.ToInteger(context, start, CodeStubAssembler::kTruncateMinusZero);
ToInteger(context, start, CodeStubAssembler::kTruncateMinusZero);
Label if_issmi(&a), if_isheapnumber(&a, Label::kDeferred);
a.Branch(a.TaggedIsSmi(start_int), &if_issmi, &if_isheapnumber);
Label if_issmi(this), if_isheapnumber(this, Label::kDeferred);
Branch(TaggedIsSmi(start_int), &if_issmi, &if_isheapnumber);
a.Bind(&if_issmi);
Bind(&if_issmi);
{
Node* const length_plus_start = a.SmiAdd(string_length, start_int);
var_start.Bind(a.Select(a.SmiLessThan(start_int, zero),
[&] { return a.SmiMax(length_plus_start, zero); },
[&] { return start_int; },
MachineRepresentation::kTagged));
a.Goto(&handle_length);
Node* const length_plus_start = SmiAdd(string_length, start_int);
var_start.Bind(Select(SmiLessThan(start_int, zero),
[&] { return SmiMax(length_plus_start, zero); },
[&] { return start_int; },
MachineRepresentation::kTagged));
Goto(&handle_length);
}
a.Bind(&if_isheapnumber);
Bind(&if_isheapnumber);
{
// If {start} is a heap number, it is definitely out of bounds. If it is
// negative, {start} = max({string_length} + {start}),0) = 0'. If it is
// positive, set {start} to {string_length} which ultimately results in
// returning an empty string.
Node* const float_zero = a.Float64Constant(0.);
Node* const start_float = a.LoadHeapNumberValue(start_int);
var_start.Bind(a.SelectTaggedConstant(
a.Float64LessThan(start_float, float_zero), zero, string_length));
a.Goto(&handle_length);
Node* const float_zero = Float64Constant(0.);
Node* const start_float = LoadHeapNumberValue(start_int);
var_start.Bind(SelectTaggedConstant(
Float64LessThan(start_float, float_zero), zero, string_length));
Goto(&handle_length);
}
}
// Conversions and bounds-checks for {length}.
a.Bind(&handle_length);
Bind(&handle_length);
{
Label if_issmi(&a), if_isheapnumber(&a, Label::kDeferred);
Label if_issmi(this), if_isheapnumber(this, Label::kDeferred);
// Default to {string_length} if {length} is undefined.
{
Label if_isundefined(&a, Label::kDeferred), if_isnotundefined(&a);
a.Branch(a.WordEqual(length, a.UndefinedConstant()), &if_isundefined,
&if_isnotundefined);
Label if_isundefined(this, Label::kDeferred), if_isnotundefined(this);
Branch(WordEqual(length, UndefinedConstant()), &if_isundefined,
&if_isnotundefined);
a.Bind(&if_isundefined);
Bind(&if_isundefined);
var_length.Bind(string_length);
a.Goto(&if_issmi);
Goto(&if_issmi);
a.Bind(&if_isnotundefined);
Bind(&if_isnotundefined);
var_length.Bind(
a.ToInteger(context, length, CodeStubAssembler::kTruncateMinusZero));
ToInteger(context, length, CodeStubAssembler::kTruncateMinusZero));
}
a.Branch(a.TaggedIsSmi(var_length.value()), &if_issmi, &if_isheapnumber);
Branch(TaggedIsSmi(var_length.value()), &if_issmi, &if_isheapnumber);
// Set {length} to min(max({length}, 0), {string_length} - {start}
a.Bind(&if_issmi);
Bind(&if_issmi);
{
Node* const positive_length = a.SmiMax(var_length.value(), zero);
Node* const positive_length = SmiMax(var_length.value(), zero);
Node* const minimal_length = a.SmiSub(string_length, var_start.value());
var_length.Bind(a.SmiMin(positive_length, minimal_length));
Node* const minimal_length = SmiSub(string_length, var_start.value());
var_length.Bind(SmiMin(positive_length, minimal_length));
a.GotoUnless(a.SmiLessThanOrEqual(var_length.value(), zero), &out);
a.Return(a.EmptyStringConstant());
GotoUnless(SmiLessThanOrEqual(var_length.value(), zero), &out);
Return(EmptyStringConstant());
}
a.Bind(&if_isheapnumber);
Bind(&if_isheapnumber);
{
// If {length} is a heap number, it is definitely out of bounds. There are
// two cases according to the spec: if it is negative, "" is returned; if
// it is positive, then length is set to {string_length} - {start}.
CSA_ASSERT(&a, a.IsHeapNumberMap(a.LoadMap(var_length.value())));
CSA_ASSERT(this, IsHeapNumberMap(LoadMap(var_length.value())));
Label if_isnegative(&a), if_ispositive(&a);
Node* const float_zero = a.Float64Constant(0.);
Node* const length_float = a.LoadHeapNumberValue(var_length.value());
a.Branch(a.Float64LessThan(length_float, float_zero), &if_isnegative,
&if_ispositive);
Label if_isnegative(this), if_ispositive(this);
Node* const float_zero = Float64Constant(0.);
Node* const length_float = LoadHeapNumberValue(var_length.value());
Branch(Float64LessThan(length_float, float_zero), &if_isnegative,
&if_ispositive);
a.Bind(&if_isnegative);
a.Return(a.EmptyStringConstant());
Bind(&if_isnegative);
Return(EmptyStringConstant());
a.Bind(&if_ispositive);
Bind(&if_ispositive);
{
var_length.Bind(a.SmiSub(string_length, var_start.value()));
a.GotoUnless(a.SmiLessThanOrEqual(var_length.value(), zero), &out);
a.Return(a.EmptyStringConstant());
var_length.Bind(SmiSub(string_length, var_start.value()));
GotoUnless(SmiLessThanOrEqual(var_length.value(), zero), &out);
Return(EmptyStringConstant());
}
}
}
a.Bind(&out);
Bind(&out);
{
Node* const end = a.SmiAdd(var_start.value(), var_length.value());
Node* const result = a.SubString(context, string, var_start.value(), end);
a.Return(result);
Node* const end = SmiAdd(var_start.value(), var_length.value());
Node* const result = SubString(context, string, var_start.value(), end);
Return(result);
}
}
namespace {
compiler::Node* ToSmiBetweenZeroAnd(CodeStubAssembler* a,
compiler::Node* context,
compiler::Node* value,
compiler::Node* limit) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
Label out(a);
Variable var_result(a, MachineRepresentation::kTagged);
compiler::Node* StringBuiltinsAssembler::ToSmiBetweenZeroAnd(Node* context,
Node* value,
Node* limit) {
Label out(this);
Variable var_result(this, MachineRepresentation::kTagged);
Node* const value_int =
a->ToInteger(context, value, CodeStubAssembler::kTruncateMinusZero);
this->ToInteger(context, value, CodeStubAssembler::kTruncateMinusZero);
Label if_issmi(a), if_isnotsmi(a, Label::kDeferred);
a->Branch(a->TaggedIsSmi(value_int), &if_issmi, &if_isnotsmi);
Label if_issmi(this), if_isnotsmi(this, Label::kDeferred);
Branch(TaggedIsSmi(value_int), &if_issmi, &if_isnotsmi);
a->Bind(&if_issmi);
Bind(&if_issmi);
{
Label if_isinbounds(a), if_isoutofbounds(a, Label::kDeferred);
a->Branch(a->SmiAbove(value_int, limit), &if_isoutofbounds, &if_isinbounds);
Label if_isinbounds(this), if_isoutofbounds(this, Label::kDeferred);
Branch(SmiAbove(value_int, limit), &if_isoutofbounds, &if_isinbounds);
a->Bind(&if_isinbounds);
Bind(&if_isinbounds);
{
var_result.Bind(value_int);
a->Goto(&out);
Goto(&out);
}
a->Bind(&if_isoutofbounds);
Bind(&if_isoutofbounds);
{
Node* const zero = a->SmiConstant(Smi::kZero);
var_result.Bind(a->SelectTaggedConstant(a->SmiLessThan(value_int, zero),
zero, limit));
a->Goto(&out);
Node* const zero = SmiConstant(Smi::kZero);
var_result.Bind(
SelectTaggedConstant(SmiLessThan(value_int, zero), zero, limit));
Goto(&out);
}
}
a->Bind(&if_isnotsmi);
Bind(&if_isnotsmi);
{
// {value} is a heap number - in this case, it is definitely out of bounds.
CSA_ASSERT(a, a->IsHeapNumberMap(a->LoadMap(value_int)));
Node* const float_zero = a->Float64Constant(0.);
Node* const smi_zero = a->SmiConstant(Smi::kZero);
Node* const value_float = a->LoadHeapNumberValue(value_int);
var_result.Bind(a->SelectTaggedConstant(
a->Float64LessThan(value_float, float_zero), smi_zero, limit));
a->Goto(&out);
CSA_ASSERT(this, IsHeapNumberMap(LoadMap(value_int)));
Node* const float_zero = Float64Constant(0.);
Node* const smi_zero = SmiConstant(Smi::kZero);
Node* const value_float = LoadHeapNumberValue(value_int);
var_result.Bind(SelectTaggedConstant(
Float64LessThan(value_float, float_zero), smi_zero, limit));
Goto(&out);
}
a->Bind(&out);
Bind(&out);
return var_result.value();
}
} // namespace
// ES6 section 21.1.3.19 String.prototype.substring ( start, end )
void Builtins::Generate_StringPrototypeSubstring(
compiler::CodeAssemblerState* state) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
CodeStubAssembler a(state);
TF_BUILTIN(StringPrototypeSubstring, StringBuiltinsAssembler) {
Label out(this);
Label out(&a);
Variable var_start(this, MachineRepresentation::kTagged);
Variable var_end(this, MachineRepresentation::kTagged);
Variable var_start(&a, MachineRepresentation::kTagged);
Variable var_end(&a, MachineRepresentation::kTagged);
Node* const receiver = a.Parameter(0);
Node* const start = a.Parameter(1);
Node* const end = a.Parameter(2);
Node* const context = a.Parameter(5);
Node* const receiver = Parameter(0);
Node* const start = Parameter(1);
Node* const end = Parameter(2);
Node* const context = Parameter(5);
// Check that {receiver} is coercible to Object and convert it to a String.
Node* const string =
a.ToThisString(context, receiver, "String.prototype.substring");
ToThisString(context, receiver, "String.prototype.substring");
Node* const length = a.LoadStringLength(string);
Node* const length = LoadStringLength(string);
// Conversion and bounds-checks for {start}.
var_start.Bind(ToSmiBetweenZeroAnd(&a, context, start, length));
var_start.Bind(ToSmiBetweenZeroAnd(context, start, length));
// Conversion and bounds-checks for {end}.
{
var_end.Bind(length);
a.GotoIf(a.WordEqual(end, a.UndefinedConstant()), &out);
GotoIf(WordEqual(end, UndefinedConstant()), &out);
var_end.Bind(ToSmiBetweenZeroAnd(&a, context, end, length));
var_end.Bind(ToSmiBetweenZeroAnd(context, end, length));
Label if_endislessthanstart(&a);
a.Branch(a.SmiLessThan(var_end.value(), var_start.value()),
&if_endislessthanstart, &out);
Label if_endislessthanstart(this);
Branch(SmiLessThan(var_end.value(), var_start.value()),
&if_endislessthanstart, &out);
a.Bind(&if_endislessthanstart);
Bind(&if_endislessthanstart);
{
Node* const tmp = var_end.value();
var_end.Bind(var_start.value());
var_start.Bind(tmp);
a.Goto(&out);
Goto(&out);
}
}
a.Bind(&out);
Bind(&out);
{
Node* result =
a.SubString(context, string, var_start.value(), var_end.value());
a.Return(result);
SubString(context, string, var_start.value(), var_end.value());
Return(result);
}
}
......@@ -1410,17 +1251,13 @@ BUILTIN(StringPrototypeStartsWith) {
}
// ES6 section 21.1.3.25 String.prototype.toString ()
void Builtins::Generate_StringPrototypeToString(
compiler::CodeAssemblerState* state) {
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
Node* receiver = assembler.Parameter(0);
Node* context = assembler.Parameter(3);
TF_BUILTIN(StringPrototypeToString, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* context = Parameter(3);
Node* result = assembler.ToThisValue(
context, receiver, PrimitiveType::kString, "String.prototype.toString");
assembler.Return(result);
Node* result = ToThisValue(context, receiver, PrimitiveType::kString,
"String.prototype.toString");
Return(result);
}
// ES6 section 21.1.3.27 String.prototype.trim ()
......@@ -1445,105 +1282,82 @@ BUILTIN(StringPrototypeTrimRight) {
}
// ES6 section 21.1.3.28 String.prototype.valueOf ( )
void Builtins::Generate_StringPrototypeValueOf(
compiler::CodeAssemblerState* state) {
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
TF_BUILTIN(StringPrototypeValueOf, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* context = Parameter(3);
Node* receiver = assembler.Parameter(0);
Node* context = assembler.Parameter(3);
Node* result = assembler.ToThisValue(
context, receiver, PrimitiveType::kString, "String.prototype.valueOf");
assembler.Return(result);
Node* result = ToThisValue(context, receiver, PrimitiveType::kString,
"String.prototype.valueOf");
Return(result);
}
void Builtins::Generate_StringPrototypeIterator(
compiler::CodeAssemblerState* state) {
typedef compiler::Node Node;
CodeStubAssembler assembler(state);
Node* receiver = assembler.Parameter(0);
Node* context = assembler.Parameter(3);
Node* string = assembler.ToThisString(context, receiver,
"String.prototype[Symbol.iterator]");
Node* native_context = assembler.LoadNativeContext(context);
Node* map = assembler.LoadContextElement(native_context,
Context::STRING_ITERATOR_MAP_INDEX);
Node* iterator = assembler.Allocate(JSStringIterator::kSize);
assembler.StoreMapNoWriteBarrier(iterator, map);
assembler.StoreObjectFieldRoot(iterator, JSValue::kPropertiesOffset,
Heap::kEmptyFixedArrayRootIndex);
assembler.StoreObjectFieldRoot(iterator, JSObject::kElementsOffset,
Heap::kEmptyFixedArrayRootIndex);
assembler.StoreObjectFieldNoWriteBarrier(
iterator, JSStringIterator::kStringOffset, string);
Node* index = assembler.SmiConstant(Smi::kZero);
assembler.StoreObjectFieldNoWriteBarrier(
iterator, JSStringIterator::kNextIndexOffset, index);
assembler.Return(iterator);
}
TF_BUILTIN(StringPrototypeIterator, CodeStubAssembler) {
Node* receiver = Parameter(0);
Node* context = Parameter(3);
namespace {
Node* string =
ToThisString(context, receiver, "String.prototype[Symbol.iterator]");
Node* native_context = LoadNativeContext(context);
Node* map =
LoadContextElement(native_context, Context::STRING_ITERATOR_MAP_INDEX);
Node* iterator = Allocate(JSStringIterator::kSize);
StoreMapNoWriteBarrier(iterator, map);
StoreObjectFieldRoot(iterator, JSValue::kPropertiesOffset,
Heap::kEmptyFixedArrayRootIndex);
StoreObjectFieldRoot(iterator, JSObject::kElementsOffset,
Heap::kEmptyFixedArrayRootIndex);
StoreObjectFieldNoWriteBarrier(iterator, JSStringIterator::kStringOffset,
string);
Node* index = SmiConstant(Smi::kZero);
StoreObjectFieldNoWriteBarrier(iterator, JSStringIterator::kNextIndexOffset,
index);
Return(iterator);
}
// Return the |word32| codepoint at {index}. Supports SeqStrings and
// ExternalStrings.
compiler::Node* LoadSurrogatePairInternal(CodeStubAssembler* assembler,
compiler::Node* string,
compiler::Node* length,
compiler::Node* index,
UnicodeEncoding encoding) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
Label handle_surrogate_pair(assembler), return_result(assembler);
Variable var_result(assembler, MachineRepresentation::kWord32);
Variable var_trail(assembler, MachineRepresentation::kWord32);
var_result.Bind(assembler->StringCharCodeAt(string, index));
var_trail.Bind(assembler->Int32Constant(0));
assembler->GotoIf(assembler->Word32NotEqual(
assembler->Word32And(var_result.value(),
assembler->Int32Constant(0xFC00)),
assembler->Int32Constant(0xD800)),
&return_result);
Node* next_index =
assembler->SmiAdd(index, assembler->SmiConstant(Smi::FromInt(1)));
assembler->GotoUnless(assembler->SmiLessThan(next_index, length),
&return_result);
var_trail.Bind(assembler->StringCharCodeAt(string, next_index));
assembler->Branch(assembler->Word32Equal(
assembler->Word32And(var_trail.value(),
assembler->Int32Constant(0xFC00)),
assembler->Int32Constant(0xDC00)),
&handle_surrogate_pair, &return_result);
assembler->Bind(&handle_surrogate_pair);
compiler::Node* StringBuiltinsAssembler::LoadSurrogatePairAt(
compiler::Node* string, compiler::Node* length, compiler::Node* index,
UnicodeEncoding encoding) {
Label handle_surrogate_pair(this), return_result(this);
Variable var_result(this, MachineRepresentation::kWord32);
Variable var_trail(this, MachineRepresentation::kWord32);
var_result.Bind(StringCharCodeAt(string, index));
var_trail.Bind(Int32Constant(0));
GotoIf(Word32NotEqual(Word32And(var_result.value(), Int32Constant(0xFC00)),
Int32Constant(0xD800)),
&return_result);
Node* next_index = SmiAdd(index, SmiConstant(Smi::FromInt(1)));
GotoUnless(SmiLessThan(next_index, length), &return_result);
var_trail.Bind(StringCharCodeAt(string, next_index));
Branch(Word32Equal(Word32And(var_trail.value(), Int32Constant(0xFC00)),
Int32Constant(0xDC00)),
&handle_surrogate_pair, &return_result);
Bind(&handle_surrogate_pair);
{
Node* lead = var_result.value();
Node* trail = var_trail.value();
// Check that this path is only taken if a surrogate pair is found
CSA_SLOW_ASSERT(assembler, assembler->Uint32GreaterThanOrEqual(
lead, assembler->Int32Constant(0xD800)));
CSA_SLOW_ASSERT(assembler, assembler->Uint32LessThan(
lead, assembler->Int32Constant(0xDC00)));
CSA_SLOW_ASSERT(assembler, assembler->Uint32GreaterThanOrEqual(
trail, assembler->Int32Constant(0xDC00)));
CSA_SLOW_ASSERT(assembler, assembler->Uint32LessThan(
trail, assembler->Int32Constant(0xE000)));
CSA_SLOW_ASSERT(this,
Uint32GreaterThanOrEqual(lead, Int32Constant(0xD800)));
CSA_SLOW_ASSERT(this, Uint32LessThan(lead, Int32Constant(0xDC00)));
CSA_SLOW_ASSERT(this,
Uint32GreaterThanOrEqual(trail, Int32Constant(0xDC00)));
CSA_SLOW_ASSERT(this, Uint32LessThan(trail, Int32Constant(0xE000)));
switch (encoding) {
case UnicodeEncoding::UTF16:
var_result.Bind(assembler->Word32Or(
var_result.Bind(Word32Or(
// Need to swap the order for big-endian platforms
#if V8_TARGET_BIG_ENDIAN
assembler->Word32Shl(lead, assembler->Int32Constant(16)), trail));
Word32Shl(lead, Int32Constant(16)), trail));
#else
assembler->Word32Shl(trail, assembler->Int32Constant(16)), lead));
Word32Shl(trail, Int32Constant(16)), lead));
#endif
break;
......@@ -1551,106 +1365,85 @@ compiler::Node* LoadSurrogatePairInternal(CodeStubAssembler* assembler,
// Convert UTF16 surrogate pair into |word32| code point, encoded as
// UTF32.
Node* surrogate_offset =
assembler->Int32Constant(0x10000 - (0xD800 << 10) - 0xDC00);
Int32Constant(0x10000 - (0xD800 << 10) - 0xDC00);
// (lead << 10) + trail + SURROGATE_OFFSET
var_result.Bind(assembler->Int32Add(
assembler->WordShl(lead, assembler->Int32Constant(10)),
assembler->Int32Add(trail, surrogate_offset)));
var_result.Bind(Int32Add(WordShl(lead, Int32Constant(10)),
Int32Add(trail, surrogate_offset)));
break;
}
}
assembler->Goto(&return_result);
Goto(&return_result);
}
assembler->Bind(&return_result);
Bind(&return_result);
return var_result.value();
}
compiler::Node* LoadSurrogatePairAt(CodeStubAssembler* assembler,
compiler::Node* string,
compiler::Node* length,
compiler::Node* index) {
return LoadSurrogatePairInternal(assembler, string, length, index,
UnicodeEncoding::UTF16);
}
} // namespace
TF_BUILTIN(StringIteratorPrototypeNext, StringBuiltinsAssembler) {
Variable var_value(this, MachineRepresentation::kTagged);
Variable var_done(this, MachineRepresentation::kTagged);
void Builtins::Generate_StringIteratorPrototypeNext(
compiler::CodeAssemblerState* state) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
CodeStubAssembler assembler(state);
var_value.Bind(UndefinedConstant());
var_done.Bind(BooleanConstant(true));
Variable var_value(&assembler, MachineRepresentation::kTagged);
Variable var_done(&assembler, MachineRepresentation::kTagged);
Label throw_bad_receiver(this), next_codepoint(this), return_result(this);
var_value.Bind(assembler.UndefinedConstant());
var_done.Bind(assembler.BooleanConstant(true));
Node* iterator = Parameter(0);
Node* context = Parameter(3);
Label throw_bad_receiver(&assembler), next_codepoint(&assembler),
return_result(&assembler);
GotoIf(TaggedIsSmi(iterator), &throw_bad_receiver);
GotoUnless(Word32Equal(LoadInstanceType(iterator),
Int32Constant(JS_STRING_ITERATOR_TYPE)),
&throw_bad_receiver);
Node* iterator = assembler.Parameter(0);
Node* context = assembler.Parameter(3);
assembler.GotoIf(assembler.TaggedIsSmi(iterator), &throw_bad_receiver);
assembler.GotoUnless(
assembler.Word32Equal(assembler.LoadInstanceType(iterator),
assembler.Int32Constant(JS_STRING_ITERATOR_TYPE)),
&throw_bad_receiver);
Node* string =
assembler.LoadObjectField(iterator, JSStringIterator::kStringOffset);
Node* string = LoadObjectField(iterator, JSStringIterator::kStringOffset);
Node* position =
assembler.LoadObjectField(iterator, JSStringIterator::kNextIndexOffset);
Node* length = assembler.LoadObjectField(string, String::kLengthOffset);
LoadObjectField(iterator, JSStringIterator::kNextIndexOffset);
Node* length = LoadObjectField(string, String::kLengthOffset);
assembler.Branch(assembler.SmiLessThan(position, length), &next_codepoint,
&return_result);
Branch(SmiLessThan(position, length), &next_codepoint, &return_result);
assembler.Bind(&next_codepoint);
Bind(&next_codepoint);
{
Node* ch = LoadSurrogatePairAt(&assembler, string, length, position);
Node* value = assembler.StringFromCodePoint(ch, UnicodeEncoding::UTF16);
UnicodeEncoding encoding = UnicodeEncoding::UTF16;
Node* ch = LoadSurrogatePairAt(string, length, position, encoding);
Node* value = StringFromCodePoint(ch, encoding);
var_value.Bind(value);
Node* length = assembler.LoadObjectField(value, String::kLengthOffset);
assembler.StoreObjectFieldNoWriteBarrier(
iterator, JSStringIterator::kNextIndexOffset,
assembler.SmiAdd(position, length));
var_done.Bind(assembler.BooleanConstant(false));
assembler.Goto(&return_result);
Node* length = LoadObjectField(value, String::kLengthOffset);
StoreObjectFieldNoWriteBarrier(iterator, JSStringIterator::kNextIndexOffset,
SmiAdd(position, length));
var_done.Bind(BooleanConstant(false));
Goto(&return_result);
}
assembler.Bind(&return_result);
Bind(&return_result);
{
Node* native_context = assembler.LoadNativeContext(context);
Node* map = assembler.LoadContextElement(
native_context, Context::ITERATOR_RESULT_MAP_INDEX);
Node* result = assembler.Allocate(JSIteratorResult::kSize);
assembler.StoreMapNoWriteBarrier(result, map);
assembler.StoreObjectFieldRoot(result, JSIteratorResult::kPropertiesOffset,
Heap::kEmptyFixedArrayRootIndex);
assembler.StoreObjectFieldRoot(result, JSIteratorResult::kElementsOffset,
Heap::kEmptyFixedArrayRootIndex);
assembler.StoreObjectFieldNoWriteBarrier(
result, JSIteratorResult::kValueOffset, var_value.value());
assembler.StoreObjectFieldNoWriteBarrier(
result, JSIteratorResult::kDoneOffset, var_done.value());
assembler.Return(result);
Node* native_context = LoadNativeContext(context);
Node* map =
LoadContextElement(native_context, Context::ITERATOR_RESULT_MAP_INDEX);
Node* result = Allocate(JSIteratorResult::kSize);
StoreMapNoWriteBarrier(result, map);
StoreObjectFieldRoot(result, JSIteratorResult::kPropertiesOffset,
Heap::kEmptyFixedArrayRootIndex);
StoreObjectFieldRoot(result, JSIteratorResult::kElementsOffset,
Heap::kEmptyFixedArrayRootIndex);
StoreObjectFieldNoWriteBarrier(result, JSIteratorResult::kValueOffset,
var_value.value());
StoreObjectFieldNoWriteBarrier(result, JSIteratorResult::kDoneOffset,
var_done.value());
Return(result);
}
assembler.Bind(&throw_bad_receiver);
Bind(&throw_bad_receiver);
{
// The {receiver} is not a valid JSGeneratorObject.
Node* result = assembler.CallRuntime(
Runtime::kThrowIncompatibleMethodReceiver, context,
assembler.HeapConstant(assembler.factory()->NewStringFromAsciiChecked(
"String Iterator.prototype.next", TENURED)),
iterator);
assembler.Return(result); // Never reached.
Node* result =
CallRuntime(Runtime::kThrowIncompatibleMethodReceiver, context,
HeapConstant(factory()->NewStringFromAsciiChecked(
"String Iterator.prototype.next", TENURED)),
iterator);
Return(result); // Never reached.
}
}
......
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