stub) { if (FLAG_collect_maps && FLAG_weak_embedded_maps_in_ic && stub->CanBeWeakStub()) { DCHECK(!stub->is_weak_stub()); MapHandleList maps; stub->FindAllMaps(&maps); if (maps.length() == 1 && stub->IsWeakObjectInIC(*maps.at(0))) { Map::AddDependentIC(maps.at(0), stub); stub->mark_as_weak_stub(); if (FLAG_enable_ool_constant_pool) { stub->constant_pool()->set_weak_object_state( ConstantPoolArray::WEAK_OBJECTS_IN_IC); } } } } void IC::InvalidateMaps(Code* stub) { DCHECK(stub->is_weak_stub()); stub->mark_as_invalidated_weak_stub(); Isolate* isolate = stub->GetIsolate(); Heap* heap = isolate->heap(); Object* undefined = heap->undefined_value(); int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(stub, mode_mask); !it.done(); it.next()) { RelocInfo::Mode mode = it.rinfo()->rmode(); if (mode == RelocInfo::EMBEDDED_OBJECT && it.rinfo()->target_object()->IsMap()) { it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER); } } CpuFeatures::FlushICache(stub->instruction_start(), stub->instruction_size()); } void IC::Clear(Isolate* isolate, Address address, ConstantPoolArray* constant_pool) { Code* target = GetTargetAtAddress(address, constant_pool); // Don't clear debug break inline cache as it will remove the break point. if (target->is_debug_stub()) return; switch (target->kind()) { case Code::LOAD_IC: return LoadIC::Clear(isolate, address, target, constant_pool); case Code::KEYED_LOAD_IC: return KeyedLoadIC::Clear(isolate, address, target, constant_pool); case Code::STORE_IC: return StoreIC::Clear(isolate, address, target, constant_pool); case Code::KEYED_STORE_IC: return KeyedStoreIC::Clear(isolate, address, target, constant_pool); case Code::CALL_IC: return CallIC::Clear(isolate, address, target, constant_pool); case Code::COMPARE_IC: return CompareIC::Clear(isolate, address, target, constant_pool); case Code::COMPARE_NIL_IC: return CompareNilIC::Clear(address, target, constant_pool); case Code::BINARY_OP_IC: case Code::TO_BOOLEAN_IC: // Clearing these is tricky and does not // make any performance difference. return; default: UNREACHABLE(); } } void KeyedLoadIC::Clear(Isolate* isolate, Address address, Code* target, ConstantPoolArray* constant_pool) { if (IsCleared(target)) return; // Make sure to also clear the map used in inline fast cases. If we // do not clear these maps, cached code can keep objects alive // through the embedded maps. SetTargetAtAddress(address, *pre_monomorphic_stub(isolate), constant_pool); } void CallIC::Clear(Isolate* isolate, Address address, Code* target, ConstantPoolArray* constant_pool) { // Currently, CallIC doesn't have state changes. } void LoadIC::Clear(Isolate* isolate, Address address, Code* target, ConstantPoolArray* constant_pool) { if (IsCleared(target)) return; Code* code = PropertyICCompiler::FindPreMonomorphic(isolate, Code::LOAD_IC, target->extra_ic_state()); SetTargetAtAddress(address, code, constant_pool); } void StoreIC::Clear(Isolate* isolate, Address address, Code* target, ConstantPoolArray* constant_pool) { if (IsCleared(target)) return; Code* code = PropertyICCompiler::FindPreMonomorphic(isolate, Code::STORE_IC, target->extra_ic_state()); SetTargetAtAddress(address, code, constant_pool); } void KeyedStoreIC::Clear(Isolate* isolate, Address address, Code* target, ConstantPoolArray* constant_pool) { if (IsCleared(target)) return; SetTargetAtAddress( address, *pre_monomorphic_stub( isolate, StoreIC::GetStrictMode(target->extra_ic_state())), constant_pool); } void CompareIC::Clear(Isolate* isolate, Address address, Code* target, ConstantPoolArray* 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_OBJECT) return; SetTargetAtAddress(address, GetRawUninitialized(isolate, stub.op()), constant_pool); PatchInlinedSmiCode(address, DISABLE_INLINED_SMI_CHECK); } // static Handle KeyedLoadIC::generic_stub(Isolate* isolate) { if (FLAG_compiled_keyed_generic_loads) { return KeyedLoadGenericStub(isolate).GetCode(); } else { return isolate->builtins()->KeyedLoadIC_Generic(); } } 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; } 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("non_object_property_load", 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) { set_target(*KeyedLoadIC::generic_stub(isolate())); TRACE_IC("LoadIC", name); TRACE_GENERIC_IC(isolate(), "LoadIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetElementOrCharAt(isolate(), object, index), Object); return result; } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupIterator it(object, name); LookupForRead(&it); if (it.IsFound() || !IsUndeclaredGlobal(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), Object); if (it.IsFound()) { return result; } else if (!IsUndeclaredGlobal(object)) { LOG(isolate(), SuspectReadEvent(*name, *object)); return result; } } return ReferenceError("not_defined", 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 type = receiver_type(); TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); int number_of_types = types.length(); int deprecated_types = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); if (current_type->IsClass() && current_type->AsClass()->Map()->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_types; } else if (type->NowIs(current_type)) { // 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 && current_type->IsClass() && type->IsClass() && IsTransitionOfMonomorphicTarget(*current_type->AsClass()->Map(), *type->AsClass()->Map())) { handler_to_overwrite = i; } } int number_of_valid_types = number_of_types - deprecated_types - (handler_to_overwrite != -1); if (number_of_valid_types >= 4) return false; if (number_of_types == 0) return false; if (!target()->FindHandlers(&handlers, types.length())) return false; number_of_valid_types++; if (number_of_valid_types > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_types == 1) { ic = PropertyICCompiler::ComputeMonomorphic(kind(), name, type, code, extra_ic_state()); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!type->NowIs(types.at(handler_to_overwrite))) { types.Set(handler_to_overwrite, type); } } else { types.Add(type); handlers.Add(code); } ic = PropertyICCompiler::ComputePolymorphic(kind(), &types, &handlers, number_of_valid_types, name, extra_ic_state()); } set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { return object->IsJSGlobalObject() ? HeapType::Constant(Handle::cast(object), isolate) : HeapType::NowOf(object, isolate); } Handle IC::TypeToMap(HeapType* type, Isolate* isolate) { if (type->Is(HeapType::Number())) return isolate->factory()->heap_number_map(); if (type->Is(HeapType::Boolean())) return isolate->factory()->boolean_map(); if (type->IsConstant()) { return handle( Handle::cast(type->AsConstant()->Value())->map()); } DCHECK(type->IsClass()); return type->AsClass()->Map(); } template typename T::TypeHandle IC::MapToType(Handle map, typename T::Region* region) { if (map->instance_type() == HEAP_NUMBER_TYPE) { return T::Number(region); } else if (map->instance_type() == ODDBALL_TYPE) { // The only oddballs that can be recorded in ICs are booleans. return T::Boolean(region); } else { return T::Class(map, region); } } template Type* IC::MapToType(Handle map, Zone* zone); template Handle IC::MapToType(Handle map, Isolate* region); void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); Handle ic = PropertyICCompiler::ComputeMonomorphic( kind(), name, receiver_type(), handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); if (!target()->FindHandlers(&handlers, types.length())) return; for (int i = 0; i < types.length(); i++) { UpdateMegamorphicCache(*types.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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
KeyedLoadIC::generic_stub(Isolate* isolate) { if (FLAG_compiled_keyed_generic_loads) { return KeyedLoadGenericStub(isolate).GetCode(); } else { return isolate->builtins()->KeyedLoadIC_Generic(); } } 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; } 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("non_object_property_load", 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) { set_target(*KeyedLoadIC::generic_stub(isolate())); TRACE_IC("LoadIC", name); TRACE_GENERIC_IC(isolate(), "LoadIC", "name as array index"); } Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetElementOrCharAt(isolate(), object, index), Object); return result; } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupIterator it(object, name); LookupForRead(&it); if (it.IsFound() || !IsUndeclaredGlobal(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), Object); if (it.IsFound()) { return result; } else if (!IsUndeclaredGlobal(object)) { LOG(isolate(), SuspectReadEvent(*name, *object)); return result; } } return ReferenceError("not_defined", 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 type = receiver_type(); TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); int number_of_types = types.length(); int deprecated_types = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); if (current_type->IsClass() && current_type->AsClass()->Map()->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_types; } else if (type->NowIs(current_type)) { // 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 && current_type->IsClass() && type->IsClass() && IsTransitionOfMonomorphicTarget(*current_type->AsClass()->Map(), *type->AsClass()->Map())) { handler_to_overwrite = i; } } int number_of_valid_types = number_of_types - deprecated_types - (handler_to_overwrite != -1); if (number_of_valid_types >= 4) return false; if (number_of_types == 0) return false; if (!target()->FindHandlers(&handlers, types.length())) return false; number_of_valid_types++; if (number_of_valid_types > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_types == 1) { ic = PropertyICCompiler::ComputeMonomorphic(kind(), name, type, code, extra_ic_state()); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!type->NowIs(types.at(handler_to_overwrite))) { types.Set(handler_to_overwrite, type); } } else { types.Add(type); handlers.Add(code); } ic = PropertyICCompiler::ComputePolymorphic(kind(), &types, &handlers, number_of_valid_types, name, extra_ic_state()); } set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { return object->IsJSGlobalObject() ? HeapType::Constant(Handle::cast(object), isolate) : HeapType::NowOf(object, isolate); } Handle IC::TypeToMap(HeapType* type, Isolate* isolate) { if (type->Is(HeapType::Number())) return isolate->factory()->heap_number_map(); if (type->Is(HeapType::Boolean())) return isolate->factory()->boolean_map(); if (type->IsConstant()) { return handle( Handle::cast(type->AsConstant()->Value())->map()); } DCHECK(type->IsClass()); return type->AsClass()->Map(); } template typename T::TypeHandle IC::MapToType(Handle map, typename T::Region* region) { if (map->instance_type() == HEAP_NUMBER_TYPE) { return T::Number(region); } else if (map->instance_type() == ODDBALL_TYPE) { // The only oddballs that can be recorded in ICs are booleans. return T::Boolean(region); } else { return T::Class(map, region); } } template Type* IC::MapToType(Handle map, Zone* zone); template Handle IC::MapToType(Handle map, Isolate* region); void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); Handle ic = PropertyICCompiler::ComputeMonomorphic( kind(), name, receiver_type(), handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); if (!target()->FindHandlers(&handlers, types.length())) return; for (int i = 0; i < types.length(); i++) { UpdateMegamorphicCache(*types.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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
code) { if (!code->is_handler()) return false; if (target()->is_keyed_stub() && state() != PROTOTYPE_FAILURE) return false; Handle type = receiver_type(); TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); int number_of_types = types.length(); int deprecated_types = 0; int handler_to_overwrite = -1; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); if (current_type->IsClass() && current_type->AsClass()->Map()->is_deprecated()) { // Filter out deprecated maps to ensure their instances get migrated. ++deprecated_types; } else if (type->NowIs(current_type)) { // 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 && current_type->IsClass() && type->IsClass() && IsTransitionOfMonomorphicTarget(*current_type->AsClass()->Map(), *type->AsClass()->Map())) { handler_to_overwrite = i; } } int number_of_valid_types = number_of_types - deprecated_types - (handler_to_overwrite != -1); if (number_of_valid_types >= 4) return false; if (number_of_types == 0) return false; if (!target()->FindHandlers(&handlers, types.length())) return false; number_of_valid_types++; if (number_of_valid_types > 1 && target()->is_keyed_stub()) return false; Handle ic; if (number_of_valid_types == 1) { ic = PropertyICCompiler::ComputeMonomorphic(kind(), name, type, code, extra_ic_state()); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!type->NowIs(types.at(handler_to_overwrite))) { types.Set(handler_to_overwrite, type); } } else { types.Add(type); handlers.Add(code); } ic = PropertyICCompiler::ComputePolymorphic(kind(), &types, &handlers, number_of_valid_types, name, extra_ic_state()); } set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { return object->IsJSGlobalObject() ? HeapType::Constant(Handle::cast(object), isolate) : HeapType::NowOf(object, isolate); } Handle IC::TypeToMap(HeapType* type, Isolate* isolate) { if (type->Is(HeapType::Number())) return isolate->factory()->heap_number_map(); if (type->Is(HeapType::Boolean())) return isolate->factory()->boolean_map(); if (type->IsConstant()) { return handle( Handle::cast(type->AsConstant()->Value())->map()); } DCHECK(type->IsClass()); return type->AsClass()->Map(); } template typename T::TypeHandle IC::MapToType(Handle map, typename T::Region* region) { if (map->instance_type() == HEAP_NUMBER_TYPE) { return T::Number(region); } else if (map->instance_type() == ODDBALL_TYPE) { // The only oddballs that can be recorded in ICs are booleans. return T::Boolean(region); } else { return T::Class(map, region); } } template Type* IC::MapToType(Handle map, Zone* zone); template Handle IC::MapToType(Handle map, Isolate* region); void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); Handle ic = PropertyICCompiler::ComputeMonomorphic( kind(), name, receiver_type(), handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); if (!target()->FindHandlers(&handlers, types.length())) return; for (int i = 0; i < types.length(); i++) { UpdateMegamorphicCache(*types.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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
ic; if (number_of_valid_types == 1) { ic = PropertyICCompiler::ComputeMonomorphic(kind(), name, type, code, extra_ic_state()); } else { if (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); if (!type->NowIs(types.at(handler_to_overwrite))) { types.Set(handler_to_overwrite, type); } } else { types.Add(type); handlers.Add(code); } ic = PropertyICCompiler::ComputePolymorphic(kind(), &types, &handlers, number_of_valid_types, name, extra_ic_state()); } set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { return object->IsJSGlobalObject() ? HeapType::Constant(Handle::cast(object), isolate) : HeapType::NowOf(object, isolate); } Handle IC::TypeToMap(HeapType* type, Isolate* isolate) { if (type->Is(HeapType::Number())) return isolate->factory()->heap_number_map(); if (type->Is(HeapType::Boolean())) return isolate->factory()->boolean_map(); if (type->IsConstant()) { return handle( Handle::cast(type->AsConstant()->Value())->map()); } DCHECK(type->IsClass()); return type->AsClass()->Map(); } template typename T::TypeHandle IC::MapToType(Handle map, typename T::Region* region) { if (map->instance_type() == HEAP_NUMBER_TYPE) { return T::Number(region); } else if (map->instance_type() == ODDBALL_TYPE) { // The only oddballs that can be recorded in ICs are booleans. return T::Boolean(region); } else { return T::Class(map, region); } } template Type* IC::MapToType(Handle map, Zone* zone); template Handle IC::MapToType(Handle map, Isolate* region); void IC::UpdateMonomorphicIC(Handle handler, Handle name) { DCHECK(handler->is_handler()); Handle ic = PropertyICCompiler::ComputeMonomorphic( kind(), name, receiver_type(), handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); if (!target()->FindHandlers(&handlers, types.length())) return; for (int i = 0; i < types.length(); i++) { UpdateMegamorphicCache(*types.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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
handler, Handle name) { DCHECK(handler->is_handler()); Handle ic = PropertyICCompiler::ComputeMonomorphic( kind(), name, receiver_type(), handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); if (!target()->FindHandlers(&handlers, types.length())) return; for (int i = 0; i < types.length(); i++) { UpdateMegamorphicCache(*types.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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
ic = PropertyICCompiler::ComputeMonomorphic( kind(), name, receiver_type(), handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; TargetTypes(&types); if (!target()->FindHandlers(&handlers, types.length())) return; for (int i = 0; i < types.length(); i++) { UpdateMegamorphicCache(*types.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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*receiver_type(), *name, *code); break; case DEBUG_STUB: break; case DEFAULT: case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
LoadIC::initialize_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state); } Handle LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
LoadIC::megamorphic_stub() { if (kind() == Code::LOAD_IC) { MegamorphicLoadStub stub(isolate(), LoadICState(extra_ic_state())); return stub.GetCode(); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::generic_stub(isolate()); } } Handle LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
LoadIC::pre_monomorphic_stub(Isolate* isolate, ExtraICState extra_state) { return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state); } Handle KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) { return isolate->builtins()->KeyedLoadIC_PreMonomorphic(); } Handle LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
LoadIC::pre_monomorphic_stub() const { if (kind() == Code::LOAD_IC) { return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state()); } else { DCHECK_EQ(Code::KEYED_LOAD_IC, kind()); return KeyedLoadIC::pre_monomorphic_stub(isolate()); } } Handle LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
LoadIC::SimpleFieldLoad(FieldIndex index) { LoadFieldStub stub(isolate(), index); return stub.GetCode(); } 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. set_target(*pre_monomorphic_stub()); 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) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
code; if (lookup->state() == LookupIterator::JSPROXY || lookup->state() == LookupIterator::ACCESS_CHECK) { code = slow_stub(); } else if (!lookup->IsFound()) { if (kind() == Code::LOAD_IC) { code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(), receiver_type()); // TODO(jkummerow/verwaest): Introduce a builtin that handles this case. if (code.is_null()) code = slow_stub(); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup); } PatchCache(lookup->name(), code); TRACE_IC("LoadIC", lookup->name()); } void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) { if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return; Map* map = *TypeToMap(type, isolate()); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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_type(), 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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()) && Handle::cast(receiver)->should_have_prototype() && !Handle::cast(receiver) ->map() ->has_non_instance_prototype()) { Handle stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
stub; FunctionPrototypeStub function_prototype_stub(isolate()); return function_prototype_stub.GetCode(); } Handle type = receiver_type(); 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(), receiver_type(), 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. if (receiver_is_holder) { DCHECK(receiver->IsJSObject()); Handle js_receiver = Handle::cast(receiver); int object_offset; if (Accessors::IsJSObjectFieldAccessor(type, lookup->name(), &object_offset)) { FieldIndex index = FieldIndex::ForInObjectOffset(object_offset, js_receiver->map()); return SimpleFieldLoad(index); } } Handle accessors = lookup->GetAccessors(); if (accessors->IsExecutableAccessorInfo()) { Handle info = Handle::cast(accessors); if (v8::ToCData(info->getter()) == 0) break; if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info, type)) { break; } if (!holder->HasFastProperties()) break; NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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; Handle function = Handle::cast(getter); if (!receiver->IsJSObject() && !function->IsBuiltin() && function->shared()->strict_mode() == SLOPPY) { // Calling sloppy non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileLoadCallback(lookup->name(), call_optimization); } return compiler.CompileLoadViaGetter(lookup->name(), function); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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( *type, 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 isolate()->builtins()->LoadIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { FieldIndex field = lookup->GetFieldIndex(); if (receiver_is_holder) { return SimpleFieldLoad(field); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadField(lookup->name(), field); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); if (receiver_is_holder) { LoadConstantStub stub(isolate(), lookup->GetConstantIndex()); return stub.GetCode(); } NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder, cache_holder); return compiler.CompileLoadConstant(lookup->name(), lookup->GetConstantIndex()); } 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 receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
KeyedLoadIC::LoadElementStub(Handle receiver) { Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { TargetMaps(&target_receiver_maps); } if (target_receiver_maps.length() == 0) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } // 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 && IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind())) { return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map); } 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 generic_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 generic_stub(); } return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps); } MaybeHandle KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Runtime::GetObjectProperty(isolate(), object, key), Object); return result; } Handle load_handle; Handle stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
stub = generic_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->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (!Object::ToSmi(isolate(), key).is_null()) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } set_target(*stub); 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), 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::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_type(receiver); return true; } // Receiver != holder. PrototypeIterator iter(it->isolate(), receiver); if (receiver->IsJSGlobalProxy()) { return it->GetHolder().is_identical_to( PrototypeIterator::GetCurrent(iter)); } 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) { // 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, strict_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("non_object_property_store", object, name); } // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { // Ignore other stores where the receiver is not a JSObject. // TODO(1475): Must check prototype chains of object wrappers. if (!object->IsJSObject()) return value; Handle receiver = Handle::cast(object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, JSObject::SetElement(receiver, index, value, NONE, strict_mode()), Object); return value; } // 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, strict_mode(), store_mode), Object); return result; } LookupIterator it(object, name); if (FLAG_use_ic) UpdateCaches(&it, value, store_mode); // Set the property. Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate(), result, Object::SetProperty(&it, value, strict_mode(), store_mode), Object); return result; } Handle CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
CallIC::initialize_stub(Isolate* isolate, int argc, CallICState::CallType call_type) { CallICStub stub(isolate, CallICState(argc, call_type)); Handle code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
code = stub.GetCode(); return code; } Handle StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
StoreIC::initialize_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState extra_state = ComputeExtraICState(strict_mode); Handle ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
ic = PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state); return ic; } 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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 (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } Handle StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
StoreIC::generic_stub() const { if (kind() == Code::STORE_IC) { return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state()); } else { DCHECK(kind() == Code::KEYED_STORE_IC); if (strict_mode() == STRICT) { return isolate()->builtins()->KeyedStoreIC_Generic_Strict(); } else { return isolate()->builtins()->KeyedStoreIC_Generic(); } } } 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
StoreIC::pre_monomorphic_stub(Isolate* isolate, StrictMode strict_mode) { ExtraICState state = ComputeExtraICState(strict_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. set_target(*pre_monomorphic_stub()); 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()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
code = use_ic ? ComputeHandler(lookup, value) : slow_stub(); PatchCache(lookup->name(), code); TRACE_IC("StoreIC", lookup->name()); } 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); 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()); switch (lookup->state()) { case LookupIterator::TRANSITION: { 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_type(), holder); return compiler.CompileStoreTransition(transition, lookup->name()); } case LookupIterator::INTERCEPTOR: { DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined()); NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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 (!ExecutableAccessorInfo::IsCompatibleReceiverType( isolate(), info, receiver_type())) { TRACE_GENERIC_IC(isolate(), "StoreIC", "incompatible receiver type"); break; } NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), 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_type(), holder); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(receiver, holder)) { return compiler.CompileStoreCallback(receiver, lookup->name(), call_optimization); } return compiler.CompileStoreViaSetter(receiver, lookup->name(), Handle::cast(setter)); } // TODO(dcarney): Handle correctly. DCHECK(accessors->IsDeclaredAccessorInfo()); TRACE_GENERIC_IC(isolate(), "StoreIC", "declared accessor info"); break; } case LookupIterator::DATA: { if (lookup->is_dictionary_holder()) { if (holder->IsGlobalObject()) { Handle cell = lookup->GetPropertyCell(); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(isolate(), union_type->IsConstant(), receiver->IsJSGlobalProxy()); Handle code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
code = stub.GetCodeCopyFromTemplate( Handle::cast(holder), cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code); return code; } DCHECK(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); } // -------------- Fields -------------- if (lookup->property_details().type() == FIELD) { 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_type(), holder); return compiler.CompileStoreField(lookup); } // -------------- Constant properties -------------- DCHECK(lookup->property_details().type() == CONSTANT); TRACE_GENERIC_IC(isolate(), "StoreIC", "constant property"); break; } case LookupIterator::ACCESS_CHECK: case LookupIterator::JSPROXY: case LookupIterator::NOT_FOUND: UNREACHABLE(); } return slow_stub(); } Handle KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
KeyedStoreIC::StoreElementStub(Handle receiver, KeyedAccessStoreMode store_mode) { // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS // 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 generic_stub(); } Handle receiver_map(receiver->map(), isolate()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( monomorphic_map, strict_mode(), store_mode); } // 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 = KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state()); 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); return PropertyICCompiler::ComputeKeyedStoreMonomorphic( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == 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. return PropertyICCompiler::ComputeKeyedStoreMonomorphic( receiver_map, strict_mode(), store_mode); } } 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 generic stub. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "same map added twice"); return generic_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(), "KeyedStoreIC", "max polymorph exceeded"); return generic_stub(); } // Make sure all polymorphic handlers have the same store mode, otherwise the // generic 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 generic_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 generic 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_external_array_elements() || 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 generic_stub(); } } return PropertyICCompiler::ComputeKeyedStorePolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle map, KeyedAccessStoreMode store_mode) { switch (store_mode) { case STORE_TRANSITION_SMI_TO_OBJECT: case STORE_TRANSITION_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT: case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT: return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: DCHECK(map->has_external_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, int index) { if (receiver->IsJSArray()) { return JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); } return index >= receiver->elements()->length(); } KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle receiver, Handle key, Handle value) { Handle smi_key = Object::ToSmi(isolate(), key).ToHandleChecked(); int index = smi_key->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(key); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } } if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiElements()) { if (value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE; } else { return STORE_TRANSITION_SMI_TO_DOUBLE; } } else if (value->IsHeapObject()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT; } else { return STORE_TRANSITION_SMI_TO_OBJECT; } } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { if (receiver->HasFastHoleyElements()) { return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT; } else { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } } if (!FLAG_trace_external_array_abuse && receiver->map()->has_external_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, strict_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 = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
stub = generic_stub(); if (key->IsInternalizedString()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED), Object); // TODO(jkummerow): Ideally we'd wrap this in "if (!is_target_set())", // but doing so causes Hydrogen crashes. Needs investigation. TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "unhandled internalized string key"); TRACE_IC("StoreIC", key); set_target(*stub); 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; } } if (use_ic) { DCHECK(!object->IsAccessCheckNeeded()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null(); if (receiver->elements()->map() == isolate()->heap()->sloppy_arguments_elements_map()) { if (strict_mode() == SLOPPY) { stub = sloppy_arguments_stub(); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "arguments receiver"); } } else if (key_is_smi_like && !(target().is_identical_to(sloppy_arguments_stub()))) { // 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 (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) { KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value); stub = StoreElementStub(receiver, store_mode); } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "dictionary prototype"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-smi-like key"); } } else { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "non-JSObject receiver"); } } if (store_handle.is_null()) { ASSIGN_RETURN_ON_EXCEPTION( isolate(), store_handle, Runtime::SetObjectProperty(isolate(), object, key, value, strict_mode()), Object); } DCHECK(!is_target_set()); Code* generic = *generic_stub(); if (*stub == generic) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } if (*stub == *slow_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "slow stub"); } DCHECK(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); return store_handle; } bool CallIC::DoCustomHandler(Handle receiver, Handle function, Handle vector, Handle slot, const CallICState& state) { DCHECK(FLAG_use_ic && function->IsJSFunction()); // Are we the array function? Handle array_function = Handle(isolate()->native_context()->array_function()); if (array_function.is_identical_to(Handle::cast(function))) { // Alter the slot. IC::State old_state = FeedbackToState(vector, slot); Object* feedback = vector->get(slot->value()); if (!feedback->IsAllocationSite()) { Handle new_site = isolate()->factory()->NewAllocationSite(); vector->set(slot->value(), *new_site); } CallIC_ArrayStub stub(isolate(), state); set_target(*stub.GetCode()); Handle name; if (array_function->shared()->name()->IsString()) { name = Handle(String::cast(array_function->shared()->name()), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state); return true; } return false; } void CallIC::PatchMegamorphic(Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); CallICStub stub(isolate(), state); Handle code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle
code = stub.GetCode(); set_target(*code); Handle name = isolate()->factory()->empty_string(); if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } void CallIC::HandleMiss(Handle receiver, Handle function, Handle vector, Handle slot) { CallICState state(target()->extra_ic_state()); IC::State old_state = FeedbackToState(vector, slot); Handle name = isolate()->factory()->empty_string(); Object* feedback = vector->get(slot->value()); // Hand-coded MISS handling is easier if CallIC slots don't contain smis. DCHECK(!feedback->IsSmi()); if (feedback->IsJSFunction() || !function->IsJSFunction()) { // We are going generic. vector->set(slot->value(), *TypeFeedbackVector::MegamorphicSentinel(isolate()), SKIP_WRITE_BARRIER); } else { // The feedback is either uninitialized or an allocation site. // It might be an allocation site because if we re-compile the full code // to add deoptimization support, we call with the default call-ic, and // merely need to patch the target to match the feedback. // TODO(mvstanton): the better approach is to dispense with patching // altogether, which is in progress. DCHECK(feedback == *TypeFeedbackVector::UninitializedSentinel(isolate()) || feedback->IsAllocationSite()); // Do we want to install a custom handler? if (FLAG_use_ic && DoCustomHandler(receiver, function, vector, slot, state)) { return; } vector->set(slot->value(), *function); } if (function->IsJSFunction()) { Handle js_function = Handle::cast(function); name = handle(js_function->shared()->name(), isolate()); } IC::State new_state = FeedbackToState(vector, slot); OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true); TRACE_VECTOR_IC("CallIC", name, old_state, new_state); } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(CallIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); CallIC ic(isolate); Handle receiver = args.at(0); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.HandleMiss(receiver, function, vector, slot); return *function; } RUNTIME_FUNCTION(CallIC_Customization_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 4); // A miss on a custom call ic always results in going megamorphic. CallIC ic(isolate); Handle function = args.at(1); Handle vector = args.at(2); Handle slot = args.at(3); ic.PatchMegamorphic(function, vector, slot); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(LoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } // Used from ic-.cc RUNTIME_FUNCTION(KeyedLoadIC_Miss) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key)); return *result; } RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) { TimerEventScope timer(isolate); HandleScope scope(isolate); DCHECK(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle