blob: 1d07c29df3d32ae46abeb415c24803807e556ce0 [file] [log] [blame]
// Copyright 2012 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.
#include "src/code-stubs.h"
#include <sstream>
#include "src/bootstrapper.h"
#include "src/code-factory.h"
#include "src/code-stub-assembler.h"
#include "src/factory.h"
#include "src/gdb-jit.h"
#include "src/ic/handler-compiler.h"
#include "src/ic/ic.h"
#include "src/macro-assembler.h"
#include "src/parsing/parser.h"
namespace v8 {
namespace internal {
RUNTIME_FUNCTION(UnexpectedStubMiss) {
FATAL("Unexpected deopt of a stub");
return Smi::FromInt(0);
}
CodeStubDescriptor::CodeStubDescriptor(CodeStub* stub)
: isolate_(stub->isolate()),
call_descriptor_(stub->GetCallInterfaceDescriptor()),
stack_parameter_count_(no_reg),
hint_stack_parameter_count_(-1),
function_mode_(NOT_JS_FUNCTION_STUB_MODE),
deoptimization_handler_(NULL),
miss_handler_(),
has_miss_handler_(false) {
stub->InitializeDescriptor(this);
}
CodeStubDescriptor::CodeStubDescriptor(Isolate* isolate, uint32_t stub_key)
: isolate_(isolate),
stack_parameter_count_(no_reg),
hint_stack_parameter_count_(-1),
function_mode_(NOT_JS_FUNCTION_STUB_MODE),
deoptimization_handler_(NULL),
miss_handler_(),
has_miss_handler_(false) {
CodeStub::InitializeDescriptor(isolate, stub_key, this);
}
void CodeStubDescriptor::Initialize(Address deoptimization_handler,
int hint_stack_parameter_count,
StubFunctionMode function_mode) {
deoptimization_handler_ = deoptimization_handler;
hint_stack_parameter_count_ = hint_stack_parameter_count;
function_mode_ = function_mode;
}
void CodeStubDescriptor::Initialize(Register stack_parameter_count,
Address deoptimization_handler,
int hint_stack_parameter_count,
StubFunctionMode function_mode) {
Initialize(deoptimization_handler, hint_stack_parameter_count, function_mode);
stack_parameter_count_ = stack_parameter_count;
}
bool CodeStub::FindCodeInCache(Code** code_out) {
UnseededNumberDictionary* stubs = isolate()->heap()->code_stubs();
int index = stubs->FindEntry(GetKey());
if (index != UnseededNumberDictionary::kNotFound) {
*code_out = Code::cast(stubs->ValueAt(index));
return true;
}
return false;
}
void CodeStub::RecordCodeGeneration(Handle<Code> code) {
std::ostringstream os;
os << *this;
PROFILE(isolate(),
CodeCreateEvent(CodeEventListener::STUB_TAG,
AbstractCode::cast(*code), os.str().c_str()));
Counters* counters = isolate()->counters();
counters->total_stubs_code_size()->Increment(code->instruction_size());
#ifdef DEBUG
code->VerifyEmbeddedObjects();
#endif
}
Code::Kind CodeStub::GetCodeKind() const {
return Code::STUB;
}
Code::Flags CodeStub::GetCodeFlags() const {
return Code::ComputeFlags(GetCodeKind(), GetExtraICState());
}
Handle<Code> CodeStub::GetCodeCopy(const Code::FindAndReplacePattern& pattern) {
Handle<Code> ic = GetCode();
ic = isolate()->factory()->CopyCode(ic);
ic->FindAndReplace(pattern);
RecordCodeGeneration(ic);
return ic;
}
Handle<Code> PlatformCodeStub::GenerateCode() {
Factory* factory = isolate()->factory();
// Generate the new code.
MacroAssembler masm(isolate(), NULL, 256, CodeObjectRequired::kYes);
{
// Update the static counter each time a new code stub is generated.
isolate()->counters()->code_stubs()->Increment();
// Generate the code for the stub.
masm.set_generating_stub(true);
// TODO(yangguo): remove this once we can serialize IC stubs.
masm.enable_serializer();
NoCurrentFrameScope scope(&masm);
Generate(&masm);
}
// Create the code object.
CodeDesc desc;
masm.GetCode(&desc);
// Copy the generated code into a heap object.
Code::Flags flags = Code::ComputeFlags(GetCodeKind(), GetExtraICState());
Handle<Code> new_object = factory->NewCode(
desc, flags, masm.CodeObject(), NeedsImmovableCode());
return new_object;
}
Handle<Code> CodeStub::GetCode() {
Heap* heap = isolate()->heap();
Code* code;
if (UseSpecialCache() ? FindCodeInSpecialCache(&code)
: FindCodeInCache(&code)) {
DCHECK(GetCodeKind() == code->kind());
return Handle<Code>(code);
}
{
HandleScope scope(isolate());
Handle<Code> new_object = GenerateCode();
new_object->set_stub_key(GetKey());
FinishCode(new_object);
RecordCodeGeneration(new_object);
#ifdef ENABLE_DISASSEMBLER
if (FLAG_print_code_stubs) {
CodeTracer::Scope trace_scope(isolate()->GetCodeTracer());
OFStream os(trace_scope.file());
std::ostringstream name;
name << *this;
new_object->Disassemble(name.str().c_str(), os);
os << "\n";
}
#endif
if (UseSpecialCache()) {
AddToSpecialCache(new_object);
} else {
// Update the dictionary and the root in Heap.
Handle<UnseededNumberDictionary> dict =
UnseededNumberDictionary::AtNumberPut(
Handle<UnseededNumberDictionary>(heap->code_stubs()),
GetKey(),
new_object);
heap->SetRootCodeStubs(*dict);
}
code = *new_object;
}
Activate(code);
DCHECK(!NeedsImmovableCode() ||
heap->lo_space()->Contains(code) ||
heap->code_space()->FirstPage()->Contains(code->address()));
return Handle<Code>(code, isolate());
}
const char* CodeStub::MajorName(CodeStub::Major major_key) {
switch (major_key) {
#define DEF_CASE(name) case name: return #name "Stub";
CODE_STUB_LIST(DEF_CASE)
#undef DEF_CASE
case NoCache:
return "<NoCache>Stub";
case NUMBER_OF_IDS:
UNREACHABLE();
return NULL;
}
return NULL;
}
void CodeStub::PrintBaseName(std::ostream& os) const { // NOLINT
os << MajorName(MajorKey());
}
void CodeStub::PrintName(std::ostream& os) const { // NOLINT
PrintBaseName(os);
PrintState(os);
}
void CodeStub::Dispatch(Isolate* isolate, uint32_t key, void** value_out,
DispatchedCall call) {
switch (MajorKeyFromKey(key)) {
#define DEF_CASE(NAME) \
case NAME: { \
NAME##Stub stub(key, isolate); \
CodeStub* pstub = &stub; \
call(pstub, value_out); \
break; \
}
CODE_STUB_LIST(DEF_CASE)
#undef DEF_CASE
case NUMBER_OF_IDS:
case NoCache:
UNREACHABLE();
break;
}
}
static void InitializeDescriptorDispatchedCall(CodeStub* stub,
void** value_out) {
CodeStubDescriptor* descriptor_out =
reinterpret_cast<CodeStubDescriptor*>(value_out);
stub->InitializeDescriptor(descriptor_out);
descriptor_out->set_call_descriptor(stub->GetCallInterfaceDescriptor());
}
void CodeStub::InitializeDescriptor(Isolate* isolate, uint32_t key,
CodeStubDescriptor* desc) {
void** value_out = reinterpret_cast<void**>(desc);
Dispatch(isolate, key, value_out, &InitializeDescriptorDispatchedCall);
}
void CodeStub::GetCodeDispatchCall(CodeStub* stub, void** value_out) {
Handle<Code>* code_out = reinterpret_cast<Handle<Code>*>(value_out);
// Code stubs with special cache cannot be recreated from stub key.
*code_out = stub->UseSpecialCache() ? Handle<Code>() : stub->GetCode();
}
MaybeHandle<Code> CodeStub::GetCode(Isolate* isolate, uint32_t key) {
HandleScope scope(isolate);
Handle<Code> code;
void** value_out = reinterpret_cast<void**>(&code);
Dispatch(isolate, key, value_out, &GetCodeDispatchCall);
return scope.CloseAndEscape(code);
}
// static
void BinaryOpICStub::GenerateAheadOfTime(Isolate* isolate) {
if (FLAG_minimal) return;
// Generate the uninitialized versions of the stub.
for (int op = Token::BIT_OR; op <= Token::MOD; ++op) {
BinaryOpICStub stub(isolate, static_cast<Token::Value>(op));
stub.GetCode();
}
// Generate special versions of the stub.
BinaryOpICState::GenerateAheadOfTime(isolate, &GenerateAheadOfTime);
}
void BinaryOpICStub::PrintState(std::ostream& os) const { // NOLINT
os << state();
}
// static
void BinaryOpICStub::GenerateAheadOfTime(Isolate* isolate,
const BinaryOpICState& state) {
if (FLAG_minimal) return;
BinaryOpICStub stub(isolate, state);
stub.GetCode();
}
// static
void BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(Isolate* isolate) {
// Generate special versions of the stub.
BinaryOpICState::GenerateAheadOfTime(isolate, &GenerateAheadOfTime);
}
void BinaryOpICWithAllocationSiteStub::PrintState(
std::ostream& os) const { // NOLINT
os << state();
}
// static
void BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(
Isolate* isolate, const BinaryOpICState& state) {
if (state.CouldCreateAllocationMementos()) {
BinaryOpICWithAllocationSiteStub stub(isolate, state);
stub.GetCode();
}
}
std::ostream& operator<<(std::ostream& os, const StringAddFlags& flags) {
switch (flags) {
case STRING_ADD_CHECK_NONE:
return os << "CheckNone";
case STRING_ADD_CHECK_LEFT:
return os << "CheckLeft";
case STRING_ADD_CHECK_RIGHT:
return os << "CheckRight";
case STRING_ADD_CHECK_BOTH:
return os << "CheckBoth";
case STRING_ADD_CONVERT_LEFT:
return os << "ConvertLeft";
case STRING_ADD_CONVERT_RIGHT:
return os << "ConvertRight";
case STRING_ADD_CONVERT:
break;
}
UNREACHABLE();
return os;
}
void StringAddStub::PrintBaseName(std::ostream& os) const { // NOLINT
os << "StringAddStub_" << flags() << "_" << pretenure_flag();
}
InlineCacheState CompareICStub::GetICState() const {
CompareICState::State state = Max(left(), right());
switch (state) {
case CompareICState::UNINITIALIZED:
return ::v8::internal::UNINITIALIZED;
case CompareICState::BOOLEAN:
case CompareICState::SMI:
case CompareICState::NUMBER:
case CompareICState::INTERNALIZED_STRING:
case CompareICState::STRING:
case CompareICState::UNIQUE_NAME:
case CompareICState::RECEIVER:
case CompareICState::KNOWN_RECEIVER:
return MONOMORPHIC;
case CompareICState::GENERIC:
return ::v8::internal::GENERIC;
}
UNREACHABLE();
return ::v8::internal::UNINITIALIZED;
}
Condition CompareICStub::GetCondition() const {
return CompareIC::ComputeCondition(op());
}
void CompareICStub::Generate(MacroAssembler* masm) {
switch (state()) {
case CompareICState::UNINITIALIZED:
GenerateMiss(masm);
break;
case CompareICState::BOOLEAN:
GenerateBooleans(masm);
break;
case CompareICState::SMI:
GenerateSmis(masm);
break;
case CompareICState::NUMBER:
GenerateNumbers(masm);
break;
case CompareICState::STRING:
GenerateStrings(masm);
break;
case CompareICState::INTERNALIZED_STRING:
GenerateInternalizedStrings(masm);
break;
case CompareICState::UNIQUE_NAME:
GenerateUniqueNames(masm);
break;
case CompareICState::RECEIVER:
GenerateReceivers(masm);
break;
case CompareICState::KNOWN_RECEIVER:
DCHECK(*known_map_ != NULL);
GenerateKnownReceivers(masm);
break;
case CompareICState::GENERIC:
GenerateGeneric(masm);
break;
}
}
Handle<Code> TurboFanCodeStub::GenerateCode() {
const char* name = CodeStub::MajorName(MajorKey());
Zone zone(isolate()->allocator());
CallInterfaceDescriptor descriptor(GetCallInterfaceDescriptor());
CodeStubAssembler assembler(isolate(), &zone, descriptor, GetCodeFlags(),
name);
GenerateAssembly(&assembler);
return assembler.GenerateCode();
}
void LoadICTrampolineTFStub::GenerateAssembly(
CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* receiver = assembler->Parameter(Descriptor::kReceiver);
Node* name = assembler->Parameter(Descriptor::kName);
Node* slot = assembler->Parameter(Descriptor::kSlot);
Node* context = assembler->Parameter(Descriptor::kContext);
Node* vector = assembler->LoadTypeFeedbackVectorForStub();
CodeStubAssembler::LoadICParameters p(context, receiver, name, slot, vector);
assembler->LoadIC(&p);
}
void LoadICTFStub::GenerateAssembly(CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* receiver = assembler->Parameter(Descriptor::kReceiver);
Node* name = assembler->Parameter(Descriptor::kName);
Node* slot = assembler->Parameter(Descriptor::kSlot);
Node* vector = assembler->Parameter(Descriptor::kVector);
Node* context = assembler->Parameter(Descriptor::kContext);
CodeStubAssembler::LoadICParameters p(context, receiver, name, slot, vector);
assembler->LoadIC(&p);
}
void LoadGlobalICTrampolineStub::GenerateAssembly(
CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* slot = assembler->Parameter(Descriptor::kSlot);
Node* context = assembler->Parameter(Descriptor::kContext);
Node* vector = assembler->LoadTypeFeedbackVectorForStub();
CodeStubAssembler::LoadICParameters p(context, nullptr, nullptr, slot,
vector);
assembler->LoadGlobalIC(&p);
}
void LoadGlobalICStub::GenerateAssembly(CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* slot = assembler->Parameter(Descriptor::kSlot);
Node* vector = assembler->Parameter(Descriptor::kVector);
Node* context = assembler->Parameter(Descriptor::kContext);
CodeStubAssembler::LoadICParameters p(context, nullptr, nullptr, slot,
vector);
assembler->LoadGlobalIC(&p);
}
void KeyedLoadICTrampolineTFStub::GenerateAssembly(
CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* receiver = assembler->Parameter(Descriptor::kReceiver);
Node* name = assembler->Parameter(Descriptor::kName);
Node* slot = assembler->Parameter(Descriptor::kSlot);
Node* context = assembler->Parameter(Descriptor::kContext);
Node* vector = assembler->LoadTypeFeedbackVectorForStub();
CodeStubAssembler::LoadICParameters p(context, receiver, name, slot, vector);
assembler->KeyedLoadIC(&p);
}
void KeyedLoadICTFStub::GenerateAssembly(CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* receiver = assembler->Parameter(Descriptor::kReceiver);
Node* name = assembler->Parameter(Descriptor::kName);
Node* slot = assembler->Parameter(Descriptor::kSlot);
Node* vector = assembler->Parameter(Descriptor::kVector);
Node* context = assembler->Parameter(Descriptor::kContext);
CodeStubAssembler::LoadICParameters p(context, receiver, name, slot, vector);
assembler->KeyedLoadIC(&p);
}
void AllocateHeapNumberStub::GenerateAssembly(
CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
Node* result = assembler->AllocateHeapNumber();
assembler->Return(result);
}
#define SIMD128_GEN_ASM(TYPE, Type, type, lane_count, lane_type) \
void Allocate##Type##Stub::GenerateAssembly(CodeStubAssembler* assembler) \
const { \
compiler::Node* result = \
assembler->Allocate(Simd128Value::kSize, CodeStubAssembler::kNone); \
compiler::Node* map = assembler->LoadMap(result); \
assembler->StoreNoWriteBarrier( \
MachineRepresentation::kTagged, map, \
assembler->HeapConstant(isolate()->factory()->type##_map())); \
assembler->Return(result); \
}
SIMD128_TYPES(SIMD128_GEN_ASM)
#undef SIMD128_GEN_ASM
void StringLengthStub::GenerateAssembly(CodeStubAssembler* assembler) const {
compiler::Node* value = assembler->Parameter(0);
compiler::Node* string = assembler->LoadJSValueValue(value);
compiler::Node* result = assembler->LoadStringLength(string);
assembler->Return(result);
}
// static
compiler::Node* AddStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left, compiler::Node* right,
compiler::Node* context) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
// Shared entry for floating point addition.
Label do_fadd(assembler);
Variable var_fadd_lhs(assembler, MachineRepresentation::kFloat64),
var_fadd_rhs(assembler, MachineRepresentation::kFloat64);
// We might need to loop several times due to ToPrimitive, ToString and/or
// ToNumber conversions.
Variable var_lhs(assembler, MachineRepresentation::kTagged),
var_rhs(assembler, MachineRepresentation::kTagged),
var_result(assembler, MachineRepresentation::kTagged);
Variable* loop_vars[2] = {&var_lhs, &var_rhs};
Label loop(assembler, 2, loop_vars), end(assembler),
string_add_convert_left(assembler, Label::kDeferred),
string_add_convert_right(assembler, Label::kDeferred);
var_lhs.Bind(left);
var_rhs.Bind(right);
assembler->Goto(&loop);
assembler->Bind(&loop);
{
// Load the current {lhs} and {rhs} values.
Node* lhs = var_lhs.value();
Node* rhs = var_rhs.value();
// Check if the {lhs} is a Smi or a HeapObject.
Label if_lhsissmi(assembler), if_lhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(lhs), &if_lhsissmi, &if_lhsisnotsmi);
assembler->Bind(&if_lhsissmi);
{
// Check if the {rhs} is also a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi,
&if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Try fast Smi addition first.
Node* pair = assembler->SmiAddWithOverflow(lhs, rhs);
Node* overflow = assembler->Projection(1, pair);
// Check if the Smi additon overflowed.
Label if_overflow(assembler), if_notoverflow(assembler);
assembler->Branch(overflow, &if_overflow, &if_notoverflow);
assembler->Bind(&if_overflow);
{
var_fadd_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fadd_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fadd);
}
assembler->Bind(&if_notoverflow);
var_result.Bind(assembler->Projection(0, pair));
assembler->Goto(&end);
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if the {rhs} is a HeapNumber.
Label if_rhsisnumber(assembler),
if_rhsisnotnumber(assembler, Label::kDeferred);
Node* number_map = assembler->HeapNumberMapConstant();
assembler->Branch(assembler->WordEqual(rhs_map, number_map),
&if_rhsisnumber, &if_rhsisnotnumber);
assembler->Bind(&if_rhsisnumber);
{
var_fadd_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fadd_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fadd);
}
assembler->Bind(&if_rhsisnotnumber);
{
// Load the instance type of {rhs}.
Node* rhs_instance_type = assembler->LoadMapInstanceType(rhs_map);
// Check if the {rhs} is a String.
Label if_rhsisstring(assembler, Label::kDeferred),
if_rhsisnotstring(assembler, Label::kDeferred);
assembler->Branch(assembler->Int32LessThan(
rhs_instance_type,
assembler->Int32Constant(FIRST_NONSTRING_TYPE)),
&if_rhsisstring, &if_rhsisnotstring);
assembler->Bind(&if_rhsisstring);
{
var_lhs.Bind(lhs);
var_rhs.Bind(rhs);
assembler->Goto(&string_add_convert_left);
}
assembler->Bind(&if_rhsisnotstring);
{
// Check if {rhs} is a JSReceiver.
Label if_rhsisreceiver(assembler, Label::kDeferred),
if_rhsisnotreceiver(assembler, Label::kDeferred);
assembler->Branch(
assembler->Int32LessThanOrEqual(
assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE),
rhs_instance_type),
&if_rhsisreceiver, &if_rhsisnotreceiver);
assembler->Bind(&if_rhsisreceiver);
{
// Convert {rhs} to a primitive first passing no hint.
Callable callable =
CodeFactory::NonPrimitiveToPrimitive(assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
assembler->Bind(&if_rhsisnotreceiver);
{
// Convert {rhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
}
}
}
}
assembler->Bind(&if_lhsisnotsmi);
{
// Load the map and instance type of {lhs}.
Node* lhs_instance_type = assembler->LoadInstanceType(lhs);
// Check if {lhs} is a String.
Label if_lhsisstring(assembler), if_lhsisnotstring(assembler);
assembler->Branch(assembler->Int32LessThan(
lhs_instance_type,
assembler->Int32Constant(FIRST_NONSTRING_TYPE)),
&if_lhsisstring, &if_lhsisnotstring);
assembler->Bind(&if_lhsisstring);
{
var_lhs.Bind(lhs);
var_rhs.Bind(rhs);
assembler->Goto(&string_add_convert_right);
}
assembler->Bind(&if_lhsisnotstring);
{
// Check if {rhs} is a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi,
&if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Check if {lhs} is a Number.
Label if_lhsisnumber(assembler),
if_lhsisnotnumber(assembler, Label::kDeferred);
assembler->Branch(assembler->Word32Equal(
lhs_instance_type,
assembler->Int32Constant(HEAP_NUMBER_TYPE)),
&if_lhsisnumber, &if_lhsisnotnumber);
assembler->Bind(&if_lhsisnumber);
{
// The {lhs} is a HeapNumber, the {rhs} is a Smi, just add them.
var_fadd_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fadd_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fadd);
}
assembler->Bind(&if_lhsisnotnumber);
{
// The {lhs} is neither a Number nor a String, and the {rhs} is a
// Smi.
Label if_lhsisreceiver(assembler, Label::kDeferred),
if_lhsisnotreceiver(assembler, Label::kDeferred);
assembler->Branch(
assembler->Int32LessThanOrEqual(
assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE),
lhs_instance_type),
&if_lhsisreceiver, &if_lhsisnotreceiver);
assembler->Bind(&if_lhsisreceiver);
{
// Convert {lhs} to a primitive first passing no hint.
Callable callable =
CodeFactory::NonPrimitiveToPrimitive(assembler->isolate());
var_lhs.Bind(assembler->CallStub(callable, context, lhs));
assembler->Goto(&loop);
}
assembler->Bind(&if_lhsisnotreceiver);
{
// Convert {lhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_lhs.Bind(assembler->CallStub(callable, context, lhs));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the instance type of {rhs}.
Node* rhs_instance_type = assembler->LoadInstanceType(rhs);
// Check if {rhs} is a String.
Label if_rhsisstring(assembler), if_rhsisnotstring(assembler);
assembler->Branch(assembler->Int32LessThan(
rhs_instance_type,
assembler->Int32Constant(FIRST_NONSTRING_TYPE)),
&if_rhsisstring, &if_rhsisnotstring);
assembler->Bind(&if_rhsisstring);
{
var_lhs.Bind(lhs);
var_rhs.Bind(rhs);
assembler->Goto(&string_add_convert_left);
}
assembler->Bind(&if_rhsisnotstring);
{
// Check if {lhs} is a HeapNumber.
Label if_lhsisnumber(assembler), if_lhsisnotnumber(assembler);
assembler->Branch(assembler->Word32Equal(
lhs_instance_type,
assembler->Int32Constant(HEAP_NUMBER_TYPE)),
&if_lhsisnumber, &if_lhsisnotnumber);
assembler->Bind(&if_lhsisnumber);
{
// Check if {rhs} is also a HeapNumber.
Label if_rhsisnumber(assembler),
if_rhsisnotnumber(assembler, Label::kDeferred);
assembler->Branch(assembler->Word32Equal(
rhs_instance_type,
assembler->Int32Constant(HEAP_NUMBER_TYPE)),
&if_rhsisnumber, &if_rhsisnotnumber);
assembler->Bind(&if_rhsisnumber);
{
// Perform a floating point addition.
var_fadd_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fadd_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fadd);
}
assembler->Bind(&if_rhsisnotnumber);
{
// Check if {rhs} is a JSReceiver.
Label if_rhsisreceiver(assembler, Label::kDeferred),
if_rhsisnotreceiver(assembler, Label::kDeferred);
assembler->Branch(
assembler->Int32LessThanOrEqual(
assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE),
rhs_instance_type),
&if_rhsisreceiver, &if_rhsisnotreceiver);
assembler->Bind(&if_rhsisreceiver);
{
// Convert {rhs} to a primitive first passing no hint.
Callable callable = CodeFactory::NonPrimitiveToPrimitive(
assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
assembler->Bind(&if_rhsisnotreceiver);
{
// Convert {rhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&if_lhsisnotnumber);
{
// Check if {lhs} is a JSReceiver.
Label if_lhsisreceiver(assembler, Label::kDeferred),
if_lhsisnotreceiver(assembler);
assembler->Branch(
assembler->Int32LessThanOrEqual(
assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE),
lhs_instance_type),
&if_lhsisreceiver, &if_lhsisnotreceiver);
assembler->Bind(&if_lhsisreceiver);
{
// Convert {lhs} to a primitive first passing no hint.
Callable callable =
CodeFactory::NonPrimitiveToPrimitive(assembler->isolate());
var_lhs.Bind(assembler->CallStub(callable, context, lhs));
assembler->Goto(&loop);
}
assembler->Bind(&if_lhsisnotreceiver);
{
// Check if {rhs} is a JSReceiver.
Label if_rhsisreceiver(assembler, Label::kDeferred),
if_rhsisnotreceiver(assembler, Label::kDeferred);
assembler->Branch(
assembler->Int32LessThanOrEqual(
assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE),
rhs_instance_type),
&if_rhsisreceiver, &if_rhsisnotreceiver);
assembler->Bind(&if_rhsisreceiver);
{
// Convert {rhs} to a primitive first passing no hint.
Callable callable = CodeFactory::NonPrimitiveToPrimitive(
assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
assembler->Bind(&if_rhsisnotreceiver);
{
// Convert {lhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_lhs.Bind(assembler->CallStub(callable, context, lhs));
assembler->Goto(&loop);
}
}
}
}
}
}
}
}
assembler->Bind(&string_add_convert_left);
{
// Convert {lhs}, which is a Smi, to a String and concatenate the
// resulting string with the String {rhs}.
Callable callable = CodeFactory::StringAdd(
assembler->isolate(), STRING_ADD_CONVERT_LEFT, NOT_TENURED);
var_result.Bind(assembler->CallStub(callable, context, var_lhs.value(),
var_rhs.value()));
assembler->Goto(&end);
}
assembler->Bind(&string_add_convert_right);
{
// Convert {lhs}, which is a Smi, to a String and concatenate the
// resulting string with the String {rhs}.
Callable callable = CodeFactory::StringAdd(
assembler->isolate(), STRING_ADD_CONVERT_RIGHT, NOT_TENURED);
var_result.Bind(assembler->CallStub(callable, context, var_lhs.value(),
var_rhs.value()));
assembler->Goto(&end);
}
assembler->Bind(&do_fadd);
{
Node* lhs_value = var_fadd_lhs.value();
Node* rhs_value = var_fadd_rhs.value();
Node* value = assembler->Float64Add(lhs_value, rhs_value);
Node* result = assembler->ChangeFloat64ToTagged(value);
var_result.Bind(result);
assembler->Goto(&end);
}
assembler->Bind(&end);
return var_result.value();
}
// static
compiler::Node* AddWithFeedbackStub::Generate(
CodeStubAssembler* assembler, compiler::Node* lhs, compiler::Node* rhs,
compiler::Node* context, compiler::Node* type_feedback_vector,
compiler::Node* slot_id) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
// Shared entry for floating point addition.
Label do_fadd(assembler), end(assembler),
call_add_stub(assembler, Label::kDeferred);
Variable var_fadd_lhs(assembler, MachineRepresentation::kFloat64),
var_fadd_rhs(assembler, MachineRepresentation::kFloat64),
var_type_feedback(assembler, MachineRepresentation::kWord32),
var_result(assembler, MachineRepresentation::kTagged);
// Check if the {lhs} is a Smi or a HeapObject.
Label if_lhsissmi(assembler), if_lhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(lhs), &if_lhsissmi, &if_lhsisnotsmi);
assembler->Bind(&if_lhsissmi);
{
// Check if the {rhs} is also a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi, &if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Try fast Smi addition first.
Node* pair = assembler->SmiAddWithOverflow(lhs, rhs);
Node* overflow = assembler->Projection(1, pair);
// Check if the Smi additon overflowed.
Label if_overflow(assembler), if_notoverflow(assembler);
assembler->Branch(overflow, &if_overflow, &if_notoverflow);
assembler->Bind(&if_overflow);
{
var_fadd_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fadd_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fadd);
}
assembler->Bind(&if_notoverflow);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kSignedSmall));
var_result.Bind(assembler->Projection(0, pair));
assembler->Goto(&end);
}
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if the {rhs} is a HeapNumber.
assembler->GotoUnless(
assembler->WordEqual(rhs_map, assembler->HeapNumberMapConstant()),
&call_add_stub);
var_fadd_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fadd_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fadd);
}
}
assembler->Bind(&if_lhsisnotsmi);
{
// Load the map of {lhs}.
Node* lhs_map = assembler->LoadMap(lhs);
// Check if {lhs} is a HeapNumber.
Label if_lhsisnumber(assembler), if_lhsisnotnumber(assembler);
assembler->GotoUnless(
assembler->WordEqual(lhs_map, assembler->HeapNumberMapConstant()),
&call_add_stub);
// Check if the {rhs} is Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi, &if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
var_fadd_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fadd_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fadd);
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if the {rhs} is a HeapNumber.
Node* number_map = assembler->HeapNumberMapConstant();
assembler->GotoUnless(assembler->WordEqual(rhs_map, number_map),
&call_add_stub);
var_fadd_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fadd_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fadd);
}
}
assembler->Bind(&do_fadd);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kNumber));
Node* value =
assembler->Float64Add(var_fadd_lhs.value(), var_fadd_rhs.value());
Node* result = assembler->ChangeFloat64ToTagged(value);
var_result.Bind(result);
assembler->Goto(&end);
}
assembler->Bind(&call_add_stub);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kAny));
Callable callable = CodeFactory::Add(assembler->isolate());
var_result.Bind(assembler->CallStub(callable, context, lhs, rhs));
assembler->Goto(&end);
}
assembler->Bind(&end);
assembler->UpdateFeedback(var_type_feedback.value(), type_feedback_vector,
slot_id);
return var_result.value();
}
// static
compiler::Node* SubtractStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
// Shared entry for floating point subtraction.
Label do_fsub(assembler), end(assembler);
Variable var_fsub_lhs(assembler, MachineRepresentation::kFloat64),
var_fsub_rhs(assembler, MachineRepresentation::kFloat64);
// We might need to loop several times due to ToPrimitive and/or ToNumber
// conversions.
Variable var_lhs(assembler, MachineRepresentation::kTagged),
var_rhs(assembler, MachineRepresentation::kTagged),
var_result(assembler, MachineRepresentation::kTagged);
Variable* loop_vars[2] = {&var_lhs, &var_rhs};
Label loop(assembler, 2, loop_vars);
var_lhs.Bind(left);
var_rhs.Bind(right);
assembler->Goto(&loop);
assembler->Bind(&loop);
{
// Load the current {lhs} and {rhs} values.
Node* lhs = var_lhs.value();
Node* rhs = var_rhs.value();
// Check if the {lhs} is a Smi or a HeapObject.
Label if_lhsissmi(assembler), if_lhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(lhs), &if_lhsissmi, &if_lhsisnotsmi);
assembler->Bind(&if_lhsissmi);
{
// Check if the {rhs} is also a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi,
&if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Try a fast Smi subtraction first.
Node* pair = assembler->SmiSubWithOverflow(lhs, rhs);
Node* overflow = assembler->Projection(1, pair);
// Check if the Smi subtraction overflowed.
Label if_overflow(assembler), if_notoverflow(assembler);
assembler->Branch(overflow, &if_overflow, &if_notoverflow);
assembler->Bind(&if_overflow);
{
// The result doesn't fit into Smi range.
var_fsub_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fsub_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fsub);
}
assembler->Bind(&if_notoverflow);
var_result.Bind(assembler->Projection(0, pair));
assembler->Goto(&end);
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of the {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if {rhs} is a HeapNumber.
Label if_rhsisnumber(assembler),
if_rhsisnotnumber(assembler, Label::kDeferred);
Node* number_map = assembler->HeapNumberMapConstant();
assembler->Branch(assembler->WordEqual(rhs_map, number_map),
&if_rhsisnumber, &if_rhsisnotnumber);
assembler->Bind(&if_rhsisnumber);
{
// Perform a floating point subtraction.
var_fsub_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fsub_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fsub);
}
assembler->Bind(&if_rhsisnotnumber);
{
// Convert the {rhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&if_lhsisnotsmi);
{
// Load the map of the {lhs}.
Node* lhs_map = assembler->LoadMap(lhs);
// Check if the {lhs} is a HeapNumber.
Label if_lhsisnumber(assembler),
if_lhsisnotnumber(assembler, Label::kDeferred);
Node* number_map = assembler->HeapNumberMapConstant();
assembler->Branch(assembler->WordEqual(lhs_map, number_map),
&if_lhsisnumber, &if_lhsisnotnumber);
assembler->Bind(&if_lhsisnumber);
{
// Check if the {rhs} is a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi,
&if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Perform a floating point subtraction.
var_fsub_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fsub_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fsub);
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of the {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if the {rhs} is a HeapNumber.
Label if_rhsisnumber(assembler),
if_rhsisnotnumber(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(rhs_map, number_map),
&if_rhsisnumber, &if_rhsisnotnumber);
assembler->Bind(&if_rhsisnumber);
{
// Perform a floating point subtraction.
var_fsub_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fsub_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fsub);
}
assembler->Bind(&if_rhsisnotnumber);
{
// Convert the {rhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_rhs.Bind(assembler->CallStub(callable, context, rhs));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&if_lhsisnotnumber);
{
// Convert the {lhs} to a Number first.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_lhs.Bind(assembler->CallStub(callable, context, lhs));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&do_fsub);
{
Node* lhs_value = var_fsub_lhs.value();
Node* rhs_value = var_fsub_rhs.value();
Node* value = assembler->Float64Sub(lhs_value, rhs_value);
var_result.Bind(assembler->ChangeFloat64ToTagged(value));
assembler->Goto(&end);
}
assembler->Bind(&end);
return var_result.value();
}
// static
compiler::Node* SubtractWithFeedbackStub::Generate(
CodeStubAssembler* assembler, compiler::Node* lhs, compiler::Node* rhs,
compiler::Node* context, compiler::Node* type_feedback_vector,
compiler::Node* slot_id) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
// Shared entry for floating point subtraction.
Label do_fsub(assembler), end(assembler),
call_subtract_stub(assembler, Label::kDeferred);
Variable var_fsub_lhs(assembler, MachineRepresentation::kFloat64),
var_fsub_rhs(assembler, MachineRepresentation::kFloat64),
var_type_feedback(assembler, MachineRepresentation::kWord32),
var_result(assembler, MachineRepresentation::kTagged);
// Check if the {lhs} is a Smi or a HeapObject.
Label if_lhsissmi(assembler), if_lhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(lhs), &if_lhsissmi, &if_lhsisnotsmi);
assembler->Bind(&if_lhsissmi);
{
// Check if the {rhs} is also a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi, &if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Try a fast Smi subtraction first.
Node* pair = assembler->SmiSubWithOverflow(lhs, rhs);
Node* overflow = assembler->Projection(1, pair);
// Check if the Smi subtraction overflowed.
Label if_overflow(assembler), if_notoverflow(assembler);
assembler->Branch(overflow, &if_overflow, &if_notoverflow);
assembler->Bind(&if_overflow);
{
// lhs, rhs - smi and result - number. combined - number.
// The result doesn't fit into Smi range.
var_fsub_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fsub_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fsub);
}
assembler->Bind(&if_notoverflow);
// lhs, rhs, result smi. combined - smi.
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kSignedSmall));
var_result.Bind(assembler->Projection(0, pair));
assembler->Goto(&end);
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of the {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if {rhs} is a HeapNumber.
assembler->GotoUnless(
assembler->WordEqual(rhs_map, assembler->HeapNumberMapConstant()),
&call_subtract_stub);
// Perform a floating point subtraction.
var_fsub_lhs.Bind(assembler->SmiToFloat64(lhs));
var_fsub_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fsub);
}
}
assembler->Bind(&if_lhsisnotsmi);
{
// Load the map of the {lhs}.
Node* lhs_map = assembler->LoadMap(lhs);
// Check if the {lhs} is a HeapNumber.
assembler->GotoUnless(
assembler->WordEqual(lhs_map, assembler->HeapNumberMapConstant()),
&call_subtract_stub);
// Check if the {rhs} is a Smi.
Label if_rhsissmi(assembler), if_rhsisnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &if_rhsissmi, &if_rhsisnotsmi);
assembler->Bind(&if_rhsissmi);
{
// Perform a floating point subtraction.
var_fsub_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fsub_rhs.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fsub);
}
assembler->Bind(&if_rhsisnotsmi);
{
// Load the map of the {rhs}.
Node* rhs_map = assembler->LoadMap(rhs);
// Check if the {rhs} is a HeapNumber.
assembler->GotoUnless(
assembler->WordEqual(rhs_map, assembler->HeapNumberMapConstant()),
&call_subtract_stub);
// Perform a floating point subtraction.
var_fsub_lhs.Bind(assembler->LoadHeapNumberValue(lhs));
var_fsub_rhs.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fsub);
}
}
assembler->Bind(&do_fsub);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kNumber));
Node* lhs_value = var_fsub_lhs.value();
Node* rhs_value = var_fsub_rhs.value();
Node* value = assembler->Float64Sub(lhs_value, rhs_value);
var_result.Bind(assembler->ChangeFloat64ToTagged(value));
assembler->Goto(&end);
}
assembler->Bind(&call_subtract_stub);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kAny));
Callable callable = CodeFactory::Subtract(assembler->isolate());
var_result.Bind(assembler->CallStub(callable, context, lhs, rhs));
assembler->Goto(&end);
}
assembler->Bind(&end);
assembler->UpdateFeedback(var_type_feedback.value(), type_feedback_vector,
slot_id);
return var_result.value();
}
// static
compiler::Node* MultiplyStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
// Shared entry point for floating point multiplication.
Label do_fmul(assembler), return_result(assembler);
Variable var_lhs_float64(assembler, MachineRepresentation::kFloat64),
var_rhs_float64(assembler, MachineRepresentation::kFloat64);
Node* number_map = assembler->HeapNumberMapConstant();
// We might need to loop one or two times due to ToNumber conversions.
Variable var_lhs(assembler, MachineRepresentation::kTagged),
var_rhs(assembler, MachineRepresentation::kTagged),
var_result(assembler, MachineRepresentation::kTagged);
Variable* loop_variables[] = {&var_lhs, &var_rhs};
Label loop(assembler, 2, loop_variables);
var_lhs.Bind(left);
var_rhs.Bind(right);
assembler->Goto(&loop);
assembler->Bind(&loop);
{
Node* lhs = var_lhs.value();
Node* rhs = var_rhs.value();
Label lhs_is_smi(assembler), lhs_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(lhs), &lhs_is_smi, &lhs_is_not_smi);
assembler->Bind(&lhs_is_smi);
{
Label rhs_is_smi(assembler), rhs_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &rhs_is_smi,
&rhs_is_not_smi);
assembler->Bind(&rhs_is_smi);
{
// Both {lhs} and {rhs} are Smis. The result is not necessarily a smi,
// in case of overflow.
var_result.Bind(assembler->SmiMul(lhs, rhs));
assembler->Goto(&return_result);
}
assembler->Bind(&rhs_is_not_smi);
{
Node* rhs_map = assembler->LoadMap(rhs);
// Check if {rhs} is a HeapNumber.
Label rhs_is_number(assembler),
rhs_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(rhs_map, number_map),
&rhs_is_number, &rhs_is_not_number);
assembler->Bind(&rhs_is_number);
{
// Convert {lhs} to a double and multiply it with the value of {rhs}.
var_lhs_float64.Bind(assembler->SmiToFloat64(lhs));
var_rhs_float64.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fmul);
}
assembler->Bind(&rhs_is_not_number);
{
// Multiplication is commutative, swap {lhs} with {rhs} and loop.
var_lhs.Bind(rhs);
var_rhs.Bind(lhs);
assembler->Goto(&loop);
}
}
}
assembler->Bind(&lhs_is_not_smi);
{
Node* lhs_map = assembler->LoadMap(lhs);
// Check if {lhs} is a HeapNumber.
Label lhs_is_number(assembler),
lhs_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(lhs_map, number_map),
&lhs_is_number, &lhs_is_not_number);
assembler->Bind(&lhs_is_number);
{
// Check if {rhs} is a Smi.
Label rhs_is_smi(assembler), rhs_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &rhs_is_smi,
&rhs_is_not_smi);
assembler->Bind(&rhs_is_smi);
{
// Convert {rhs} to a double and multiply it with the value of {lhs}.
var_lhs_float64.Bind(assembler->LoadHeapNumberValue(lhs));
var_rhs_float64.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fmul);
}
assembler->Bind(&rhs_is_not_smi);
{
Node* rhs_map = assembler->LoadMap(rhs);
// Check if {rhs} is a HeapNumber.
Label rhs_is_number(assembler),
rhs_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(rhs_map, number_map),
&rhs_is_number, &rhs_is_not_number);
assembler->Bind(&rhs_is_number);
{
// Both {lhs} and {rhs} are HeapNumbers. Load their values and
// multiply them.
var_lhs_float64.Bind(assembler->LoadHeapNumberValue(lhs));
var_rhs_float64.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fmul);
}
assembler->Bind(&rhs_is_not_number);
{
// Multiplication is commutative, swap {lhs} with {rhs} and loop.
var_lhs.Bind(rhs);
var_rhs.Bind(lhs);
assembler->Goto(&loop);
}
}
}
assembler->Bind(&lhs_is_not_number);
{
// Convert {lhs} to a Number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_lhs.Bind(assembler->CallStub(callable, context, lhs));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&do_fmul);
{
Node* value =
assembler->Float64Mul(var_lhs_float64.value(), var_rhs_float64.value());
Node* result = assembler->ChangeFloat64ToTagged(value);
var_result.Bind(result);
assembler->Goto(&return_result);
}
assembler->Bind(&return_result);
return var_result.value();
}
// static
compiler::Node* MultiplyWithFeedbackStub::Generate(
CodeStubAssembler* assembler, compiler::Node* lhs, compiler::Node* rhs,
compiler::Node* context, compiler::Node* type_feedback_vector,
compiler::Node* slot_id) {
using compiler::Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
// Shared entry point for floating point multiplication.
Label do_fmul(assembler), end(assembler),
call_multiply_stub(assembler, Label::kDeferred);
Variable var_lhs_float64(assembler, MachineRepresentation::kFloat64),
var_rhs_float64(assembler, MachineRepresentation::kFloat64),
var_result(assembler, MachineRepresentation::kTagged),
var_type_feedback(assembler, MachineRepresentation::kWord32);
Node* number_map = assembler->HeapNumberMapConstant();
Label lhs_is_smi(assembler), lhs_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(lhs), &lhs_is_smi, &lhs_is_not_smi);
assembler->Bind(&lhs_is_smi);
{
Label rhs_is_smi(assembler), rhs_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &rhs_is_smi, &rhs_is_not_smi);
assembler->Bind(&rhs_is_smi);
{
// Both {lhs} and {rhs} are Smis. The result is not necessarily a smi,
// in case of overflow.
var_result.Bind(assembler->SmiMul(lhs, rhs));
var_type_feedback.Bind(assembler->Select(
assembler->WordIsSmi(var_result.value()),
assembler->Int32Constant(BinaryOperationFeedback::kSignedSmall),
assembler->Int32Constant(BinaryOperationFeedback::kNumber),
MachineRepresentation::kWord32));
assembler->Goto(&end);
}
assembler->Bind(&rhs_is_not_smi);
{
Node* rhs_map = assembler->LoadMap(rhs);
// Check if {rhs} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(rhs_map, number_map),
&call_multiply_stub);
// Convert {lhs} to a double and multiply it with the value of {rhs}.
var_lhs_float64.Bind(assembler->SmiToFloat64(lhs));
var_rhs_float64.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fmul);
}
}
assembler->Bind(&lhs_is_not_smi);
{
Node* lhs_map = assembler->LoadMap(lhs);
// Check if {lhs} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(lhs_map, number_map),
&call_multiply_stub);
// Check if {rhs} is a Smi.
Label rhs_is_smi(assembler), rhs_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(rhs), &rhs_is_smi, &rhs_is_not_smi);
assembler->Bind(&rhs_is_smi);
{
// Convert {rhs} to a double and multiply it with the value of {lhs}.
var_lhs_float64.Bind(assembler->LoadHeapNumberValue(lhs));
var_rhs_float64.Bind(assembler->SmiToFloat64(rhs));
assembler->Goto(&do_fmul);
}
assembler->Bind(&rhs_is_not_smi);
{
Node* rhs_map = assembler->LoadMap(rhs);
// Check if {rhs} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(rhs_map, number_map),
&call_multiply_stub);
// Both {lhs} and {rhs} are HeapNumbers. Load their values and
// multiply them.
var_lhs_float64.Bind(assembler->LoadHeapNumberValue(lhs));
var_rhs_float64.Bind(assembler->LoadHeapNumberValue(rhs));
assembler->Goto(&do_fmul);
}
}
assembler->Bind(&do_fmul);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kNumber));
Node* value =
assembler->Float64Mul(var_lhs_float64.value(), var_rhs_float64.value());
Node* result = assembler->ChangeFloat64ToTagged(value);
var_result.Bind(result);
assembler->Goto(&end);
}
assembler->Bind(&call_multiply_stub);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kAny));
Callable callable = CodeFactory::Multiply(assembler->isolate());
var_result.Bind(assembler->CallStub(callable, context, lhs, rhs));
assembler->Goto(&end);
}
assembler->Bind(&end);
assembler->UpdateFeedback(var_type_feedback.value(), type_feedback_vector,
slot_id);
return var_result.value();
}
// static
compiler::Node* DivideStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
// Shared entry point for floating point division.
Label do_fdiv(assembler), end(assembler);
Variable var_dividend_float64(assembler, MachineRepresentation::kFloat64),
var_divisor_float64(assembler, MachineRepresentation::kFloat64);
Node* number_map = assembler->HeapNumberMapConstant();
// We might need to loop one or two times due to ToNumber conversions.
Variable var_dividend(assembler, MachineRepresentation::kTagged),
var_divisor(assembler, MachineRepresentation::kTagged),
var_result(assembler, MachineRepresentation::kTagged);
Variable* loop_variables[] = {&var_dividend, &var_divisor};
Label loop(assembler, 2, loop_variables);
var_dividend.Bind(left);
var_divisor.Bind(right);
assembler->Goto(&loop);
assembler->Bind(&loop);
{
Node* dividend = var_dividend.value();
Node* divisor = var_divisor.value();
Label dividend_is_smi(assembler), dividend_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(dividend), &dividend_is_smi,
&dividend_is_not_smi);
assembler->Bind(&dividend_is_smi);
{
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
Label bailout(assembler);
// Do floating point division if {divisor} is zero.
assembler->GotoIf(
assembler->WordEqual(divisor, assembler->IntPtrConstant(0)),
&bailout);
// Do floating point division {dividend} is zero and {divisor} is
// negative.
Label dividend_is_zero(assembler), dividend_is_not_zero(assembler);
assembler->Branch(
assembler->WordEqual(dividend, assembler->IntPtrConstant(0)),
&dividend_is_zero, &dividend_is_not_zero);
assembler->Bind(&dividend_is_zero);
{
assembler->GotoIf(
assembler->IntPtrLessThan(divisor, assembler->IntPtrConstant(0)),
&bailout);
assembler->Goto(&dividend_is_not_zero);
}
assembler->Bind(&dividend_is_not_zero);
Node* untagged_divisor = assembler->SmiUntag(divisor);
Node* untagged_dividend = assembler->SmiUntag(dividend);
// Do floating point division if {dividend} is kMinInt (or kMinInt - 1
// if the Smi size is 31) and {divisor} is -1.
Label divisor_is_minus_one(assembler),
divisor_is_not_minus_one(assembler);
assembler->Branch(assembler->Word32Equal(untagged_divisor,
assembler->Int32Constant(-1)),
&divisor_is_minus_one, &divisor_is_not_minus_one);
assembler->Bind(&divisor_is_minus_one);
{
assembler->GotoIf(
assembler->Word32Equal(
untagged_dividend,
assembler->Int32Constant(
kSmiValueSize == 32 ? kMinInt : (kMinInt >> 1))),
&bailout);
assembler->Goto(&divisor_is_not_minus_one);
}
assembler->Bind(&divisor_is_not_minus_one);
// TODO(epertoso): consider adding a machine instruction that returns
// both the result and the remainder.
Node* untagged_result =
assembler->Int32Div(untagged_dividend, untagged_divisor);
Node* truncated =
assembler->IntPtrMul(untagged_result, untagged_divisor);
// Do floating point division if the remainder is not 0.
assembler->GotoIf(
assembler->Word32NotEqual(untagged_dividend, truncated), &bailout);
var_result.Bind(assembler->SmiTag(untagged_result));
assembler->Goto(&end);
// Bailout: convert {dividend} and {divisor} to double and do double
// division.
assembler->Bind(&bailout);
{
var_dividend_float64.Bind(assembler->SmiToFloat64(dividend));
var_divisor_float64.Bind(assembler->SmiToFloat64(divisor));
assembler->Goto(&do_fdiv);
}
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
Label divisor_is_number(assembler),
divisor_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(divisor_map, number_map),
&divisor_is_number, &divisor_is_not_number);
assembler->Bind(&divisor_is_number);
{
// Convert {dividend} to a double and divide it with the value of
// {divisor}.
var_dividend_float64.Bind(assembler->SmiToFloat64(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fdiv);
}
assembler->Bind(&divisor_is_not_number);
{
// Convert {divisor} to a number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_divisor.Bind(assembler->CallStub(callable, context, divisor));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&dividend_is_not_smi);
{
Node* dividend_map = assembler->LoadMap(dividend);
// Check if {dividend} is a HeapNumber.
Label dividend_is_number(assembler),
dividend_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(dividend_map, number_map),
&dividend_is_number, &dividend_is_not_number);
assembler->Bind(&dividend_is_number);
{
// Check if {divisor} is a Smi.
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
// Convert {divisor} to a double and use it for a floating point
// division.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->SmiToFloat64(divisor));
assembler->Goto(&do_fdiv);
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
Label divisor_is_number(assembler),
divisor_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(divisor_map, number_map),
&divisor_is_number, &divisor_is_not_number);
assembler->Bind(&divisor_is_number);
{
// Both {dividend} and {divisor} are HeapNumbers. Load their values
// and divide them.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fdiv);
}
assembler->Bind(&divisor_is_not_number);
{
// Convert {divisor} to a number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_divisor.Bind(assembler->CallStub(callable, context, divisor));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&dividend_is_not_number);
{
// Convert {dividend} to a Number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_dividend.Bind(assembler->CallStub(callable, context, dividend));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&do_fdiv);
{
Node* value = assembler->Float64Div(var_dividend_float64.value(),
var_divisor_float64.value());
var_result.Bind(assembler->ChangeFloat64ToTagged(value));
assembler->Goto(&end);
}
assembler->Bind(&end);
return var_result.value();
}
// static
compiler::Node* DivideWithFeedbackStub::Generate(
CodeStubAssembler* assembler, compiler::Node* dividend,
compiler::Node* divisor, compiler::Node* context,
compiler::Node* type_feedback_vector, compiler::Node* slot_id) {
using compiler::Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
// Shared entry point for floating point division.
Label do_fdiv(assembler), end(assembler), call_divide_stub(assembler);
Variable var_dividend_float64(assembler, MachineRepresentation::kFloat64),
var_divisor_float64(assembler, MachineRepresentation::kFloat64),
var_result(assembler, MachineRepresentation::kTagged),
var_type_feedback(assembler, MachineRepresentation::kWord32);
Node* number_map = assembler->HeapNumberMapConstant();
Label dividend_is_smi(assembler), dividend_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(dividend), &dividend_is_smi,
&dividend_is_not_smi);
assembler->Bind(&dividend_is_smi);
{
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
Label bailout(assembler);
// Do floating point division if {divisor} is zero.
assembler->GotoIf(
assembler->WordEqual(divisor, assembler->IntPtrConstant(0)),
&bailout);
// Do floating point division {dividend} is zero and {divisor} is
// negative.
Label dividend_is_zero(assembler), dividend_is_not_zero(assembler);
assembler->Branch(
assembler->WordEqual(dividend, assembler->IntPtrConstant(0)),
&dividend_is_zero, &dividend_is_not_zero);
assembler->Bind(&dividend_is_zero);
{
assembler->GotoIf(
assembler->IntPtrLessThan(divisor, assembler->IntPtrConstant(0)),
&bailout);
assembler->Goto(&dividend_is_not_zero);
}
assembler->Bind(&dividend_is_not_zero);
Node* untagged_divisor = assembler->SmiUntag(divisor);
Node* untagged_dividend = assembler->SmiUntag(dividend);
// Do floating point division if {dividend} is kMinInt (or kMinInt - 1
// if the Smi size is 31) and {divisor} is -1.
Label divisor_is_minus_one(assembler),
divisor_is_not_minus_one(assembler);
assembler->Branch(assembler->Word32Equal(untagged_divisor,
assembler->Int32Constant(-1)),
&divisor_is_minus_one, &divisor_is_not_minus_one);
assembler->Bind(&divisor_is_minus_one);
{
assembler->GotoIf(
assembler->Word32Equal(
untagged_dividend,
assembler->Int32Constant(kSmiValueSize == 32 ? kMinInt
: (kMinInt >> 1))),
&bailout);
assembler->Goto(&divisor_is_not_minus_one);
}
assembler->Bind(&divisor_is_not_minus_one);
Node* untagged_result =
assembler->Int32Div(untagged_dividend, untagged_divisor);
Node* truncated = assembler->IntPtrMul(untagged_result, untagged_divisor);
// Do floating point division if the remainder is not 0.
assembler->GotoIf(assembler->Word32NotEqual(untagged_dividend, truncated),
&bailout);
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kSignedSmall));
var_result.Bind(assembler->SmiTag(untagged_result));
assembler->Goto(&end);
// Bailout: convert {dividend} and {divisor} to double and do double
// division.
assembler->Bind(&bailout);
{
var_dividend_float64.Bind(assembler->SmiToFloat64(dividend));
var_divisor_float64.Bind(assembler->SmiToFloat64(divisor));
assembler->Goto(&do_fdiv);
}
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(divisor_map, number_map),
&call_divide_stub);
// Convert {dividend} to a double and divide it with the value of
// {divisor}.
var_dividend_float64.Bind(assembler->SmiToFloat64(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fdiv);
}
assembler->Bind(&dividend_is_not_smi);
{
Node* dividend_map = assembler->LoadMap(dividend);
// Check if {dividend} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(dividend_map, number_map),
&call_divide_stub);
// Check if {divisor} is a Smi.
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
// Convert {divisor} to a double and use it for a floating point
// division.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->SmiToFloat64(divisor));
assembler->Goto(&do_fdiv);
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(divisor_map, number_map),
&call_divide_stub);
// Both {dividend} and {divisor} are HeapNumbers. Load their values
// and divide them.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fdiv);
}
}
}
assembler->Bind(&do_fdiv);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kNumber));
Node* value = assembler->Float64Div(var_dividend_float64.value(),
var_divisor_float64.value());
var_result.Bind(assembler->ChangeFloat64ToTagged(value));
assembler->Goto(&end);
}
assembler->Bind(&call_divide_stub);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kAny));
Callable callable = CodeFactory::Divide(assembler->isolate());
var_result.Bind(assembler->CallStub(callable, context, dividend, divisor));
assembler->Goto(&end);
}
assembler->Bind(&end);
assembler->UpdateFeedback(var_type_feedback.value(), type_feedback_vector,
slot_id);
return var_result.value();
}
// static
compiler::Node* ModulusStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
Variable var_result(assembler, MachineRepresentation::kTagged);
Label return_result(assembler, &var_result);
// Shared entry point for floating point modulus.
Label do_fmod(assembler);
Variable var_dividend_float64(assembler, MachineRepresentation::kFloat64),
var_divisor_float64(assembler, MachineRepresentation::kFloat64);
Node* number_map = assembler->HeapNumberMapConstant();
// We might need to loop one or two times due to ToNumber conversions.
Variable var_dividend(assembler, MachineRepresentation::kTagged),
var_divisor(assembler, MachineRepresentation::kTagged);
Variable* loop_variables[] = {&var_dividend, &var_divisor};
Label loop(assembler, 2, loop_variables);
var_dividend.Bind(left);
var_divisor.Bind(right);
assembler->Goto(&loop);
assembler->Bind(&loop);
{
Node* dividend = var_dividend.value();
Node* divisor = var_divisor.value();
Label dividend_is_smi(assembler), dividend_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(dividend), &dividend_is_smi,
&dividend_is_not_smi);
assembler->Bind(&dividend_is_smi);
{
Label dividend_is_not_zero(assembler);
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
// Compute the modulus of two Smis.
var_result.Bind(assembler->SmiMod(dividend, divisor));
assembler->Goto(&return_result);
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
Label divisor_is_number(assembler),
divisor_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(divisor_map, number_map),
&divisor_is_number, &divisor_is_not_number);
assembler->Bind(&divisor_is_number);
{
// Convert {dividend} to a double and compute its modulus with the
// value of {dividend}.
var_dividend_float64.Bind(assembler->SmiToFloat64(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fmod);
}
assembler->Bind(&divisor_is_not_number);
{
// Convert {divisor} to a number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_divisor.Bind(assembler->CallStub(callable, context, divisor));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&dividend_is_not_smi);
{
Node* dividend_map = assembler->LoadMap(dividend);
// Check if {dividend} is a HeapNumber.
Label dividend_is_number(assembler),
dividend_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(dividend_map, number_map),
&dividend_is_number, &dividend_is_not_number);
assembler->Bind(&dividend_is_number);
{
// Check if {divisor} is a Smi.
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
// Convert {divisor} to a double and compute {dividend}'s modulus with
// it.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->SmiToFloat64(divisor));
assembler->Goto(&do_fmod);
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
Label divisor_is_number(assembler),
divisor_is_not_number(assembler, Label::kDeferred);
assembler->Branch(assembler->WordEqual(divisor_map, number_map),
&divisor_is_number, &divisor_is_not_number);
assembler->Bind(&divisor_is_number);
{
// Both {dividend} and {divisor} are HeapNumbers. Load their values
// and compute their modulus.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fmod);
}
assembler->Bind(&divisor_is_not_number);
{
// Convert {divisor} to a number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_divisor.Bind(assembler->CallStub(callable, context, divisor));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&dividend_is_not_number);
{
// Convert {dividend} to a Number and loop.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
var_dividend.Bind(assembler->CallStub(callable, context, dividend));
assembler->Goto(&loop);
}
}
}
assembler->Bind(&do_fmod);
{
Node* value = assembler->Float64Mod(var_dividend_float64.value(),
var_divisor_float64.value());
var_result.Bind(assembler->ChangeFloat64ToTagged(value));
assembler->Goto(&return_result);
}
assembler->Bind(&return_result);
return var_result.value();
}
// static
compiler::Node* ModulusWithFeedbackStub::Generate(
CodeStubAssembler* assembler, compiler::Node* dividend,
compiler::Node* divisor, compiler::Node* context,
compiler::Node* type_feedback_vector, compiler::Node* slot_id) {
using compiler::Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
// Shared entry point for floating point division.
Label do_fmod(assembler), end(assembler), call_modulus_stub(assembler);
Variable var_dividend_float64(assembler, MachineRepresentation::kFloat64),
var_divisor_float64(assembler, MachineRepresentation::kFloat64),
var_result(assembler, MachineRepresentation::kTagged),
var_type_feedback(assembler, MachineRepresentation::kWord32);
Node* number_map = assembler->HeapNumberMapConstant();
Label dividend_is_smi(assembler), dividend_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(dividend), &dividend_is_smi,
&dividend_is_not_smi);
assembler->Bind(&dividend_is_smi);
{
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
var_result.Bind(assembler->SmiMod(dividend, divisor));
var_type_feedback.Bind(assembler->Select(
assembler->WordIsSmi(var_result.value()),
assembler->Int32Constant(BinaryOperationFeedback::kSignedSmall),
assembler->Int32Constant(BinaryOperationFeedback::kNumber)));
assembler->Goto(&end);
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(divisor_map, number_map),
&call_modulus_stub);
// Convert {dividend} to a double and divide it with the value of
// {divisor}.
var_dividend_float64.Bind(assembler->SmiToFloat64(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fmod);
}
}
assembler->Bind(&dividend_is_not_smi);
{
Node* dividend_map = assembler->LoadMap(dividend);
// Check if {dividend} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(dividend_map, number_map),
&call_modulus_stub);
// Check if {divisor} is a Smi.
Label divisor_is_smi(assembler), divisor_is_not_smi(assembler);
assembler->Branch(assembler->WordIsSmi(divisor), &divisor_is_smi,
&divisor_is_not_smi);
assembler->Bind(&divisor_is_smi);
{
// Convert {divisor} to a double and use it for a floating point
// division.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->SmiToFloat64(divisor));
assembler->Goto(&do_fmod);
}
assembler->Bind(&divisor_is_not_smi);
{
Node* divisor_map = assembler->LoadMap(divisor);
// Check if {divisor} is a HeapNumber.
assembler->GotoUnless(assembler->WordEqual(divisor_map, number_map),
&call_modulus_stub);
// Both {dividend} and {divisor} are HeapNumbers. Load their values
// and divide them.
var_dividend_float64.Bind(assembler->LoadHeapNumberValue(dividend));
var_divisor_float64.Bind(assembler->LoadHeapNumberValue(divisor));
assembler->Goto(&do_fmod);
}
}
assembler->Bind(&do_fmod);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kNumber));
Node* value = assembler->Float64Mod(var_dividend_float64.value(),
var_divisor_float64.value());
var_result.Bind(assembler->ChangeFloat64ToTagged(value));
assembler->Goto(&end);
}
assembler->Bind(&call_modulus_stub);
{
var_type_feedback.Bind(
assembler->Int32Constant(BinaryOperationFeedback::kAny));
Callable callable = CodeFactory::Modulus(assembler->isolate());
var_result.Bind(assembler->CallStub(callable, context, dividend, divisor));
assembler->Goto(&end);
}
assembler->Bind(&end);
assembler->UpdateFeedback(var_type_feedback.value(), type_feedback_vector,
slot_id);
return var_result.value();
}
// static
compiler::Node* ShiftLeftStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
Node* lhs_value = assembler->TruncateTaggedToWord32(context, left);
Node* rhs_value = assembler->TruncateTaggedToWord32(context, right);
Node* shift_count =
assembler->Word32And(rhs_value, assembler->Int32Constant(0x1f));
Node* value = assembler->Word32Shl(lhs_value, shift_count);
Node* result = assembler->ChangeInt32ToTagged(value);
return result;
}
// static
compiler::Node* ShiftRightStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
Node* lhs_value = assembler->TruncateTaggedToWord32(context, left);
Node* rhs_value = assembler->TruncateTaggedToWord32(context, right);
Node* shift_count =
assembler->Word32And(rhs_value, assembler->Int32Constant(0x1f));
Node* value = assembler->Word32Sar(lhs_value, shift_count);
Node* result = assembler->ChangeInt32ToTagged(value);
return result;
}
// static
compiler::Node* ShiftRightLogicalStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
Node* lhs_value = assembler->TruncateTaggedToWord32(context, left);
Node* rhs_value = assembler->TruncateTaggedToWord32(context, right);
Node* shift_count =
assembler->Word32And(rhs_value, assembler->Int32Constant(0x1f));
Node* value = assembler->Word32Shr(lhs_value, shift_count);
Node* result = assembler->ChangeUint32ToTagged(value);
return result;
}
// static
compiler::Node* BitwiseAndStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
Node* lhs_value = assembler->TruncateTaggedToWord32(context, left);
Node* rhs_value = assembler->TruncateTaggedToWord32(context, right);
Node* value = assembler->Word32And(lhs_value, rhs_value);
Node* result = assembler->ChangeInt32ToTagged(value);
return result;
}
// static
compiler::Node* BitwiseOrStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
Node* lhs_value = assembler->TruncateTaggedToWord32(context, left);
Node* rhs_value = assembler->TruncateTaggedToWord32(context, right);
Node* value = assembler->Word32Or(lhs_value, rhs_value);
Node* result = assembler->ChangeInt32ToTagged(value);
return result;
}
// static
compiler::Node* BitwiseXorStub::Generate(CodeStubAssembler* assembler,
compiler::Node* left,
compiler::Node* right,
compiler::Node* context) {
using compiler::Node;
Node* lhs_value = assembler->TruncateTaggedToWord32(context, left);
Node* rhs_value = assembler->TruncateTaggedToWord32(context, right);
Node* value = assembler->Word32Xor(lhs_value, rhs_value);
Node* result = assembler->ChangeInt32ToTagged(value);
return result;
}
// static
compiler::Node* IncStub::Generate(CodeStubAssembler* assembler,
compiler::Node* value,
compiler::Node* context) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
// Shared entry for floating point increment.
Label do_finc(assembler), end(assembler);
Variable var_finc_value(assembler, MachineRepresentation::kFloat64);
// We might need to try again due to ToNumber conversion.
Variable value_var(assembler, MachineRepresentation::kTagged);
Variable result_var(assembler, MachineRepresentation::kTagged);
Label start(assembler, &value_var);
value_var.Bind(value);
assembler->Goto(&start);
assembler->Bind(&start);
{
value = value_var.value();
Label if_issmi(assembler), if_isnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(value), &if_issmi, &if_isnotsmi);
assembler->Bind(&if_issmi);
{
// Try fast Smi addition first.
Node* one = assembler->SmiConstant(Smi::FromInt(1));
Node* pair = assembler->SmiAddWithOverflow(value, one);
Node* overflow = assembler->Projection(1, pair);
// Check if the Smi addition overflowed.
Label if_overflow(assembler), if_notoverflow(assembler);
assembler->Branch(overflow, &if_overflow, &if_notoverflow);
assembler->Bind(&if_notoverflow);
result_var.Bind(assembler->Projection(0, pair));
assembler->Goto(&end);
assembler->Bind(&if_overflow);
{
var_finc_value.Bind(assembler->SmiToFloat64(value));
assembler->Goto(&do_finc);
}
}
assembler->Bind(&if_isnotsmi);
{
// Check if the value is a HeapNumber.
Label if_valueisnumber(assembler),
if_valuenotnumber(assembler, Label::kDeferred);
Node* value_map = assembler->LoadMap(value);
Node* number_map = assembler->HeapNumberMapConstant();
assembler->Branch(assembler->WordEqual(value_map, number_map),
&if_valueisnumber, &if_valuenotnumber);
assembler->Bind(&if_valueisnumber);
{
// Load the HeapNumber value.
var_finc_value.Bind(assembler->LoadHeapNumberValue(value));
assembler->Goto(&do_finc);
}
assembler->Bind(&if_valuenotnumber);
{
// Convert to a Number first and try again.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
value_var.Bind(assembler->CallStub(callable, context, value));
assembler->Goto(&start);
}
}
}
assembler->Bind(&do_finc);
{
Node* finc_value = var_finc_value.value();
Node* one = assembler->Float64Constant(1.0);
Node* finc_result = assembler->Float64Add(finc_value, one);
result_var.Bind(assembler->ChangeFloat64ToTagged(finc_result));
assembler->Goto(&end);
}
assembler->Bind(&end);
return result_var.value();
}
// static
compiler::Node* DecStub::Generate(CodeStubAssembler* assembler,
compiler::Node* value,
compiler::Node* context) {
typedef CodeStubAssembler::Label Label;
typedef compiler::Node Node;
typedef CodeStubAssembler::Variable Variable;
// Shared entry for floating point decrement.
Label do_fdec(assembler), end(assembler);
Variable var_fdec_value(assembler, MachineRepresentation::kFloat64);
// We might need to try again due to ToNumber conversion.
Variable value_var(assembler, MachineRepresentation::kTagged);
Variable result_var(assembler, MachineRepresentation::kTagged);
Label start(assembler, &value_var);
value_var.Bind(value);
assembler->Goto(&start);
assembler->Bind(&start);
{
value = value_var.value();
Label if_issmi(assembler), if_isnotsmi(assembler);
assembler->Branch(assembler->WordIsSmi(value), &if_issmi, &if_isnotsmi);
assembler->Bind(&if_issmi);
{
// Try fast Smi subtraction first.
Node* one = assembler->SmiConstant(Smi::FromInt(1));
Node* pair = assembler->SmiSubWithOverflow(value, one);
Node* overflow = assembler->Projection(1, pair);
// Check if the Smi subtraction overflowed.
Label if_overflow(assembler), if_notoverflow(assembler);
assembler->Branch(overflow, &if_overflow, &if_notoverflow);
assembler->Bind(&if_notoverflow);
result_var.Bind(assembler->Projection(0, pair));
assembler->Goto(&end);
assembler->Bind(&if_overflow);
{
var_fdec_value.Bind(assembler->SmiToFloat64(value));
assembler->Goto(&do_fdec);
}
}
assembler->Bind(&if_isnotsmi);
{
// Check if the value is a HeapNumber.
Label if_valueisnumber(assembler),
if_valuenotnumber(assembler, Label::kDeferred);
Node* value_map = assembler->LoadMap(value);
Node* number_map = assembler->HeapNumberMapConstant();
assembler->Branch(assembler->WordEqual(value_map, number_map),
&if_valueisnumber, &if_valuenotnumber);
assembler->Bind(&if_valueisnumber);
{
// Load the HeapNumber value.
var_fdec_value.Bind(assembler->LoadHeapNumberValue(value));
assembler->Goto(&do_fdec);
}
assembler->Bind(&if_valuenotnumber);
{
// Convert to a Number first and try again.
Callable callable =
CodeFactory::NonNumberToNumber(assembler->isolate());
value_var.Bind(assembler->CallStub(callable, context, value));
assembler->Goto(&start);
}
}
}
assembler->Bind(&do_fdec);
{
Node* fdec_value = var_fdec_value.value();
Node* one = assembler->Float64Constant(1.0);
Node* fdec_result = assembler->Float64Sub(fdec_value, one);
result_var.Bind(assembler->ChangeFloat64ToTagged(fdec_result));
assembler->Goto(&end);
}
assembler->Bind(&end);
return result_var.value();
}
// ES6 section 7.1.13 ToObject (argument)
void ToObjectStub::GenerateAssembly(CodeStubAssembler* assembler) const {
typedef compiler::Node Node;
typedef CodeStubAssembler::Label Label;
typedef CodeStubAssembler::Variable Variable;
Label if_number(assembler, Label::kDeferred), if_notsmi(assembler),
if_jsreceiver(assembler), if_noconstructor(assembler, Label::kDeferred),
if_wrapjsvalue(assembler);
Node* object = assembler->Parameter(Descriptor::kArgument);
Node* context = assembler->Parameter(Descriptor::kContext);
Variable constructor_function_index_var(assembler,
MachineRepresentation::kWord32);
assembler->Branch(assembler->WordIsSmi(object), &if_number, &if_notsmi);
assembler->Bind(&if_notsmi);
Node* map = assembler->LoadMap(object);
assembler->GotoIf(
assembler->WordEqual(map, assembler->HeapNumberMapConstant()),
&if_number);
Node* instance_type = assembler->LoadMapInstanceType(map);
assembler->GotoIf(
assembler->Int32GreaterThanOrEqual(
instance_type, assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE)),
&if_jsreceiver);
Node* constructor_function_index = assembler->LoadObjectField(
map, Map::kInObjectPropertiesOrConstructorFunctionIndexOffset,
MachineType::Uint8());
assembler->GotoIf(
assembler->Word32Equal(
constructor_function_index,
assembler->Int32Constant(Map::kNoConstructorFunctionIndex)),
&if_noconstructor);
constructor_function_index_var.Bind(constructor_function_index);
assembler->Goto(&if_wrapjsvalue);
assembler->Bind(&if_number);
constructor_function_index_var.Bind(
assembler->Int32Constant(Context::NUMBER_FUNCTION_INDEX));
assembler->Goto(&if_wrapjsvalue);
assembler->Bind(&if_wrapjsvalue);
Node* native_context = assembler->LoadNativeContext(context);
Node* constructor = assembler->LoadFixedArrayElement(
native_context, constructor_function_index_var.value());
Node* initial_map = assembler->LoadObjectField(
constructor, JSFunction::kPrototypeOrInitialMapOffset);
Node* js_value = assembler->Allocate(JSValue::kSize);
assembler->StoreMapNoWriteBarrier(js_value, initial_map);
assembler->StoreObjectFieldRoot(js_value, JSValue::kPropertiesOffset,