handler = handlers.at(i); int index = map->IndexInCodeCache(*name, *handler); if (index >= 0) { map->RemoveFromCodeCache(*name, *handler, index); return; } } } void IC::UpdateState(Handle receiver, Handle name) { if (!name->IsString()) return; if (state() != MONOMORPHIC) { if (state() == POLYMORPHIC && receiver->IsHeapObject()) { TryRemoveInvalidHandlers( handle(Handle::cast(receiver)->map()), Handle::cast(name)); } return; } if (receiver->IsUndefined() || receiver->IsNull()) return; // Remove the target from the code cache if it became invalid // because of changes in the prototype chain to avoid hitting it // again. if (TryRemoveInvalidPrototypeDependentStub( receiver, Handle::cast(name))) { return MarkMonomorphicPrototypeFailure(); } // The builtins object is special. It only changes when JavaScript // builtins are loaded lazily. It is important to keep inline // caches for the builtins object monomorphic. Therefore, if we get // an inline cache miss for the builtins object after lazily loading // JavaScript builtins, we return uninitialized as the state to // force the inline cache back to monomorphic state. if (receiver->IsJSBuiltinsObject()) state_ = UNINITIALIZED; } RelocInfo::Mode IC::ComputeMode() { Address addr = address(); Code* code = Code::cast(isolate()->FindCodeObject(addr)); for (RelocIterator it(code, RelocInfo::kCodeTargetMask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); if (info->pc() == addr) return info->rmode(); } UNREACHABLE(); return RelocInfo::NONE32; } Failure* IC::TypeError(const char* type, Handle object, Handle key) { HandleScope scope(isolate()); Handle args[2] = { key, object }; Handle error = isolate()->factory()->NewTypeError( type, HandleVector(args, 2)); return isolate()->Throw(*error); } Failure* IC::ReferenceError(const char* type, Handle name) { HandleScope scope(isolate()); Handle error = isolate()->factory()->NewReferenceError( type, HandleVector(&name, 1)); return isolate()->Throw(*error); } static int ComputeTypeInfoCountDelta(IC::State old_state, IC::State new_state) { bool was_uninitialized = old_state == UNINITIALIZED || old_state == PREMONOMORPHIC; bool is_uninitialized = new_state == UNINITIALIZED || new_state == PREMONOMORPHIC; return (was_uninitialized && !is_uninitialized) ? 1 : (!was_uninitialized && is_uninitialized) ? -1 : 0; } void IC::PostPatching(Address address, Code* target, Code* old_target) { if (FLAG_type_info_threshold == 0 && !FLAG_watch_ic_patching) { return; } Isolate* isolate = target->GetHeap()->isolate(); Code* host = isolate-> inner_pointer_to_code_cache()->GetCacheEntry(address)->code; if (host->kind() != Code::FUNCTION) return; if (FLAG_type_info_threshold > 0 && old_target->is_inline_cache_stub() && target->is_inline_cache_stub()) { int delta = ComputeTypeInfoCountDelta(old_target->ic_state(), target->ic_state()); // Not all Code objects have TypeFeedbackInfo. if (host->type_feedback_info()->IsTypeFeedbackInfo() && delta != 0) { TypeFeedbackInfo* info = TypeFeedbackInfo::cast(host->type_feedback_info()); info->change_ic_with_type_info_count(delta); } } if (host->type_feedback_info()->IsTypeFeedbackInfo()) { TypeFeedbackInfo* info = TypeFeedbackInfo::cast(host->type_feedback_info()); info->change_own_type_change_checksum(); } if (FLAG_watch_ic_patching) { host->set_profiler_ticks(0); isolate->runtime_profiler()->NotifyICChanged(); } // TODO(2029): When an optimized function is patched, it would // be nice to propagate the corresponding type information to its // unoptimized version for the benefit of later inlining. } void IC::Clear(Isolate* isolate, Address address) { Code* target = GetTargetAtAddress(address); // 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); case Code::KEYED_LOAD_IC: return KeyedLoadIC::Clear(isolate, address, target); case Code::STORE_IC: return StoreIC::Clear(isolate, address, target); case Code::KEYED_STORE_IC: return KeyedStoreIC::Clear(isolate, address, target); case Code::CALL_IC: return CallIC::Clear(address, target); case Code::KEYED_CALL_IC: return KeyedCallIC::Clear(address, target); case Code::COMPARE_IC: return CompareIC::Clear(isolate, address, target); case Code::COMPARE_NIL_IC: return CompareNilIC::Clear(address, target); 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 CallICBase::Clear(Address address, Code* target) { if (IsCleared(target)) return; bool contextual = CallICBase::Contextual::decode(target->extra_ic_state()); Code* code = target->GetIsolate()->stub_cache()->FindCallInitialize( target->arguments_count(), contextual ? RelocInfo::CODE_TARGET_CONTEXT : RelocInfo::CODE_TARGET, target->kind()); SetTargetAtAddress(address, code); } void KeyedLoadIC::Clear(Isolate* isolate, Address address, Code* target) { 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)); } void LoadIC::Clear(Isolate* isolate, Address address, Code* target) { if (IsCleared(target)) return; SetTargetAtAddress(address, *pre_monomorphic_stub(isolate)); } void StoreIC::Clear(Isolate* isolate, Address address, Code* target) { if (IsCleared(target)) return; SetTargetAtAddress(address, *pre_monomorphic_stub( isolate, StoreIC::GetStrictMode(target->extra_ic_state()))); } void KeyedStoreIC::Clear(Isolate* isolate, Address address, Code* target) { if (IsCleared(target)) return; SetTargetAtAddress(address, *pre_monomorphic_stub( isolate, StoreIC::GetStrictMode(target->extra_ic_state()))); } void CompareIC::Clear(Isolate* isolate, Address address, Code* target) { ASSERT(target->major_key() == CodeStub::CompareIC); CompareIC::State handler_state; Token::Value op; ICCompareStub::DecodeMinorKey(target->stub_info(), NULL, NULL, &handler_state, &op); // Only clear CompareICs that can retain objects. if (handler_state != KNOWN_OBJECT) return; SetTargetAtAddress(address, GetRawUninitialized(isolate, op)); PatchInlinedSmiCode(address, DISABLE_INLINED_SMI_CHECK); } Handle CallICBase::TryCallAsFunction(Handle object) { Handle delegate = Execution::GetFunctionDelegate(isolate(), object); if (delegate->IsJSFunction() && !object->IsJSFunctionProxy()) { // Patch the receiver and use the delegate as the function to // invoke. This is used for invoking objects as if they were functions. const int argc = target()->arguments_count(); StackFrameLocator locator(isolate()); JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); int index = frame->ComputeExpressionsCount() - (argc + 1); frame->SetExpression(index, *object); } return delegate; } void CallICBase::ReceiverToObjectIfRequired(Handle callee, Handle object) { while (callee->IsJSFunctionProxy()) { callee = Handle(JSFunctionProxy::cast(*callee)->call_trap(), isolate()); } if (callee->IsJSFunction()) { Handle function = Handle::cast(callee); if (!function->shared()->is_classic_mode() || function->IsBuiltin()) { // Do not wrap receiver for strict mode functions or for builtins. return; } } // And only wrap string, number or boolean. if (object->IsString() || object->IsNumber() || object->IsBoolean()) { // Change the receiver to the result of calling ToObject on it. const int argc = this->target()->arguments_count(); StackFrameLocator locator(isolate()); JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); int index = frame->ComputeExpressionsCount() - (argc + 1); frame->SetExpression(index, *isolate()->factory()->ToObject(object)); } } 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; } MaybeObject* CallICBase::LoadFunction(Handle object, Handle name) { bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // 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_call", object, name); } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = Object::GetElement(isolate(), object, index); RETURN_IF_EMPTY_HANDLE(isolate(), result); if (result->IsJSFunction()) return *result; // Try to find a suitable function delegate for the object at hand. result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; // Otherwise, it will fail in the lookup step. } // Lookup the property in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); if (!lookup.IsFound()) { // If the object does not have the requested property, check which // exception we need to throw. return IsUndeclaredGlobal(object) ? ReferenceError("not_defined", name) : TypeError("undefined_method", object, name); } // Lookup is valid: Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); // Get the property. PropertyAttributes attr; Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); if (lookup.IsInterceptor() && attr == ABSENT) { // If the object does not have the requested property, check which // exception we need to throw. return IsUndeclaredGlobal(object) ? ReferenceError("not_defined", name) : TypeError("undefined_method", object, name); } ASSERT(!result->IsTheHole()); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) { Handle function = Handle::cast(result); #ifdef ENABLE_DEBUGGER_SUPPORT // Handle stepping into a function if step into is active. Debug* debug = isolate()->debug(); if (debug->StepInActive()) { // Protect the result in a handle as the debugger can allocate and might // cause GC. debug->HandleStepIn(function, object, fp(), false); } #endif return *function; } // Try to find a suitable function delegate for the object at hand. result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, name); } Handle CallICBase::ComputeMonomorphicStub(LookupResult* lookup, Handle object, Handle name) { int argc = target()->arguments_count(); Handle holder(lookup->holder(), isolate()); switch (lookup->type()) { case FIELD: { PropertyIndex index = lookup->GetFieldIndex(); return isolate()->stub_cache()->ComputeCallField( argc, kind_, extra_ic_state(), name, object, holder, index); } case CONSTANT: { if (!lookup->IsConstantFunction()) return Handle::null(); // Get the constant function and compute the code stub for this // call; used for rewriting to monomorphic state and making sure // that the code stub is in the stub cache. Handle function(lookup->GetConstantFunction(), isolate()); return isolate()->stub_cache()->ComputeCallConstant( argc, kind_, extra_ic_state(), name, object, holder, function); } case NORMAL: { // If we return a null handle, the IC will not be patched. if (!object->IsJSObject()) return Handle::null(); Handle receiver = Handle::cast(object); if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); if (!cell->value()->IsJSFunction()) return Handle::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_ic_state(), name, receiver, global, cell, function); } else { // There is only one shared stub for calling normalized // properties. It does not traverse the prototype chain, so the // property must be found in the receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return Handle::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_ic_state()); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_ic_state(), name, object, holder); default: return Handle::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
CallICBase::ComputeMonomorphicStub(LookupResult* lookup, Handle object, Handle name) { int argc = target()->arguments_count(); Handle holder(lookup->holder(), isolate()); switch (lookup->type()) { case FIELD: { PropertyIndex index = lookup->GetFieldIndex(); return isolate()->stub_cache()->ComputeCallField( argc, kind_, extra_ic_state(), name, object, holder, index); } case CONSTANT: { if (!lookup->IsConstantFunction()) return Handle::null(); // Get the constant function and compute the code stub for this // call; used for rewriting to monomorphic state and making sure // that the code stub is in the stub cache. Handle function(lookup->GetConstantFunction(), isolate()); return isolate()->stub_cache()->ComputeCallConstant( argc, kind_, extra_ic_state(), name, object, holder, function); } case NORMAL: { // If we return a null handle, the IC will not be patched. if (!object->IsJSObject()) return Handle::null(); Handle receiver = Handle::cast(object); if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); if (!cell->value()->IsJSFunction()) return Handle::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_ic_state(), name, receiver, global, cell, function); } else { // There is only one shared stub for calling normalized // properties. It does not traverse the prototype chain, so the // property must be found in the receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return Handle::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_ic_state()); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_ic_state(), name, object, holder); default: return Handle::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
::null(); // Get the constant function and compute the code stub for this // call; used for rewriting to monomorphic state and making sure // that the code stub is in the stub cache. Handle function(lookup->GetConstantFunction(), isolate()); return isolate()->stub_cache()->ComputeCallConstant( argc, kind_, extra_ic_state(), name, object, holder, function); } case NORMAL: { // If we return a null handle, the IC will not be patched. if (!object->IsJSObject()) return Handle::null(); Handle receiver = Handle::cast(object); if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); if (!cell->value()->IsJSFunction()) return Handle::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_ic_state(), name, receiver, global, cell, function); } else { // There is only one shared stub for calling normalized // properties. It does not traverse the prototype chain, so the // property must be found in the receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return Handle::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_ic_state()); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_ic_state(), name, object, holder); default: return Handle::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
::null(); Handle receiver = Handle::cast(object); if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); if (!cell->value()->IsJSFunction()) return Handle::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_ic_state(), name, receiver, global, cell, function); } else { // There is only one shared stub for calling normalized // properties. It does not traverse the prototype chain, so the // property must be found in the receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return Handle::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_ic_state()); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_ic_state(), name, object, holder); default: return Handle::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_ic_state(), name, receiver, global, cell, function); } else { // There is only one shared stub for calling normalized // properties. It does not traverse the prototype chain, so the // property must be found in the receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return Handle::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_ic_state()); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_ic_state(), name, object, holder); default: return Handle::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_ic_state()); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_ic_state(), name, object, holder); default: return Handle::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
::null(); } } Handle CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
CallICBase::megamorphic_stub() { return isolate()->stub_cache()->ComputeCallMegamorphic( target()->arguments_count(), kind_, extra_ic_state()); } Handle CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
CallICBase::pre_monomorphic_stub() { return isolate()->stub_cache()->ComputeCallPreMonomorphic( target()->arguments_count(), kind_, extra_ic_state()); } void CallICBase::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (state() == UNINITIALIZED) { set_target(*pre_monomorphic_stub()); TRACE_IC("CallIC", name); return; } Handle code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code = ComputeMonomorphicStub(lookup, object, name); // If there's no appropriate stub we simply avoid updating the caches. // TODO(verwaest): Install a slow fallback in this case to avoid not learning, // and deopting Crankshaft code. if (code.is_null()) return; Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype(isolate())), isolate()); PatchCache(CurrentTypeOf(cache_object, isolate()), name, code); TRACE_IC("CallIC", name); } MaybeObject* KeyedCallIC::LoadFunction(Handle object, Handle key) { if (key->IsInternalizedString()) { return CallICBase::LoadFunction(object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic && state() != MEGAMORPHIC) { ASSERT(!object->IsJSGlobalProxy()); int argc = target()->arguments_count(); Handle stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub; // Use the KeyedArrayCallStub if the call is of the form array[smi](...), // where array is an instance of one of the initial array maps (without // extra named properties). // TODO(verwaest): Also support keyed calls on instances of other maps. if (object->IsJSArray() && key->IsSmi()) { Handle array = Handle::cast(object); ElementsKind kind = array->map()->elements_kind(); if (IsFastObjectElementsKind(kind) && array->map() == isolate()->get_initial_js_array_map(kind)) { KeyedArrayCallStub stub_gen(IsHoleyElementsKind(kind), argc); stub = stub_gen.GetCode(isolate()); } } if (stub.is_null()) { stub = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, kNoExtraICState); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = isolate()->stub_cache()->ComputeCallArguments(argc); } } ASSERT(!stub.is_null()); } set_target(*stub); TRACE_IC("CallIC", key); } Handle result = GetProperty(isolate(), object, key); RETURN_IF_EMPTY_HANDLE(isolate(), result); // Make receiver an object if the callee requires it. Strict mode or builtin // functions do not wrap the receiver, non-strict functions and objects // called as functions do. ReceiverToObjectIfRequired(result, object); if (result->IsJSFunction()) return *result; result = TryCallAsFunction(result); if (result->IsJSFunction()) return *result; return TypeError("property_not_function", object, key); } MaybeObject* 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); } if (FLAG_use_ic) { // Use specialized code for getting the length of strings and // string wrapper objects. The length property of string wrapper // objects is read-only and therefore always returns the length of // the underlying string value. See ECMA-262 15.5.5.1. if (object->IsStringWrapper() && name->Equals(isolate()->heap()->length_string())) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC || state() == MONOMORPHIC) { StringLengthStub string_length_stub(kind()); stub = string_length_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#length /stringwrapper]\n"); } // Get the string if we have a string wrapper object. String* string = String::cast(JSValue::cast(*object)->value()); return Smi::FromInt(string->length()); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_string()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub; if (state() == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state() == PREMONOMORPHIC) { FunctionPrototypeStub function_prototype_stub(kind()); stub = function_prototype_stub.GetCode(isolate()); } else if (state() != MEGAMORPHIC) { ASSERT(state() != GENERIC); stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); } return *Accessors::FunctionGetPrototype(Handle::cast(object)); } } // 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(*generic_stub()); return Runtime::GetElementOrCharAtOrFail(isolate(), object, index); } bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic; // Named lookup in the object. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsFound()) { if (IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (use_ic) UpdateCaches(&lookup, object, name); PropertyAttributes attr; // Get the property. Handle result = Object::GetProperty(object, object, &lookup, name, &attr); RETURN_IF_EMPTY_HANDLE(isolate(), result); // If the property is not present, check if we need to throw an // exception. if ((lookup.IsInterceptor() || lookup.IsHandler()) && attr == ABSENT && IsUndeclaredGlobal(object)) { return ReferenceError("not_defined", name); } return *result; } static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps, Handle new_receiver_map) { ASSERT(!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 type, Handle name, Handle code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code) { if (!code->is_handler()) return false; TypeHandleList types; CodeHandleList handlers; int number_of_valid_types; int handler_to_overwrite = -1; target()->FindAllTypes(&types); int number_of_types = types.length(); number_of_valid_types = number_of_types; for (int i = 0; i < number_of_types; i++) { Handle current_type = types.at(i); // Filter out deprecated maps to ensure their instances get migrated. if (current_type->IsClass() && current_type->AsClass()->is_deprecated()) { number_of_valid_types--; // 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. } else if (type->IsCurrently(current_type)) { ASSERT(handler_to_overwrite == -1); number_of_valid_types--; handler_to_overwrite = i; } } 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 (handler_to_overwrite >= 0) { handlers.Set(handler_to_overwrite, code); } else { types.Add(type); handlers.Add(code); } Handle ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
ic = isolate()->stub_cache()->ComputePolymorphicIC( &types, &handlers, number_of_valid_types, name, extra_ic_state()); set_target(*ic); return true; } Handle IC::CurrentTypeOf(Handle object, Isolate* isolate) { Type* type = object->IsJSGlobalObject() ? Type::Constant(Handle::cast(object)) : Type::OfCurrently(object); return handle(type, isolate); } Handle IC::TypeToMap(Type* type, Isolate* isolate) { if (type->Is(Type::Number())) return isolate->factory()->heap_number_map(); if (type->Is(Type::Boolean())) return isolate->factory()->oddball_map(); if (type->IsConstant()) { return handle(Handle::cast(type->AsConstant())->map()); } ASSERT(type->IsClass()); return type->AsClass(); } Type* IC::MapToType(Handle map) { if (map->instance_type() == HEAP_NUMBER_TYPE) return Type::Number(); // The only oddballs that can be recorded in ICs are booleans. if (map->instance_type() == ODDBALL_TYPE) return Type::Boolean(); return Type::Class(map); } void IC::UpdateMonomorphicIC(Handle type, Handle handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
handler, Handle name) { if (!handler->is_handler()) return set_target(*handler); Handle ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
ic = isolate()->stub_cache()->ComputeMonomorphicIC( name, type, handler, extra_ic_state()); set_target(*ic); } void IC::CopyICToMegamorphicCache(Handle name) { TypeHandleList types; CodeHandleList handlers; target()->FindAllTypes(&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(Type* type) { if (!type->IsClass()) return false; Map* receiver_map = *type->AsClass(); Map* current_map = target()->FindFirstMap(); ElementsKind receiver_elements_kind = receiver_map->elements_kind(); bool more_general_transition = IsMoreGeneralElementsKindTransition( current_map->elements_kind(), receiver_elements_kind); Map* transitioned_map = more_general_transition ? current_map->LookupElementsTransitionMap(receiver_elements_kind) : NULL; return transitioned_map == receiver_map; } void IC::PatchCache(Handle type, Handle name, Handle code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code) { switch (state()) { case UNINITIALIZED: case PREMONOMORPHIC: case MONOMORPHIC_PROTOTYPE_FAILURE: UpdateMonomorphicIC(type, code, name); break; case MONOMORPHIC: { // For now, call stubs are allowed to rewrite to the same stub. This // happens e.g., when the field does not contain a function. ASSERT(target()->is_call_stub() || target()->is_keyed_call_stub() || !target().is_identical_to(code)); Code* old_handler = target()->FindFirstHandler(); if (old_handler == *code && IsTransitionOfMonomorphicTarget(*type)) { UpdateMonomorphicIC(type, code, name); break; } // Fall through. } case POLYMORPHIC: if (!target()->is_keyed_stub()) { if (UpdatePolymorphicIC(type, name, code)) break; CopyICToMegamorphicCache(name); } set_target(*megamorphic_stub()); // Fall through. case MEGAMORPHIC: UpdateMegamorphicCache(*type, *name, *code); break; case DEBUG_STUB: break; case GENERIC: UNREACHABLE(); break; } } Handle LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
LoadIC::SimpleFieldLoad(int offset, bool inobject, Representation representation) { if (kind() == Code::LOAD_IC) { LoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } else { KeyedLoadFieldStub stub(inobject, offset, representation); return stub.GetCode(isolate()); } } void LoadIC::UpdateCaches(LookupResult* lookup, Handle object, Handle name) { 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", name); return; } Handle type = CurrentTypeOf(object, isolate()); Handle code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code; if (!lookup->IsCacheable()) { // Bail out if the result is not cacheable. code = slow_stub(); } else if (!lookup->IsProperty()) { if (kind() == Code::LOAD_IC) { code = isolate()->stub_cache()->ComputeLoadNonexistent(name, type); } else { code = slow_stub(); } } else { code = ComputeHandler(lookup, object, name); } PatchCache(type, name, code); TRACE_IC("LoadIC", name); } void IC::UpdateMegamorphicCache(Type* type, Name* name, Code* code) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. Map* map = *TypeToMap(type, isolate()); isolate()->stub_cache()->Set(name, map, code); } Handle IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
IC::ComputeHandler(LookupResult* lookup, Handle object, Handle name, Handle value) { InlineCacheHolderFlag cache_holder = GetCodeCacheForObject(*object); Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); Handle code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code = isolate()->stub_cache()->FindHandler( name, handle(stub_holder->map()), kind(), cache_holder); if (!code.is_null()) return code; code = CompileHandler(lookup, object, name, value, cache_holder); ASSERT(code->is_handler()); if (code->type() != Code::NORMAL) { HeapObject::UpdateMapCodeCache(stub_holder, name, code); } return code; } Handle LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
LoadIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle unused, InlineCacheHolderFlag cache_holder) { if (object->IsString() && name->Equals(isolate()->heap()->length_string())) { int length_index = String::kLengthOffset / kPointerSize; return SimpleFieldLoad(length_index); } Handle type = CurrentTypeOf(object, isolate()); Handle holder(lookup->holder()); LoadStubCompiler compiler(isolate(), kNoExtraICState, cache_holder, kind()); switch (lookup->type()) { case FIELD: { PropertyIndex field = lookup->GetFieldIndex(); if (object.is_identical_to(holder)) { return SimpleFieldLoad(field.translate(holder), field.is_inobject(holder), lookup->representation()); } return compiler.CompileLoadField( type, holder, name, field, lookup->representation()); } case CONSTANT: { Handle constant(lookup->GetConstant(), isolate()); // TODO(2803): Don't compute a stub for cons strings because they cannot // be embedded into code. if (constant->IsConsString()) break; return compiler.CompileLoadConstant(type, holder, name, constant); } case NORMAL: if (kind() != Code::LOAD_IC) break; if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell( global->GetPropertyCell(lookup), isolate()); Handle code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code = compiler.CompileLoadGlobal( type, global, cell, name, lookup->IsDontDelete()); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. Handle stub_holder(GetCodeCacheHolder( isolate(), *object, cache_holder)); HeapObject::UpdateMapCodeCache(stub_holder, 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 (!object.is_identical_to(holder)) break; return isolate()->builtins()->LoadIC_Normal(); case CALLBACKS: { // Use simple field loads for some well-known callback properties. if (object->IsJSObject()) { Handle receiver = Handle::cast(object); Handle map(receiver->map()); int object_offset; if (Accessors::IsJSObjectFieldAccessor(map, name, &object_offset)) { return SimpleFieldLoad(object_offset / kPointerSize); } } Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->getter()) == 0) break; if (!info->IsCompatibleReceiver(*object)) break; return compiler.CompileLoadCallback(type, holder, name, info); } else if (callback->IsAccessorPair()) { Handle getter(Handle::cast(callback)->getter(), isolate()); if (!getter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(getter); if (!object->IsJSObject() && !function->IsBuiltin() && function->shared()->is_classic_mode()) { // Calling non-strict non-builtins with a value as the receiver // requires boxing. break; } CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*object)) { return compiler.CompileLoadCallback( type, holder, name, call_optimization); } return compiler.CompileLoadViaGetter(type, holder, name, function); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return compiler.CompileLoadInterceptor(type, holder, name); default: break; } 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) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
KeyedLoadIC::LoadElementStub(Handle receiver) { // 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); MapHandleList target_receiver_maps; if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. return isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } if (target().is_identical_to(string_stub())) { target_receiver_maps.Add(isolate()->factory()->string_map()); } else { target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { return isolate()->stub_cache()->ComputeKeyedLoadElement(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 isolate()->stub_cache()->ComputeKeyedLoadElement(receiver_map); } ASSERT(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(), "KeyedIC", "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(), "KeyedIC", "max polymorph exceeded"); return generic_stub(); } return isolate()->stub_cache()->ComputeLoadElementPolymorphic( &target_receiver_maps); } MaybeObject* KeyedLoadIC::Load(Handle object, Handle key) { if (MigrateDeprecated(object)) { return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub = generic_stub(); // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); if (key->IsInternalizedString()) { maybe_object = LoadIC::Load(object, Handle::cast(key)); if (maybe_object->IsFailure()) return maybe_object; } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsString() && key->IsNumber()) { if (state() == UNINITIALIZED) stub = string_stub(); } else if (object->IsJSObject()) { Handle receiver = Handle::cast(object); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (receiver->HasIndexedInterceptor()) { stub = indexed_interceptor_stub(); } else if (!key->ToSmi()->IsFailure() && (!target().is_identical_to(non_strict_arguments_stub()))) { stub = LoadElementStub(receiver); } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("LoadIC", key); } if (maybe_object != NULL) return maybe_object; return Runtime::GetObjectPropertyOrFail(isolate(), object, key); } static bool LookupForWrite(Handle receiver, Handle name, Handle value, LookupResult* lookup, IC* ic) { Handle holder = receiver; receiver->Lookup(*name, lookup); if (lookup->IsFound()) { if (lookup->IsReadOnly() || !lookup->IsCacheable()) return false; if (lookup->holder() == *receiver) { if (lookup->IsInterceptor() && !HasInterceptorSetter(*receiver)) { receiver->LocalLookupRealNamedProperty(*name, lookup); return lookup->IsFound() && !lookup->IsReadOnly() && lookup->CanHoldValue(value) && lookup->IsCacheable(); } return lookup->CanHoldValue(value); } if (lookup->IsPropertyCallbacks()) return true; // JSGlobalProxy always goes via the runtime, so it's safe to cache. if (receiver->IsJSGlobalProxy()) return true; // Currently normal holders in the prototype chain are not supported. They // would require a runtime positive lookup and verification that the details // have not changed. if (lookup->IsInterceptor() || lookup->IsNormal()) return false; holder = Handle(lookup->holder(), lookup->isolate()); } // While normally LookupTransition gets passed the receiver, in this case we // pass the holder of the property that we overwrite. This keeps the holder in // the LookupResult intact so we can later use it to generate a prototype // chain check. This avoids a double lookup, but requires us to pass in the // receiver when trying to fetch extra information from the transition. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; PropertyDetails target_details = lookup->GetTransitionDetails(); if (target_details.IsReadOnly()) return false; // If the value that's being stored does not fit in the field that the // instance would transition to, create a new transition that fits the value. // This has to be done before generating the IC, since that IC will embed the // transition target. // Ensure the instance and its map were migrated before trying to update the // transition target. ASSERT(!receiver->map()->is_deprecated()); if (!value->FitsRepresentation(target_details.representation())) { Handle target(lookup->GetTransitionTarget()); Map::GeneralizeRepresentation( target, target->LastAdded(), value->OptimalRepresentation(), FORCE_FIELD); // Lookup the transition again since the transition tree may have changed // entirely by the migration above. receiver->map()->LookupTransition(*holder, *name, lookup); if (!lookup->IsTransition()) return false; ic->MarkMonomorphicPrototypeFailure(); } return true; } MaybeObject* StoreIC::Store(Handle object, Handle name, Handle value, JSReceiver::StoreFromKeyed store_mode) { if (MigrateDeprecated(object) || object->IsJSProxy()) { Handle result = JSReceiver::SetProperty( Handle::cast(object), name, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); 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); } // The length property of string values is read-only. Throw in strict mode. if (strict_mode() == kStrictMode && object->IsString() && name->Equals(isolate()->heap()->length_string())) { return TypeError("strict_read_only_property", object, name); } // 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); // Check if the given name is an array index. uint32_t index; if (name->AsArrayIndex(&index)) { Handle result = JSObject::SetElement(receiver, index, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *value; } // Observed objects are always modified through the runtime. if (FLAG_harmony_observation && receiver->map()->is_observed()) { Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the length // property. Note that when redefined using Object.freeze, it's possible // to have fast properties but a read-only length. if (FLAG_use_ic && receiver->IsJSArray() && name->Equals(isolate()->heap()->length_string()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties() && !receiver->map()->is_frozen()) { Handle stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub = StoreArrayLengthStub(kind(), strict_mode()).GetCode(isolate()); set_target(*stub); TRACE_IC("StoreIC", name); Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } LookupResult lookup(isolate()); bool can_store = LookupForWrite(receiver, name, value, &lookup, this); if (!can_store && strict_mode() == kStrictMode && !(lookup.IsProperty() && lookup.IsReadOnly()) && IsUndeclaredGlobal(object)) { // Strict mode doesn't allow setting non-existent global property. return ReferenceError("not_defined", name); } if (FLAG_use_ic) { if (state() == UNINITIALIZED) { Handle stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub = pre_monomorphic_stub(); set_target(*stub); TRACE_IC("StoreIC", name); } else if (can_store) { UpdateCaches(&lookup, receiver, name, value); } else if (!name->IsCacheable(isolate()) || lookup.IsNormal() || (lookup.IsField() && lookup.CanHoldValue(value))) { Handle stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub = generic_stub(); set_target(*stub); } } // Set the property. Handle result = JSReceiver::SetProperty( receiver, name, value, NONE, strict_mode(), store_mode); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } void StoreIC::UpdateCaches(LookupResult* lookup, Handle receiver, Handle name, Handle value) { ASSERT(lookup->IsFound()); // These are not cacheable, so we never see such LookupResults here. ASSERT(!lookup->IsHandler()); Handle code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code = ComputeHandler(lookup, receiver, name, value); PatchCache(CurrentTypeOf(receiver, isolate()), name, code); TRACE_IC("StoreIC", name); } Handle StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
StoreIC::CompileHandler(LookupResult* lookup, Handle object, Handle name, Handle value, InlineCacheHolderFlag cache_holder) { if (object->IsJSGlobalProxy()) return slow_stub(); ASSERT(cache_holder == OWN_MAP); // This is currently guaranteed by checks in StoreIC::Store. Handle receiver = Handle::cast(object); Handle holder(lookup->holder()); // Handlers do not use strict mode. StoreStubCompiler compiler(isolate(), kNonStrictMode, kind()); switch (lookup->type()) { case FIELD: return compiler.CompileStoreField(receiver, lookup, name); case TRANSITION: { // Explicitly pass in the receiver map since LookupForWrite may have // stored something else than the receiver in the holder. Handle transition(lookup->GetTransitionTarget()); PropertyDetails details = transition->GetLastDescriptorDetails(); if (details.type() == CALLBACKS || details.attributes() != NONE) break; return compiler.CompileStoreTransition( receiver, lookup, transition, name); } case NORMAL: if (kind() == Code::KEYED_STORE_IC) break; if (receiver->IsGlobalObject()) { // The stub generated for the global object picks the value directly // from the property cell. So the property must be directly on the // global object. Handle global = Handle::cast(receiver); Handle cell(global->GetPropertyCell(lookup), isolate()); Handle union_type = PropertyCell::UpdatedType(cell, value); StoreGlobalStub stub(union_type->IsConstant()); Handle code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
code = stub.GetCodeCopyFromTemplate( isolate(), receiver->map(), *cell); // TODO(verwaest): Move caching of these NORMAL stubs outside as well. HeapObject::UpdateMapCodeCache(receiver, name, code); return code; } ASSERT(holder.is_identical_to(receiver)); return isolate()->builtins()->StoreIC_Normal(); case CALLBACKS: { if (kind() == Code::KEYED_STORE_IC) break; Handle callback(lookup->GetCallbackObject(), isolate()); if (callback->IsExecutableAccessorInfo()) { Handle info = Handle::cast(callback); if (v8::ToCData(info->setter()) == 0) break; if (!holder->HasFastProperties()) break; if (!info->IsCompatibleReceiver(*receiver)) break; return compiler.CompileStoreCallback(receiver, holder, name, info); } else if (callback->IsAccessorPair()) { Handle setter( Handle::cast(callback)->setter(), isolate()); if (!setter->IsJSFunction()) break; if (holder->IsGlobalObject()) break; if (!holder->HasFastProperties()) break; Handle function = Handle::cast(setter); CallOptimization call_optimization(function); if (call_optimization.is_simple_api_call() && call_optimization.IsCompatibleReceiver(*receiver)) { return compiler.CompileStoreCallback( receiver, holder, name, call_optimization); } return compiler.CompileStoreViaSetter( receiver, holder, name, Handle::cast(setter)); } // TODO(dcarney): Handle correctly. if (callback->IsDeclaredAccessorInfo()) break; ASSERT(callback->IsForeign()); // No IC support for old-style native accessors. break; } case INTERCEPTOR: if (kind() == Code::KEYED_STORE_IC) break; ASSERT(HasInterceptorSetter(*receiver)); return compiler.CompileStoreInterceptor(receiver, name); case CONSTANT: break; case NONEXISTENT: case HANDLER: UNREACHABLE(); break; } 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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
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(), "KeyedIC", "non-NORMAL target type"); return generic_stub(); } Handle receiver_map(receiver->map(), isolate()); if (state() == UNINITIALIZED || state() == PREMONOMORPHIC) { // Optimistically assume that ICs that haven't reached the MONOMORPHIC state // yet will do so and stay there. Handle monomorphic_map = ComputeTransitionedMap(receiver, store_mode); store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( monomorphic_map, strict_mode(), store_mode); } MapHandleList target_receiver_maps; target()->FindAllMaps(&target_receiver_maps); if (target_receiver_maps.length() == 0) { // In the case that there is a non-map-specific IC is installed (e.g. keyed // stores into properties in dictionary mode), then there will be not // receiver maps in the target. return generic_stub(); } // 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) { // If the "old" and "new" maps are in the same elements map family, stay // MONOMORPHIC and use the map for the most generic ElementsKind. Handle transitioned_receiver_map = receiver_map; if (IsTransitionStoreMode(store_mode)) { transitioned_receiver_map = ComputeTransitionedMap(receiver, store_mode); } if (IsTransitionOfMonomorphicTarget(MapToType(transitioned_receiver_map))) { // Element family is the same, use the "worst" case map. store_mode = GetNonTransitioningStoreMode(store_mode); return isolate()->stub_cache()->ComputeKeyedStoreElement( transitioned_receiver_map, strict_mode(), store_mode); } else if (*previous_receiver_map == receiver->map() && old_store_mode == STANDARD_STORE && (IsGrowStoreMode(store_mode) || 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 isolate()->stub_cache()->ComputeKeyedStoreElement( receiver_map, strict_mode(), store_mode); } } ASSERT(state() != GENERIC); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStoreMode(store_mode)) { Handle transitioned_receiver_map = ComputeTransitionedMap(receiver, 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(), "KeyedIC", "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(), "KeyedIC", "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(), "KeyedIC", "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()) { external_arrays++; } } if (external_arrays != 0 && external_arrays != target_receiver_maps.length()) { TRACE_GENERIC_IC(isolate(), "KeyedIC", "unsupported combination of external and normal arrays"); return generic_stub(); } } return isolate()->stub_cache()->ComputeStoreElementPolymorphic( &target_receiver_maps, store_mode, strict_mode()); } Handle KeyedStoreIC::ComputeTransitionedMap( Handle receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); case STORE_TRANSITION_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, 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 JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_ELEMENTS); case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE: case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_HOLEY_DOUBLE_ELEMENTS); case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS: ASSERT(receiver->map()->has_external_array_elements()); // Fall through case STORE_NO_TRANSITION_HANDLE_COW: case STANDARD_STORE: case STORE_AND_GROW_NO_TRANSITION: return Handle(receiver->map(), isolate()); } return Handle::null(); } 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) { ASSERT(!key->ToSmi()->IsFailure()); Smi* smi_key = NULL; key->ToSmi()->To(&smi_key); int index = smi_key->value(); bool oob_access = IsOutOfBoundsAccess(receiver, index); bool allow_growth = receiver->IsJSArray() && oob_access; 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; } } } MaybeObject* KeyedStoreIC::Store(Handle object, Handle key, Handle value) { if (MigrateDeprecated(object)) { Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } // Check for values that can be converted into an internalized string directly // or is representable as a smi. key = TryConvertKey(key, isolate()); MaybeObject* maybe_object = NULL; Handle stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at
stub = generic_stub(); if (key->IsInternalizedString()) { maybe_object = StoreIC::Store(object, Handle::cast(key), value, JSReceiver::MAY_BE_STORE_FROM_KEYED); if (maybe_object->IsFailure()) return maybe_object; } else { bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded() && !(FLAG_harmony_observation && 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()) use_ic = false; } if (use_ic) { ASSERT(!object->IsJSGlobalProxy()); if (object->IsJSObject()) { Handle receiver = Handle::cast(object); bool key_is_smi_like = key->IsSmi() || !key->ToSmi()->IsFailure(); if (receiver->elements()->map() == isolate()->heap()->non_strict_arguments_elements_map()) { stub = non_strict_arguments_stub(); } else if (key_is_smi_like && !(target().is_identical_to(non_strict_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); } } } } } if (!is_target_set()) { if (*stub == *generic_stub()) { TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic"); } ASSERT(!stub.is_null()); set_target(*stub); TRACE_IC("StoreIC", key); } if (maybe_object) return maybe_object; Handle result = Runtime::SetObjectProperty(isolate(), object, key, value, NONE, strict_mode()); RETURN_IF_EMPTY_HANDLE(isolate(), result); return *result; } #undef TRACE_IC // ---------------------------------------------------------------------------- // Static IC stub generators. // // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); CallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); JSFunction* raw_function; if (!maybe_result->To(&raw_function)) return maybe_result; // The first time the inline cache is updated may be the first time the // function it references gets called. If the function is lazily compiled // then the first call will trigger a compilation. We check for this case // and we do the compilation immediately, instead of waiting for the stub // currently attached to the JSFunction object to trigger compilation. if (raw_function->is_compiled()) return raw_function; Handle function(raw_function); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedCallIC ic(isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); MaybeObject* maybe_result = ic.LoadFunction(receiver, key); // Result could be a function or a failure. JSFunction* raw_function = NULL; if (!maybe_result->To(&raw_function)) return maybe_result; if (raw_function->is_compiled()) return raw_function; Handle function(raw_function, isolate); JSFunction::CompileLazy(function, CLEAR_EXCEPTION); return *function; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); LoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissFromStubFailure) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate); Handle receiver = args.at(0); Handle key = args.at(1); ic.UpdateState(receiver, key); return ic.Load(receiver, key); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); StoreIC ic(IC::NO_EXTRA_FRAME, isolate); Handle receiver = args.at