code = pre_monomorphic_stub( isolate, StoreICState::GetLanguageMode(target->extra_ic_state())); SetTargetAtAddress(address, *code, constant_pool); } void KeyedStoreIC::Clear(Isolate* isolate, Code* host, KeyedStoreICNexus* nexus) { if (IsCleared(nexus)) return; nexus->ConfigurePremonomorphic(); OnTypeFeedbackChanged(isolate, host); } void CompareIC::Clear(Isolate* isolate, Address address, Code* target, Address constant_pool) { DCHECK(CodeStub::GetMajorKey(target) == CodeStub::CompareIC); CompareICStub stub(target->stub_key(), isolate); // Only clear CompareICs that can retain objects. if (stub.state() != CompareICState::KNOWN_RECEIVER) return; SetTargetAtAddress(address, GetRawUninitialized(isolate, stub.op(), stub.strength()), constant_pool); PatchInlinedSmiCode(isolate, address, DISABLE_INLINED_SMI_CHECK); } // static Handle KeyedLoadIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { if (FLAG_compiled_keyed_generic_loads) { return KeyedLoadGenericStub(isolate, LoadICState(extra_state)).GetCode(); } else { return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } } static bool MigrateDeprecated(Handle object) { if (!object->IsJSObject()) return false; Handle receiver = Handle::cast(object); if (!receiver->map()->is_deprecated()) return false; JSObject::MigrateInstance(Handle::cast(object)); return true; } void IC::ConfigureVectorState(IC::State new_state) { DCHECK(UseVector()); if (new_state == PREMONOMORPHIC) { nexus()->ConfigurePremonomorphic(); } else if (new_state == MEGAMORPHIC) { nexus()->ConfigureMegamorphic(); } else { UNREACHABLE(); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(Handle name, Handle map, Handle handler) { DCHECK(UseVector()); if (kind() == Code::LOAD_IC) { LoadICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(map, handler); } else if (kind() == Code::KEYED_LOAD_IC) { KeyedLoadICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(name, map, handler); } else if (kind() == Code::STORE_IC) { StoreICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(map, handler); } else { DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(name, map, handler); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(Handle name, MapHandleList* maps, CodeHandleList* handlers) { DCHECK(UseVector()); if (kind() == Code::LOAD_IC) { LoadICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, handlers); } else if (kind() == Code::KEYED_LOAD_IC) { KeyedLoadICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(name, maps, handlers); } else if (kind() == Code::STORE_IC) { StoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, handlers); } else { DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(name, maps, handlers); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(MapHandleList* maps, MapHandleList* transitioned_maps, CodeHandleList* handlers) { DCHECK(UseVector()); DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, transitioned_maps, handlers); vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } MaybeHandle LoadIC::Load(Handle object, Handle name) { // If the object is undefined or null it's illegal to try to get any // of its properties; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyLoad, object, name); } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index; if (kind() == Code::KEYED_LOAD_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) { DCHECK(UseVector()); ConfigureVectorState(MEGAMORPHIC); TRACE_IC("LoadIC", name); TRACE_GENERIC_IC(isolate(), "LoadIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::GetElement(isolate(), object, index, language_mode()), Object); return result; } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle result = FixedArray::get(ScriptContextTable::GetContext( script_contexts, lookup_result.context_index), lookup_result.slot_index); if (*result == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (use_ic && LoadScriptContextFieldStub::Accepted(&lookup_result)) { LoadScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } return result; } } // Named lookup in the object. LookupIterator it(object, name); LookupForRead(&it); if (it.IsFound() || !ShouldThrowReferenceError(object)) { // Update inline cache and stub cache. if (use_ic) UpdateCaches(&it); // Get the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::GetProperty(&it, language_mode()), Object); if (it.IsFound()) { return result; } else if (!ShouldThrowReferenceError(object)) { LOG(isolate(), SuspectReadEvent(*name, *object)); return result; } } return ReferenceError(name); } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { DCHECK(!new_receiver_map.is_null()); for (int current = 0; current < receiver_maps->length(); ++current) { if (!receiver_maps->at(current).is_null() && receiver_maps->at(current).is_identical_to(new_receiver_map)) { return false; } } receiver_maps->Add(new_receiver_map); return true; } bool IC::UpdatePolymorphicIC(Handle name, Handle code) { if (!code->is_handler()) return false; if (target()->is_keyed_stub() && state() != PROTOTYPE_FAILURE) return false; Handle map = receiver_map(); MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); int number_of_maps = maps.length(); int deprecated_maps = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_maps; i++) { Handle current_map = maps.at(i); if (current_map->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_maps; } else if (map.is_identical_to(current_map)) { // If the receiver type is already in the polymorphic IC, this indicates // there was a prototoype chain failure. In that case, just overwrite the // handler. handler_to_overwrite = i; } else if (handler_to_overwrite == -1 && IsTransitionOfMonomorphicTarget(*current_map, *map)) { handler_to_overwrite = i; } } int number_of_valid_maps = number_of_maps - deprecated_maps - (handler_to_overwrite != -1); if (number_of_valid_maps >= 4) return false; if (number_of_maps == 0 && state() != MONOMORPHIC && state() != POLYMORPHIC) { return false; } if (UseVector()) { if (!nexus()->FindHandlers(&handlers, maps.length())) return false; } else { if (!target()->FindHandlers(&handlers, maps.length())) return false; } number_of_valid_maps++; if (number_of_valid_maps > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_maps == 1) { ConfigureVectorState(name, receiver_map(), code); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!map.is_identical_to(maps.at(handler_to_overwrite))) { maps.Set(handler_to_overwrite, map); } } else { maps.Add(map); handlers.Add(code); } ConfigureVectorState(name, &maps, &handlers); } if (!UseVector()) set_target(*ic); return true; } void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); ConfigureVectorState(name, receiver_map(), handler); } void IC::CopyICToMegamorphicCache(Handle name) { MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); if (!target()->FindHandlers(&handlers, maps.length())) return; for (int i = 0; i < maps.length(); i++) { UpdateMegamorphicCache(*maps.at(i), *name, *handlers.at(i)); } } bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) { if (source_map == NULL) return true; if (target_map == NULL) return false; ElementsKind target_elements_kind = target_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( source_map->elements_kind(), target_elements_kind); Map* transitioned_map = more_general_transition ? source_map->LookupElementsTransitionMap(target_elements_kind) : NULL; return transitioned_map == target_map; } void IC::PatchCache(Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedLoadIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { if (FLAG_compiled_keyed_generic_loads) { return KeyedLoadGenericStub(isolate, LoadICState(extra_state)).GetCode(); } else { return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } } static bool MigrateDeprecated(Handle object) { if (!object->IsJSObject()) return false; Handle receiver = Handle::cast(object); if (!receiver->map()->is_deprecated()) return false; JSObject::MigrateInstance(Handle::cast(object)); return true; } void IC::ConfigureVectorState(IC::State new_state) { DCHECK(UseVector()); if (new_state == PREMONOMORPHIC) { nexus()->ConfigurePremonomorphic(); } else if (new_state == MEGAMORPHIC) { nexus()->ConfigureMegamorphic(); } else { UNREACHABLE(); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(Handle name, Handle map, Handle handler) { DCHECK(UseVector()); if (kind() == Code::LOAD_IC) { LoadICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(map, handler); } else if (kind() == Code::KEYED_LOAD_IC) { KeyedLoadICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(name, map, handler); } else if (kind() == Code::STORE_IC) { StoreICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(map, handler); } else { DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(name, map, handler); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(Handle name, MapHandleList* maps, CodeHandleList* handlers) { DCHECK(UseVector()); if (kind() == Code::LOAD_IC) { LoadICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, handlers); } else if (kind() == Code::KEYED_LOAD_IC) { KeyedLoadICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(name, maps, handlers); } else if (kind() == Code::STORE_IC) { StoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, handlers); } else { DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(name, maps, handlers); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(MapHandleList* maps, MapHandleList* transitioned_maps, CodeHandleList* handlers) { DCHECK(UseVector()); DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, transitioned_maps, handlers); vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } MaybeHandle LoadIC::Load(Handle object, Handle name) { // If the object is undefined or null it's illegal to try to get any // of its properties; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyLoad, object, name); } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index; if (kind() == Code::KEYED_LOAD_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) { DCHECK(UseVector()); ConfigureVectorState(MEGAMORPHIC); TRACE_IC("LoadIC", name); TRACE_GENERIC_IC(isolate(), "LoadIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::GetElement(isolate(), object, index, language_mode()), Object); return result; } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle result = FixedArray::get(ScriptContextTable::GetContext( script_contexts, lookup_result.context_index), lookup_result.slot_index); if (*result == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (use_ic && LoadScriptContextFieldStub::Accepted(&lookup_result)) { LoadScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } return result; } } // Named lookup in the object. LookupIterator it(object, name); LookupForRead(&it); if (it.IsFound() || !ShouldThrowReferenceError(object)) { // Update inline cache and stub cache. if (use_ic) UpdateCaches(&it); // Get the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::GetProperty(&it, language_mode()), Object); if (it.IsFound()) { return result; } else if (!ShouldThrowReferenceError(object)) { LOG(isolate(), SuspectReadEvent(*name, *object)); return result; } } return ReferenceError(name); } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { DCHECK(!new_receiver_map.is_null()); for (int current = 0; current < receiver_maps->length(); ++current) { if (!receiver_maps->at(current).is_null() && receiver_maps->at(current).is_identical_to(new_receiver_map)) { return false; } } receiver_maps->Add(new_receiver_map); return true; } bool IC::UpdatePolymorphicIC(Handle name, Handle code) { if (!code->is_handler()) return false; if (target()->is_keyed_stub() && state() != PROTOTYPE_FAILURE) return false; Handle map = receiver_map(); MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); int number_of_maps = maps.length(); int deprecated_maps = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_maps; i++) { Handle current_map = maps.at(i); if (current_map->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_maps; } else if (map.is_identical_to(current_map)) { // If the receiver type is already in the polymorphic IC, this indicates // there was a prototoype chain failure. In that case, just overwrite the // handler. handler_to_overwrite = i; } else if (handler_to_overwrite == -1 && IsTransitionOfMonomorphicTarget(*current_map, *map)) { handler_to_overwrite = i; } } int number_of_valid_maps = number_of_maps - deprecated_maps - (handler_to_overwrite != -1); if (number_of_valid_maps >= 4) return false; if (number_of_maps == 0 && state() != MONOMORPHIC && state() != POLYMORPHIC) { return false; } if (UseVector()) { if (!nexus()->FindHandlers(&handlers, maps.length())) return false; } else { if (!target()->FindHandlers(&handlers, maps.length())) return false; } number_of_valid_maps++; if (number_of_valid_maps > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_maps == 1) { ConfigureVectorState(name, receiver_map(), code); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!map.is_identical_to(maps.at(handler_to_overwrite))) { maps.Set(handler_to_overwrite, map); } } else { maps.Add(map); handlers.Add(code); } ConfigureVectorState(name, &maps, &handlers); } if (!UseVector()) set_target(*ic); return true; } void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); ConfigureVectorState(name, receiver_map(), handler); } void IC::CopyICToMegamorphicCache(Handle name) { MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); if (!target()->FindHandlers(&handlers, maps.length())) return; for (int i = 0; i < maps.length(); i++) { UpdateMegamorphicCache(*maps.at(i), *name, *handlers.at(i)); } } bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) { if (source_map == NULL) return true; if (target_map == NULL) return false; ElementsKind target_elements_kind = target_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( source_map->elements_kind(), target_elements_kind); Map* transitioned_map = more_general_transition ? source_map->LookupElementsTransitionMap(target_elements_kind) : NULL; return transitioned_map == target_map; } void IC::PatchCache(Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler) { DCHECK(UseVector()); if (kind() == Code::LOAD_IC) { LoadICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(map, handler); } else if (kind() == Code::KEYED_LOAD_IC) { KeyedLoadICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(name, map, handler); } else if (kind() == Code::STORE_IC) { StoreICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(map, handler); } else { DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigureMonomorphic(name, map, handler); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(Handle name, MapHandleList* maps, CodeHandleList* handlers) { DCHECK(UseVector()); if (kind() == Code::LOAD_IC) { LoadICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, handlers); } else if (kind() == Code::KEYED_LOAD_IC) { KeyedLoadICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(name, maps, handlers); } else if (kind() == Code::STORE_IC) { StoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, handlers); } else { DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(name, maps, handlers); } vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } void IC::ConfigureVectorState(MapHandleList* maps, MapHandleList* transitioned_maps, CodeHandleList* handlers) { DCHECK(UseVector()); DCHECK(kind() == Code::KEYED_STORE_IC); KeyedStoreICNexus* nexus = casted_nexus(); nexus->ConfigurePolymorphic(maps, transitioned_maps, handlers); vector_set_ = true; OnTypeFeedbackChanged(isolate(), get_host()); } MaybeHandle LoadIC::Load(Handle object, Handle name) { // If the object is undefined or null it's illegal to try to get any // of its properties; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyLoad, object, name); } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index; if (kind() == Code::KEYED_LOAD_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) { DCHECK(UseVector()); ConfigureVectorState(MEGAMORPHIC); TRACE_IC("LoadIC", name); TRACE_GENERIC_IC(isolate(), "LoadIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::GetElement(isolate(), object, index, language_mode()), Object); return result; } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle result = FixedArray::get(ScriptContextTable::GetContext( script_contexts, lookup_result.context_index), lookup_result.slot_index); if (*result == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (use_ic && LoadScriptContextFieldStub::Accepted(&lookup_result)) { LoadScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } return result; } } // Named lookup in the object. LookupIterator it(object, name); LookupForRead(&it); if (it.IsFound() || !ShouldThrowReferenceError(object)) { // Update inline cache and stub cache. if (use_ic) UpdateCaches(&it); // Get the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::GetProperty(&it, language_mode()), Object); if (it.IsFound()) { return result; } else if (!ShouldThrowReferenceError(object)) { LOG(isolate(), SuspectReadEvent(*name, *object)); return result; } } return ReferenceError(name); } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { DCHECK(!new_receiver_map.is_null()); for (int current = 0; current < receiver_maps->length(); ++current) { if (!receiver_maps->at(current).is_null() && receiver_maps->at(current).is_identical_to(new_receiver_map)) { return false; } } receiver_maps->Add(new_receiver_map); return true; } bool IC::UpdatePolymorphicIC(Handle name, Handle code) { if (!code->is_handler()) return false; if (target()->is_keyed_stub() && state() != PROTOTYPE_FAILURE) return false; Handle map = receiver_map(); MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); int number_of_maps = maps.length(); int deprecated_maps = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_maps; i++) { Handle current_map = maps.at(i); if (current_map->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_maps; } else if (map.is_identical_to(current_map)) { // If the receiver type is already in the polymorphic IC, this indicates // there was a prototoype chain failure. In that case, just overwrite the // handler. handler_to_overwrite = i; } else if (handler_to_overwrite == -1 && IsTransitionOfMonomorphicTarget(*current_map, *map)) { handler_to_overwrite = i; } } int number_of_valid_maps = number_of_maps - deprecated_maps - (handler_to_overwrite != -1); if (number_of_valid_maps >= 4) return false; if (number_of_maps == 0 && state() != MONOMORPHIC && state() != POLYMORPHIC) { return false; } if (UseVector()) { if (!nexus()->FindHandlers(&handlers, maps.length())) return false; } else { if (!target()->FindHandlers(&handlers, maps.length())) return false; } number_of_valid_maps++; if (number_of_valid_maps > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_maps == 1) { ConfigureVectorState(name, receiver_map(), code); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!map.is_identical_to(maps.at(handler_to_overwrite))) { maps.Set(handler_to_overwrite, map); } } else { maps.Add(map); handlers.Add(code); } ConfigureVectorState(name, &maps, &handlers); } if (!UseVector()) set_target(*ic); return true; } void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); ConfigureVectorState(name, receiver_map(), handler); } void IC::CopyICToMegamorphicCache(Handle name) { MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); if (!target()->FindHandlers(&handlers, maps.length())) return; for (int i = 0; i < maps.length(); i++) { UpdateMegamorphicCache(*maps.at(i), *name, *handlers.at(i)); } } bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) { if (source_map == NULL) return true; if (target_map == NULL) return false; ElementsKind target_elements_kind = target_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( source_map->elements_kind(), target_elements_kind); Map* transitioned_map = more_general_transition ? source_map->LookupElementsTransitionMap(target_elements_kind) : NULL; return transitioned_map == target_map; } void IC::PatchCache(Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code) { if (!code->is_handler()) return false; if (target()->is_keyed_stub() && state() != PROTOTYPE_FAILURE) return false; Handle map = receiver_map(); MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); int number_of_maps = maps.length(); int deprecated_maps = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_maps; i++) { Handle current_map = maps.at(i); if (current_map->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_maps; } else if (map.is_identical_to(current_map)) { // If the receiver type is already in the polymorphic IC, this indicates // there was a prototoype chain failure. In that case, just overwrite the // handler. handler_to_overwrite = i; } else if (handler_to_overwrite == -1 && IsTransitionOfMonomorphicTarget(*current_map, *map)) { handler_to_overwrite = i; } } int number_of_valid_maps = number_of_maps - deprecated_maps - (handler_to_overwrite != -1); if (number_of_valid_maps >= 4) return false; if (number_of_maps == 0 && state() != MONOMORPHIC && state() != POLYMORPHIC) { return false; } if (UseVector()) { if (!nexus()->FindHandlers(&handlers, maps.length())) return false; } else { if (!target()->FindHandlers(&handlers, maps.length())) return false; } number_of_valid_maps++; if (number_of_valid_maps > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_maps == 1) { ConfigureVectorState(name, receiver_map(), code); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!map.is_identical_to(maps.at(handler_to_overwrite))) { maps.Set(handler_to_overwrite, map); } } else { maps.Add(map); handlers.Add(code); } ConfigureVectorState(name, &maps, &handlers); } if (!UseVector()) set_target(*ic); return true; } void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); ConfigureVectorState(name, receiver_map(), handler); } void IC::CopyICToMegamorphicCache(Handle name) { MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); if (!target()->FindHandlers(&handlers, maps.length())) return; for (int i = 0; i < maps.length(); i++) { UpdateMegamorphicCache(*maps.at(i), *name, *handlers.at(i)); } } bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) { if (source_map == NULL) return true; if (target_map == NULL) return false; ElementsKind target_elements_kind = target_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( source_map->elements_kind(), target_elements_kind); Map* transitioned_map = more_general_transition ? source_map->LookupElementsTransitionMap(target_elements_kind) : NULL; return transitioned_map == target_map; } void IC::PatchCache(Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
ic; if (number_of_valid_maps == 1) { ConfigureVectorState(name, receiver_map(), code); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!map.is_identical_to(maps.at(handler_to_overwrite))) { maps.Set(handler_to_overwrite, map); } } else { maps.Add(map); handlers.Add(code); } ConfigureVectorState(name, &maps, &handlers); } if (!UseVector()) set_target(*ic); return true; } void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); ConfigureVectorState(name, receiver_map(), handler); } void IC::CopyICToMegamorphicCache(Handle name) { MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); if (!target()->FindHandlers(&handlers, maps.length())) return; for (int i = 0; i < maps.length(); i++) { UpdateMegamorphicCache(*maps.at(i), *name, *handlers.at(i)); } } bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) { if (source_map == NULL) return true; if (target_map == NULL) return false; ElementsKind target_elements_kind = target_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( source_map->elements_kind(), target_elements_kind); Map* transitioned_map = more_general_transition ? source_map->LookupElementsTransitionMap(target_elements_kind) : NULL; return transitioned_map == target_map; } void IC::PatchCache(Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler, Handle name) { DCHECK(handler->is_handler()); ConfigureVectorState(name, receiver_map(), handler); } void IC::CopyICToMegamorphicCache(Handle name) { MapHandleList maps; CodeHandleList handlers; TargetMaps(&maps); if (!target()->FindHandlers(&handlers, maps.length())) return; for (int i = 0; i < maps.length(); i++) { UpdateMegamorphicCache(*maps.at(i), *name, *handlers.at(i)); } } bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) { if (source_map == NULL) return true; if (target_map == NULL) return false; ElementsKind target_elements_kind = target_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( source_map->elements_kind(), target_elements_kind); Map* transitioned_map = more_general_transition ? source_map->LookupElementsTransitionMap(target_elements_kind) : NULL; return transitioned_map == target_map; } void IC::PatchCache(Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: UpdateMonomorphicIC(code, name); break; case PROTOTYPE_FAILURE: case MONOMORPHIC: case POLYMORPHIC: if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) { if (UpdatePolymorphicIC(name, code)) break; // For keyed stubs, we can't know whether old handlers were for the // same key. CopyICToMegamorphicCache(name); } if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else { set_target(*megamorphic_stub()); } // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_map(), *name, *code); // Indicate that we've handled this case. if (UseVector()) { vector_set_ = true; } else { target_set_ = true; } break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return LoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
LoadIC::initialize_stub_in_optimized_code( Isolate* isolate, ExtraICState extra_state, State initialization_state) { return LoadICStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedLoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return KeyedLoadICTrampolineStub(isolate, LoadICState(extra_state)).GetCode(); } Handle KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedLoadIC::initialize_stub_in_optimized_code( Isolate* isolate, State initialization_state, ExtraICState extra_state) { if (initialization_state != MEGAMORPHIC) { return KeyedLoadICStub(isolate, LoadICState(extra_state)).GetCode(); } return is_strong(LoadICState::GetLanguageMode(extra_state)) ? isolate->builtins()->KeyedLoadIC_Megamorphic_Strong() : isolate->builtins()->KeyedLoadIC_Megamorphic(); } static Handle KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedStoreICInitializeStubHelper( Isolate* isolate, LanguageMode language_mode, InlineCacheState initialization_state) { switch (initialization_state) { case UNINITIALIZED: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Initialize_Strict() : isolate->builtins()->KeyedStoreIC_Initialize(); case PREMONOMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict() : isolate->builtins()->KeyedStoreIC_PreMonomorphic(); case MEGAMORPHIC: return is_strict(language_mode) ? isolate->builtins()->KeyedStoreIC_Megamorphic_Strict() : isolate->builtins()->KeyedStoreIC_Megamorphic(); default: UNREACHABLE(); } return Handle(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
(); } Handle KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedStoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedStoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { if (initialization_state != MEGAMORPHIC) { VectorKeyedStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return KeyedStoreICInitializeStubHelper(isolate, language_mode, initialization_state); } Handle KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedStoreIC::ChooseMegamorphicStub(Isolate* isolate, ExtraICState extra_state) { LanguageMode mode = StoreICState::GetLanguageMode(extra_state); return KeyedStoreICInitializeStubHelper(isolate, mode, MEGAMORPHIC); } Handle LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
LoadIC::megamorphic_stub() { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::ChooseMegamorphicStub(isolate(), extra_ic_state()); } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } bool IsCompatibleReceiver(LookupIterator* lookup, Handle receiver_map) { DCHECK(lookup->state() == LookupIterator::ACCESSOR); Isolate* isolate = lookup->isolate(); Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (info->getter() != NULL && !ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate, info, receiver_map)) { return false; } } else if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate); Handle holder = lookup->GetHolder(); Handle receiver = lookup->GetReceiver(); if (getter->IsJSFunction() && holder->HasFastProperties()) { Handle function = Handle::cast(getter); if (receiver->IsJSObject() || function->shared()->IsBuiltin() || !is_sloppy(function->shared()->language_mode())) { CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && !call_optimization.IsCompatibleReceiverMap(receiver_map, holder)) { return false; } } } } return true; } void LoadIC::UpdateCaches(LookupIterator* lookup) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("LoadIC", lookup->name()); return; } Handle code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC && !is_strong(language_mode())) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_map()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { if (lookup->state() == LookupIterator::ACCESSOR) { if (!IsCompatibleReceiver(lookup, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } else if (lookup->state() == LookupIterator::INTERCEPTOR) { // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); if (it.state() == LookupIterator::ACCESSOR && !IsCompatibleReceiver(&it, receiver_map())) { TRACE_GENERIC_IC(isolate(), "LoadIC", "incompatible receiver type"); code = slow_stub(); } } if (code.is_null()) code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(Map* map, Name* name, Code* code) { isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
IC::ComputeHandler(LookupIterator* lookup, Handle value) { bool receiver_is_holder = lookup->GetReceiver().is_identical_to(lookup->GetHolder()); CacheHolderFlag flag; Handle stub_holder_map = IC::GetHandlerCacheHolder( receiver_map(), receiver_is_holder, isolate(), &flag); Handle code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code = PropertyHandlerCompiler::Find( lookup->name(), stub_holder_map, kind(), flag, lookup->is_dictionary_holder() ? Code::NORMAL : Code::FAST); // Use the cached value if it exists, and if it is different from the // handler that just missed. if (!code.is_null()) { if (!maybe_handler_.is_null() && !maybe_handler_.ToHandleChecked().is_identical_to(code)) { return code; } if (maybe_handler_.is_null()) { // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs. // In MEGAMORPHIC case, check if the handler in the megamorphic stub // cache (which just missed) is different from the cached handler. if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) { Map* map = Handle::cast(lookup->GetReceiver())->map(); Code* megamorphic_cached_code = isolate()->stub_cache()->Get(*lookup->name(), map, code->flags()); if (megamorphic_cached_code != *code) return code; } else { return code; } } } code = CompileHandler(lookup, value, flag); DCHECK(code->is_handler()); // TODO(mvstanton): we'd only like to cache code on the map when it's custom // code compiled for this map, otherwise it's already cached in the global // code // cache. We are also guarding against installing code with flags that don't // match the desired CacheHolderFlag computed above, which would lead to // invalid lookups later. if (code->type() != Code::NORMAL && Code::ExtractCacheHolderFromFlags(code->flags()) == flag) { Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); } return code; } Handle LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
LoadIC::CompileHandler(LookupIterator* lookup, Handle unused, CacheHolderFlag cache_holder) { Handle receiver = lookup->GetReceiver(); if (receiver->IsString() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset); return SimpleFieldLoad(index); } if (receiver->IsStringWrapper() && Name::Equals(isolate()->factory()->length_string(), lookup->name())) { StringLengthStub string_length_stub(isolate()); return string_length_stub.GetCode(); } // Use specialized code for getting prototype of functions. if (receiver->IsJSFunction() && Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) && receiver->IsConstructor() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle map = receiver_map(); Handle holder = lookup->GetHolder(); bool receiver_is_holder = receiver.is_identical_to(holder); switch (lookup->state()) { case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined()); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); // Perform a lookup behind the interceptor. Copy the LookupIterator since // the original iterator will be used to fetch the value. LookupIterator it = *lookup; it.Next(); LookupForRead(&it); return compiler.CompileLoadInterceptor(&it); } case LookupIterator::ACCESSOR: { // Use simple field loads for some well-known callback properties. // The method will only return true for absolute truths based on the // receiver maps. int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); return SimpleFieldLoad(index); } if (Accessors::IsJSArrayBufferViewFieldAccessor(map, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, *map); ArrayBufferViewLoadFieldStub stub(isolate(), index); return stub.GetCode(); } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, map)) { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadCallback(lookup->name(), info); } if (accessors->IsAccessorPair()) { Handle getter(Handle::cast(accessors)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (!holder->HasFastProperties()) break; // When debugging we need to go the slow path to flood the accessor. if (GetSharedFunctionInfo()->HasDebugInfo()) break; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->shared()->IsBuiltin() && is_sloppy(function->shared()->language_mode())) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); if (call_optimization.is_simple_api_call()) { if (call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback( lookup->name(), call_optimization, lookup->GetAccessorIndex()); } else { // This case should be already handled in LoadIC::UpdateCaches. UNREACHABLE(); } } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileLoadViaGetter( lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsJSGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); Handle cell = lookup->GetPropertyCell(); Handle code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code = compiler.CompileLoadGlobal( cell, lookup->name(), lookup->IsConfigurable()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. CacheHolderFlag flag; Handle stub_holder_map = GetHandlerCacheHolder(map, receiver_is_holder, isolate(), &flag); Map::UpdateCodeCache(stub_holder_map, lookup->name(), code); return code; } // There is only one shared stub for loading normalized // properties. It does not traverse the prototype chain, so the // property must be found in the object for the stub to be // applicable. if (!receiver_is_holder) break; return is_strong(language_mode()) ? isolate()->builtins()->LoadIC_Normal_Strong() : isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), map, holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } case LookupIterator::INTEGER_INDEXED_EXOTIC: return slow_stub(); case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); } return slow_stub(); } static Handle TryConvertKey(Handle key, Isolate* isolate) { // This helper implements a few common fast cases for converting // non-smi keys of keyed loads/stores to a smi or a string. if (key->IsHeapNumber()) { double value = Handle::cast(key)->value(); if (std::isnan(value)) { key = isolate->factory()->nan_string(); } else { int int_value = FastD2I(value); if (value == int_value && Smi::IsValid(int_value)) { key = handle(Smi::FromInt(int_value), isolate); } } } else if (key->IsUndefined()) { key = isolate->factory()->undefined_string(); } return key; } Handle KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedLoadIC::LoadElementStub(Handle receiver) { Handle null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
null_handle; Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } // The first time a receiver is seen that is a transitioned version of the // previous monomorphic receiver type, assume the new ElementsKind is the // monomorphic type. This benefits global arrays that only transition // once, and all call sites accessing them are faster if they remain // monomorphic. If this optimistic assumption is not true, the IC will // miss again and it will become polymorphic and support both the // untransitioned and transitioned maps. if (state() == MONOMORPHIC && !receiver->IsString() && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), Handle::cast(receiver)->GetElementsKind())) { Handle handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler = PropertyICCompiler::ComputeKeyedLoadMonomorphicHandler( receiver_map, extra_ic_state()); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } DCHECK(state() != GENERIC); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the generic stub. TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the generic // version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "max polymorph exceeded"); return megamorphic_stub(); } CodeHandleList handlers(target_receiver_maps.length()); ElementHandlerCompiler compiler(isolate()); compiler.CompileElementHandlers(&target_receiver_maps, &handlers, language_mode()); ConfigureVectorState(Handle::null(), &target_receiver_maps, &handlers); return null_handle; } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } Handle load_handle; Handle stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
stub = megamorphic_stub(); // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString() || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION(isolate(), load_handle, LoadIC::Load(object, Handle::cast(key)), Object); } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { if (object->IsJSObject() || (object->IsString() && key->IsNumber())) { Handle receiver = Handle::cast(object); if (object->IsString() || key->IsSmi()) stub = LoadElementStub(receiver); } } DCHECK(UseVector()); if (!is_vector_set() || stub.is_null()) { Code* generic = *megamorphic_stub(); if (!stub.is_null() && *stub == generic) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } TRACE_IC("LoadIC", key); } if (!load_handle.is_null()) return load_handle; Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key, language_mode()), Object); return result; } bool StoreIC::LookupForWrite(LookupIterator* it, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Disable ICs for non-JSObjects for now. Handle receiver = it->GetReceiver(); if (!receiver->IsJSObject()) return false; DCHECK(!Handle::cast(receiver)->map()->is_deprecated()); for (; it->IsFound(); it->Next()) { switch (it->state()) { case LookupIterator::NOT_FOUND: case LookupIterator::TRANSITION: UNREACHABLE(); case LookupIterator::JSPROXY: return false; case LookupIterator::INTERCEPTOR: { Handle holder = it->GetHolder(); InterceptorInfo* info = holder->GetNamedInterceptor(); if (it->HolderIsReceiverOrHiddenPrototype()) { if (!info->setter()->IsUndefined()) return true; } else if (!info->getter()->IsUndefined() || !info->query()->IsUndefined()) { return false; } break; } case LookupIterator::ACCESS_CHECK: if (it->GetHolder()->IsAccessCheckNeeded()) return false; break; case LookupIterator::ACCESSOR: return !it->IsReadOnly(); case LookupIterator::INTEGER_INDEXED_EXOTIC: return false; case LookupIterator::DATA: { if (it->IsReadOnly()) return false; Handle holder = it->GetHolder(); if (receiver.is_identical_to(holder)) { it->PrepareForDataProperty(value); // The previous receiver map might just have been deprecated, // so reload it. update_receiver_map(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } if (it->HolderIsReceiverOrHiddenPrototype()) return false; it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } } } it->PrepareTransitionToDataProperty(value, NONE, store_mode); return it->IsCacheableTransition(); } MaybeHandle StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { // Check if the name is trivially convertible to an index and set the element. uint32_t index; if (kind() == Code::KEYED_STORE_IC && name->AsArrayIndex(&index)) { // Rewrite to the generic keyed store stub. if (FLAG_use_ic) { if (UseVector()) { ConfigureVectorState(MEGAMORPHIC); } else if (!AddressIsDeoptimizedCode()) { set_target(*megamorphic_stub()); } TRACE_IC("StoreIC", name); TRACE_GENERIC_IC(isolate(), "StoreIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetElement(isolate(), object, index, value, language_mode()), Object); return result; } if (object->IsJSGlobalObject() && name->IsString()) { // Look up in script context table. Handle str_name = Handle::cast(name); Handle global = Handle::cast(object); Handle script_contexts( global->native_context()->script_context_table()); ScriptContextTable::LookupResult lookup_result; if (ScriptContextTable::Lookup(script_contexts, str_name, &lookup_result)) { Handle script_context = ScriptContextTable::GetContext( script_contexts, lookup_result.context_index); if (lookup_result.mode == CONST) { return TypeError(MessageTemplate::kConstAssign, object, name); } Handle previous_value = FixedArray::get(script_context, lookup_result.slot_index); if (*previous_value == *isolate()->factory()->the_hole_value()) { // Do not install stubs and stay pre-monomorphic for // uninitialized accesses. return ReferenceError(name); } if (FLAG_use_ic && StoreScriptContextFieldStub::Accepted(&lookup_result)) { StoreScriptContextFieldStub stub(isolate(), &lookup_result); PatchCache(name, stub.GetCode()); } script_context->set(lookup_result.slot_index, *value); return value; } } // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode()), Object); return result; } // If the object is undefined or null it's illegal to try to set any // properties on it; throw a TypeError in that case. if (object->IsUndefined() || object->IsNull()) { return TypeError(MessageTemplate::kNonObjectPropertyStore, object, name); } // Observed objects are always modified through the runtime. if (object->IsHeapObject() && Handle::cast(object)->map()->is_observed()) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(object, name, value, language_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); MAYBE_RETURN_NULL( Object::SetProperty(&it, value, language_mode(), store_mode)); return value; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
CallIC::initialize_stub(Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICTrampolineStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code = stub.GetCode(); return code; } Handle CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
CallIC::initialize_stub_in_optimized_code( Isolate* isolate, int argc, ConvertReceiverMode mode) { CallICStub stub(isolate, CallICState(argc, mode)); Handle code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code = stub.GetCode(); return code; } static Handle StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreICInitializeStubHelper( Isolate* isolate, ExtraICState extra_state, InlineCacheState initialization_state) { Handle ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
ic = PropertyICCompiler::ComputeStore( isolate, initialization_state, extra_state); return ic; } Handle StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreIC::initialize_stub(Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); VectorStoreICTrampolineStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } Handle StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreIC::initialize_stub_in_optimized_code( Isolate* isolate, LanguageMode language_mode, State initialization_state) { DCHECK(initialization_state == UNINITIALIZED || initialization_state == PREMONOMORPHIC || initialization_state == MEGAMORPHIC); if (initialization_state != MEGAMORPHIC) { VectorStoreICStub stub(isolate, StoreICState(language_mode)); return stub.GetCode(); } return StoreICInitializeStubHelper( isolate, ComputeExtraICState(language_mode), initialization_state); } Handle StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreIC::megamorphic_stub() { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (is_strict(language_mode())) { return isolate()->builtins()->KeyedStoreIC_Megamorphic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Megamorphic(); } } } Handle StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreIC::slow_stub() const { if (kind() == Code::STORE_IC) { return isolate()->builtins()->StoreIC_Slow(); } else { DCHECK(kind() == Code::KEYED_STORE_IC); return isolate()->builtins()->KeyedStoreIC_Slow(); } } Handle StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreIC::pre_monomorphic_stub(Isolate* isolate, LanguageMode language_mode) { ExtraICState state = ComputeExtraICState(language_mode); return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state); } void StoreIC::UpdateCaches(LookupIterator* lookup, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (state() == UNINITIALIZED) { // This is the first time we execute this inline cache. Set the target to // the pre monomorphic stub to delay setting the monomorphic state. ConfigureVectorState(PREMONOMORPHIC); TRACE_IC("StoreIC", lookup->name()); return; } bool use_ic = LookupForWrite(lookup, value, store_mode); if (!use_ic) { TRACE_GENERIC_IC(isolate(), "StoreIC", "LookupForWrite said 'false'"); } Handle code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } static Handle PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
PropertyCellStoreHandler( Isolate* isolate, Handle receiver, Handle holder, Handle name, Handle cell, PropertyCellType type) { auto constant_type = Nothing(); if (type == PropertyCellType::kConstantType) { constant_type = Just(cell->GetConstantType()); } StoreGlobalStub stub(isolate, type, constant_type, receiver->IsJSGlobalProxy()); auto code = stub.GetCodeCopyFromTemplate(holder, cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } Handle StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
StoreIC::CompileHandler(LookupIterator* lookup, Handle value, CacheHolderFlag cache_holder) { DCHECK_NE(LookupIterator::JSPROXY, lookup->state()); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(lookup->GetReceiver()); Handle holder = lookup->GetHolder(); DCHECK(!receiver->IsAccessCheckNeeded() || isolate()->IsInternallyUsedPropertyName(lookup->name())); switch (lookup->state()) { case LookupIterator::TRANSITION: { auto store_target = lookup->GetStoreTarget(); if (store_target->IsJSGlobalObject()) { // TODO(dcarney): this currently just deopts. Use the transition cell. auto cell = isolate()->factory()->NewPropertyCell(); cell->set_value(*value); auto code = PropertyCellStoreHandler( isolate(), store_target, Handle::cast(store_target), lookup->name(), cell, PropertyCellType::kConstant); cell->set_value(isolate()->heap()->the_hole_value()); return code; } Handle transition = lookup->transition_map(); // Currently not handled by CompileStoreTransition. if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "transition from slow"); break; } DCHECK(lookup->IsCacheableTransition()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreInterceptor(lookup->name()); } case LookupIterator::ACCESSOR: { if (!holder->HasFastProperties()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "accessor on slow map"); break; } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->setter()) == 0) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter == 0"); break; } if (AccessorInfo::cast(*accessors)->is_special_data_property() && !lookup->HolderIsReceiverOrHiddenPrototype()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "special data property in prototype chain"); break; } if (!ExecutableAccessorInfo::IsCompatibleReceiverMap(isolate(), info, receiver_map())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreCallback(receiver, lookup->name(), info); } else if (accessors->IsAccessorPair()) { Handle setter(Handle::cast(accessors)->setter(), isolate()); if (!setter->IsJSFunction()) { TRACE_GENERIC_IC(isolate(), "StoreIC", "setter not a function"); break; } Handle function = Handle::cast(setter); CallOptimization call_optimization(function); NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization, lookup->GetAccessorIndex()); } int expected_arguments = function->shared()->internal_formal_parameter_count(); return compiler.CompileStoreViaSetter(receiver, lookup->name(), lookup->GetAccessorIndex(), expected_arguments); } break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsJSGlobalObject()) { DCHECK(holder.is_identical_to(receiver) || receiver->map()->prototype() == *holder); auto cell = lookup->GetPropertyCell(); auto updated_type = PropertyCell::UpdatedType( cell, value, lookup->property_details()); auto code = PropertyCellStoreHandler( isolate(), receiver, Handle::cast(holder), lookup->name(), cell, updated_type); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == DATA) { bool use_stub = true; if (lookup->representation().IsHeapObject()) { // Only use a generic stub if no types need to be tracked. Handle field_type = lookup->GetFieldType(); HeapType::Iterator it = field_type->Classes(); use_stub = it.Done(); } if (use_stub) { StoreFieldStub stub(isolate(), lookup->GetFieldIndex(), lookup->representation()); return stub.GetCode(); } NamedStoreHandlerCompiler compiler(isolate(), receiver_map(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == DATA_CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::INTEGER_INDEXED_EXOTIC: case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
KeyedStoreIC::StoreElementStub(Handle receiver_map, KeyedAccessStoreMode store_mode) { Handle null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
null_handle; // Don't handle megamorphic property accesses for INTERCEPTORS or // ACCESSOR_CONSTANT // via megamorphic stubs, since they don't have a map in their relocation info // and so the stubs can't be harvested for the object needed for a map check. if (target()->type() != Code::NORMAL) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-NORMAL target type"); return megamorphic_stub(); } MapHandleList target_receiver_maps; TargetMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { Handle monomorphic_map = ComputeTransitionedMap(receiver_map, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( monomorphic_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), monomorphic_map, handler); return null_handle; } // There are several special cases where an IC that is MONOMORPHIC can still // transition to a different GetNonTransitioningStoreMode IC that handles a // superset of the original IC. Handle those here if the receiver map hasn't // changed or it has transitioned to a more general kind. KeyedAccessStoreMode old_store_mode = GetKeyedAccessStoreMode(); Handle previous_receiver_map = target_receiver_maps.at(0); if (state() == MONOMORPHIC) { Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); } if ((receiver_map.is_identical_to(previous_receiver_map) && IsTransitionStoreMode(store_mode)) || IsTransitionOfMonomorphicTarget(*previous_receiver_map, *transitioned_receiver_map)) { // If the "old" and "new" maps are in the same elements map family, or // if they at least come from the same origin for a transitioning store, // stay MONOMORPHIC and use the map for the most generic ElementsKind. store_mode = GetNonTransitioningStoreMode(store_mode); Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( transitioned_receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), transitioned_receiver_map, handler); return null_handle; } else if (receiver_map.is_identical_to(previous_receiver_map) && old_store_mode == STANDARD_STORE && (store_mode == STORE_AND_GROW_NO_TRANSITION || store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS || store_mode == STORE_NO_TRANSITION_HANDLE_COW)) { // A "normal" IC that handles stores can switch to a version that can // grow at the end of the array, handle OOB accesses or copy COW arrays // and still stay MONOMORPHIC. Handle handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
handler = PropertyICCompiler::ComputeKeyedStoreMonomorphicHandler( receiver_map, language_mode(), store_mode); ConfigureVectorState(Handle::null(), receiver_map, handler); return null_handle; } } DCHECK(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver_map, store_mode); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, transitioned_receiver_map); } if (!map_added) { // If the miss wasn't due to an unseen map, a polymorphic stub // won't help, use the megamorphic stub which can handle everything. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return megamorphic_stub(); } // If the maximum number of receiver maps has been exceeded, use the // megamorphic version of the IC. if (target_receiver_maps.length() > kMaxKeyedPolymorphism) { return megamorphic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // megamorphic stub must be used. store_mode = GetNonTransitioningStoreMode(store_mode); if (old_store_mode != STANDARD_STORE) { if (store_mode == STANDARD_STORE) { store_mode = old_store_mode; } else if (store_mode != old_store_mode) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "store mode mismatch"); return megamorphic_stub(); } } // If the store mode isn't the standard mode, make sure that all polymorphic // receivers are either external arrays, or all "normal" arrays. Otherwise, // use the megamorphic stub. if (store_mode != STANDARD_STORE) { int external_arrays = 0; for (int i = 0; i < target_receiver_maps.length(); ++i) { if (target_receiver_maps[i]->has_fixed_typed_array_elements()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unsupported combination of external and normal arrays"); return megamorphic_stub(); } } MapHandleList transitioned_maps(target_receiver_maps.length()); CodeHandleList handlers(target_receiver_maps.length()); PropertyICCompiler::ComputeKeyedStorePolymorphicHandlers( &target_receiver_maps, &transitioned_maps, &handlers, store_mode, language_mode()); ConfigureVectorState(&target_receiver_maps, &transitioned_maps, &handlers); return null_handle; } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_TO_OBJECT: case STORE_AND_GROW_TRANSITION_TO_OBJECT: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_TRANSITION_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_TO_DOUBLE: { ElementsKind kind = IsFastHoleyElementsKind(map->elements_kind()) ? FAST_HOLEY_DOUBLE_ELEMENTS : FAST_DOUBLE_ELEMENTS; return Map::TransitionElementsTo(map, kind); } case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_fixed_typed_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return map; } UNREACHABLE(); return MaybeHandle().ToHandleChecked(); } bool IsOutOfBoundsAccess(Handle receiver, uint32_t index) { uint32_t length = 0; if (receiver->IsJSArray()) { JSArray::cast(*receiver)->length()->ToArrayLength(&length); } else { length = static_cast(receiver->elements()->length()); } return index >= length; } static KeyedAccessStoreMode GetStoreMode(Handle receiver, uint32_t index, Handle value) { bool oob_access = IsOutOfBoundsAccess(receiver, index); // Don't consider this a growing store if the store would send the receiver to // dictionary mode. bool allow_growth = receiver->IsJSArray() && oob_access && !receiver->WouldConvertToSlowElements(index); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_TO_OBJECT; } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_fixed_typed_array_elements() && oob_access) { return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS; } Heap* heap = receiver->GetHeap(); if (receiver->elements()->map() == heap->fixed_cow_array_map()) { return STORE_NO_TRANSITION_HANDLE_COW; } else { return STANDARD_STORE; } } } MaybeHandle KeyedStoreIC::Store(Handle object, Handle key, Handle value) { // TODO(verwaest): Let SetProperty do the migration, since storing a property // might deprecate the current map again, if value does not fit. if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); return result; } // Check for non-string values that can be converted into an // internalized string directly or is representable as a smi. key = TryConvertKey(key, isolate()); Handle store_handle; Handle stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle
stub = megamorphic_stub(); uint32_t index; if ((key->IsInternalizedString() && !String::cast(*key)->AsArrayIndex(&index)) || key->IsSymbol()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); if (!is_vector_set()) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); } return store_handle; } bool use_ic = FLAG_use_ic && !object->IsStringWrapper() && !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() && !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed()); if (use_ic && !object->IsSmi()) { // Don't use ICs for maps of the objects in Array's prototype chain. We // expect to be able to trap element sets to objects with those maps in // the runtime to enable optimization of element hole access. Handle heap_object = Handle::cast(object); if (heap_object->map()->IsMapInArrayPrototypeChain()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "map in array prototype"); use_ic = false; } } Handle old_receiver_map; bool sloppy_arguments_elements = false; bool key_is_valid_index = false; KeyedAccessStoreMode store_mode = STANDARD_STORE; if (use_ic && object->IsJSObject()) { Handle receiver = Handle::cast(object); old_receiver_map = handle(receiver->map(), isolate()); sloppy_arguments_elements = !is_sloppy(language_mode()) && receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map(); if (!sloppy_arguments_elements) { key_is_valid_index = key->IsSmi() && Smi::cast(*key)->value() >= 0; if (key_is_valid_index) { uint32_t index = static_cast(Smi::cast(*key)->value()); store_mode = GetStoreMode(receiver, index, value); } } } DCHECK(store_handle.is_null()); ASSIGN_RETURN_ON_EXCEPTION(isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, language_mode()), Object); if (use_ic) { if (!old_receiver_map.is_null()) { if (sloppy_arguments_elements) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } else if (key_is_valid_index) { // We should go generic if receiver isn't a dictionary, but our // prototype chain does have dictionary elements. This ensures that // other non-dictionary receivers in the polymorphic case benefit // from fast path keyed stores. if (!old_receiver_map->DictionaryElementsInPrototypeChainOnly()) { stub = StoreElementStub(old_receiver_map, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary or proxy prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (!is_vector_set() || stub.is_null()) { Code* megamorphic = *megamorphic_stub(); if (!stub.is_null() && (*stub == megamorphic || *stub == *slow_stub())) { ConfigureVectorState(MEGAMORPHIC); TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", *stub == megamorphic ? "set generic" : "slow stub"); } } TRACE_IC("StoreIC", key); return store_handle; } void CallIC::HandleMiss(Handle function) { Handle name = isolate()->factory()->empty_string(); CallICNexus* nexus = casted_nexus(); Object* feedback = nexus->GetFeedback(); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsWeakCell() || !function->IsJSFunction() || feedback->IsAllocationSite()) { // We are going generic. nexus->ConfigureMegamorphic(); } else { DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate())); Handle js_function = Handle::cast(function); Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(js_function)) { // Alter the slot. nexus->ConfigureMonomorphicArray(); } else if (js_function->context()->native_context() != *isolate()->native_context()) { // Don't collect cross-native context feedback for the CallIC. // TODO(bmeurer): We should collect the SharedFunctionInfo as // feedback in this case instead. nexus->ConfigureMegamorphic(); } else { nexus->ConfigureMonomorphic(js_function); } } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } OnTypeFeedbackChanged(isolate(), get_host()); TRACE_IC("CallIC", name); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 3); Handle function = args.at(0); Handle vector = args.at(1); Handle slot = args.at(2); FeedbackVectorSlot vector_slot = vector->ToSlot(slot->value()); CallICNexus nexus(vector, vector_slot); CallIC ic(isolate, &nexus); ic.HandleMiss(function); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(Runtime_LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); Handle