[builtins] Create StringToNumber helper.
Move StringToNumber builtin implementation to helper function in CodeStubAssembler. BUG= Review-Url: https://codereview.chromium.org/2293943002 Cr-Commit-Position: refs/heads/master@{#39249}
This commit is contained in:
parent
65128ab230
commit
5e6a1abf54
@ -110,35 +110,13 @@ void Builtins::Generate_NonPrimitiveToPrimitive_String(
|
||||
}
|
||||
|
||||
void Builtins::Generate_StringToNumber(CodeStubAssembler* assembler) {
|
||||
typedef CodeStubAssembler::Label Label;
|
||||
typedef compiler::Node Node;
|
||||
typedef TypeConversionDescriptor Descriptor;
|
||||
|
||||
Node* input = assembler->Parameter(Descriptor::kArgument);
|
||||
Node* context = assembler->Parameter(Descriptor::kContext);
|
||||
|
||||
Label runtime(assembler);
|
||||
|
||||
// Check if string has a cached array index.
|
||||
Node* hash = assembler->LoadNameHashField(input);
|
||||
Node* bit = assembler->Word32And(
|
||||
hash, assembler->Int32Constant(String::kContainsCachedArrayIndexMask));
|
||||
assembler->GotoIf(assembler->Word32NotEqual(bit, assembler->Int32Constant(0)),
|
||||
&runtime);
|
||||
|
||||
assembler->Return(assembler->SmiTag(
|
||||
assembler->BitFieldDecode<String::ArrayIndexValueBits>(hash)));
|
||||
|
||||
assembler->Bind(&runtime);
|
||||
{
|
||||
// Note: We cannot tail call to the runtime here, as js-to-wasm
|
||||
// trampolines also use this code currently, and they declare all
|
||||
// outgoing parameters as untagged, while we would push a tagged
|
||||
// object here.
|
||||
Node* result =
|
||||
assembler->CallRuntime(Runtime::kStringToNumber, context, input);
|
||||
assembler->Return(result);
|
||||
}
|
||||
assembler->Return(assembler->StringToNumber(context, input));
|
||||
}
|
||||
|
||||
// ES6 section 7.1.3 ToNumber ( argument )
|
||||
@ -183,9 +161,7 @@ void Builtins::Generate_NonNumberToNumber(CodeStubAssembler* assembler) {
|
||||
assembler->Bind(&if_inputisstring);
|
||||
{
|
||||
// The {input} is a String, use the fast stub to convert it to a Number.
|
||||
// TODO(bmeurer): Consider inlining the StringToNumber logic here.
|
||||
Callable callable = CodeFactory::StringToNumber(assembler->isolate());
|
||||
assembler->TailCallStub(callable, context, input);
|
||||
assembler->Return(assembler->StringToNumber(context, input));
|
||||
}
|
||||
|
||||
assembler->Bind(&if_inputisoddball);
|
||||
|
@ -2215,6 +2215,31 @@ Node* CodeStubAssembler::StringFromCharCode(Node* code) {
|
||||
return var_result.value();
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::StringToNumber(Node* context, Node* input) {
|
||||
Label runtime(this, Label::kDeferred);
|
||||
Label end(this);
|
||||
|
||||
Variable var_result(this, MachineRepresentation::kTagged);
|
||||
|
||||
// Check if string has a cached array index.
|
||||
Node* hash = LoadNameHashField(input);
|
||||
Node* bit =
|
||||
Word32And(hash, Int32Constant(String::kContainsCachedArrayIndexMask));
|
||||
GotoIf(Word32NotEqual(bit, Int32Constant(0)), &runtime);
|
||||
|
||||
var_result.Bind(SmiTag(BitFieldDecode<String::ArrayIndexValueBits>(hash)));
|
||||
Goto(&end);
|
||||
|
||||
Bind(&runtime);
|
||||
{
|
||||
var_result.Bind(CallRuntime(Runtime::kStringToNumber, context, input));
|
||||
Goto(&end);
|
||||
}
|
||||
|
||||
Bind(&end);
|
||||
return var_result.value();
|
||||
}
|
||||
|
||||
Node* CodeStubAssembler::BitFieldDecode(Node* word32, uint32_t shift,
|
||||
uint32_t mask) {
|
||||
return Word32Shr(Word32And(word32, Int32Constant(mask)),
|
||||
|
@ -355,6 +355,11 @@ class CodeStubAssembler : public compiler::CodeAssembler {
|
||||
// Return the single character string with only {code}.
|
||||
compiler::Node* StringFromCharCode(compiler::Node* code);
|
||||
|
||||
// Type conversion helpers.
|
||||
// Convert a String to a Number.
|
||||
compiler::Node* StringToNumber(compiler::Node* context,
|
||||
compiler::Node* input);
|
||||
|
||||
// Returns a node that is true if the given bit is set in |word32|.
|
||||
template <typename T>
|
||||
compiler::Node* BitFieldDecode(compiler::Node* word32) {
|
||||
|
Loading…
Reference in New Issue
Block a user