CallICBase::ComputeMonomorphicStub(LookupResult* lookup, State state, Code::ExtraICState extra_state, Handle object, Handle name) { int argc = target()->arguments_count(); Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: { int index = lookup->GetFieldIndex(); return isolate()->stub_cache()->ComputeCallField( argc, kind_, extra_state, name, object, holder, index); } case CONSTANT_FUNCTION: { // 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()); return isolate()->stub_cache()->ComputeCallConstant( argc, kind_, extra_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)); if (!cell->value()->IsJSFunction()) return Handle::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_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_state); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_state, name, object, holder); default: return Handle::null(); } } void CallICBase::UpdateCaches(LookupResult* lookup, State state, Code::ExtraICState extra_ic_state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (lookup->holder() != *object && HasNormalObjectsInPrototypeChain( isolate(), lookup, object->GetPrototype())) { // Suppress optimization for prototype chains with slow properties objects // in the middle. return; } // Compute the number of arguments. int argc = target()->arguments_count(); bool had_proto_failure = false; Handle code; 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. code = isolate()->stub_cache()->ComputeCallPreMonomorphic( argc, kind_, extra_ic_state); } else if (state == MONOMORPHIC) { if (kind_ == Code::CALL_IC && TryUpdateExtraICState(lookup, object, &extra_ic_state)) { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else if (kind_ == Code::CALL_IC && TryRemoveInvalidPrototypeDependentStub(target(), *object, *name)) { had_proto_failure = true; code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else { code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, kind_, extra_ic_state); } } else { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } // If there's no appropriate stub we simply avoid updating the caches. if (code.is_null()) return; // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype())); // Update the stub cache. isolate()->stub_cache()->Set(*name, cache_object->map(), *code); } if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", name, state, target()); } MaybeObject* KeyedCallIC::LoadFunction(State state, Handle object, Handle key) { if (key->IsSymbol()) { return CallICBase::LoadFunction(state, Code::kNoExtraICState, object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { int argc = target()->arguments_count(); Handle map = isolate()->factory()->non_strict_arguments_elements_map(); if (object->IsJSObject() && Handle::cast(object)->elements()->map() == *map) { Handle code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
::null(); Handle receiver = Handle::cast(object); if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); if (!cell->value()->IsJSFunction()) return Handle::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_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_state); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_state, name, object, holder); default: return Handle::null(); } } void CallICBase::UpdateCaches(LookupResult* lookup, State state, Code::ExtraICState extra_ic_state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (lookup->holder() != *object && HasNormalObjectsInPrototypeChain( isolate(), lookup, object->GetPrototype())) { // Suppress optimization for prototype chains with slow properties objects // in the middle. return; } // Compute the number of arguments. int argc = target()->arguments_count(); bool had_proto_failure = false; Handle code; 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. code = isolate()->stub_cache()->ComputeCallPreMonomorphic( argc, kind_, extra_ic_state); } else if (state == MONOMORPHIC) { if (kind_ == Code::CALL_IC && TryUpdateExtraICState(lookup, object, &extra_ic_state)) { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else if (kind_ == Code::CALL_IC && TryRemoveInvalidPrototypeDependentStub(target(), *object, *name)) { had_proto_failure = true; code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else { code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, kind_, extra_ic_state); } } else { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } // If there's no appropriate stub we simply avoid updating the caches. if (code.is_null()) return; // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype())); // Update the stub cache. isolate()->stub_cache()->Set(*name, cache_object->map(), *code); } if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", name, state, target()); } MaybeObject* KeyedCallIC::LoadFunction(State state, Handle object, Handle key) { if (key->IsSymbol()) { return CallICBase::LoadFunction(state, Code::kNoExtraICState, object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { int argc = target()->arguments_count(); Handle map = isolate()->factory()->non_strict_arguments_elements_map(); if (object->IsJSObject() && Handle::cast(object)->elements()->map() == *map) { Handle code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
::null(); Handle function(JSFunction::cast(cell->value())); return isolate()->stub_cache()->ComputeCallGlobal( argc, kind_, extra_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_state); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_state, name, object, holder); default: return Handle::null(); } } void CallICBase::UpdateCaches(LookupResult* lookup, State state, Code::ExtraICState extra_ic_state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (lookup->holder() != *object && HasNormalObjectsInPrototypeChain( isolate(), lookup, object->GetPrototype())) { // Suppress optimization for prototype chains with slow properties objects // in the middle. return; } // Compute the number of arguments. int argc = target()->arguments_count(); bool had_proto_failure = false; Handle code; 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. code = isolate()->stub_cache()->ComputeCallPreMonomorphic( argc, kind_, extra_ic_state); } else if (state == MONOMORPHIC) { if (kind_ == Code::CALL_IC && TryUpdateExtraICState(lookup, object, &extra_ic_state)) { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else if (kind_ == Code::CALL_IC && TryRemoveInvalidPrototypeDependentStub(target(), *object, *name)) { had_proto_failure = true; code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else { code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, kind_, extra_ic_state); } } else { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } // If there's no appropriate stub we simply avoid updating the caches. if (code.is_null()) return; // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype())); // Update the stub cache. isolate()->stub_cache()->Set(*name, cache_object->map(), *code); } if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", name, state, target()); } MaybeObject* KeyedCallIC::LoadFunction(State state, Handle object, Handle key) { if (key->IsSymbol()) { return CallICBase::LoadFunction(state, Code::kNoExtraICState, object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { int argc = target()->arguments_count(); Handle map = isolate()->factory()->non_strict_arguments_elements_map(); if (object->IsJSObject() && Handle::cast(object)->elements()->map() == *map) { Handle code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
::null(); return isolate()->stub_cache()->ComputeCallNormal( argc, kind_, extra_state); } break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); return isolate()->stub_cache()->ComputeCallInterceptor( argc, kind_, extra_state, name, object, holder); default: return Handle::null(); } } void CallICBase::UpdateCaches(LookupResult* lookup, State state, Code::ExtraICState extra_ic_state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (lookup->holder() != *object && HasNormalObjectsInPrototypeChain( isolate(), lookup, object->GetPrototype())) { // Suppress optimization for prototype chains with slow properties objects // in the middle. return; } // Compute the number of arguments. int argc = target()->arguments_count(); bool had_proto_failure = false; Handle code; 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. code = isolate()->stub_cache()->ComputeCallPreMonomorphic( argc, kind_, extra_ic_state); } else if (state == MONOMORPHIC) { if (kind_ == Code::CALL_IC && TryUpdateExtraICState(lookup, object, &extra_ic_state)) { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else if (kind_ == Code::CALL_IC && TryRemoveInvalidPrototypeDependentStub(target(), *object, *name)) { had_proto_failure = true; code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else { code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, kind_, extra_ic_state); } } else { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } // If there's no appropriate stub we simply avoid updating the caches. if (code.is_null()) return; // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype())); // Update the stub cache. isolate()->stub_cache()->Set(*name, cache_object->map(), *code); } if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", name, state, target()); } MaybeObject* KeyedCallIC::LoadFunction(State state, Handle object, Handle key) { if (key->IsSymbol()) { return CallICBase::LoadFunction(state, Code::kNoExtraICState, object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { int argc = target()->arguments_count(); Handle map = isolate()->factory()->non_strict_arguments_elements_map(); if (object->IsJSObject() && Handle::cast(object)->elements()->map() == *map) { Handle code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
::null(); } } void CallICBase::UpdateCaches(LookupResult* lookup, State state, Code::ExtraICState extra_ic_state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (lookup->holder() != *object && HasNormalObjectsInPrototypeChain( isolate(), lookup, object->GetPrototype())) { // Suppress optimization for prototype chains with slow properties objects // in the middle. return; } // Compute the number of arguments. int argc = target()->arguments_count(); bool had_proto_failure = false; Handle code; 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. code = isolate()->stub_cache()->ComputeCallPreMonomorphic( argc, kind_, extra_ic_state); } else if (state == MONOMORPHIC) { if (kind_ == Code::CALL_IC && TryUpdateExtraICState(lookup, object, &extra_ic_state)) { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else if (kind_ == Code::CALL_IC && TryRemoveInvalidPrototypeDependentStub(target(), *object, *name)) { had_proto_failure = true; code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else { code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, kind_, extra_ic_state); } } else { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } // If there's no appropriate stub we simply avoid updating the caches. if (code.is_null()) return; // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype())); // Update the stub cache. isolate()->stub_cache()->Set(*name, cache_object->map(), *code); } if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", name, state, target()); } MaybeObject* KeyedCallIC::LoadFunction(State state, Handle object, Handle key) { if (key->IsSymbol()) { return CallICBase::LoadFunction(state, Code::kNoExtraICState, object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { int argc = target()->arguments_count(); Handle map = isolate()->factory()->non_strict_arguments_elements_map(); if (object->IsJSObject() && Handle::cast(object)->elements()->map() == *map) { Handle code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code; 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. code = isolate()->stub_cache()->ComputeCallPreMonomorphic( argc, kind_, extra_ic_state); } else if (state == MONOMORPHIC) { if (kind_ == Code::CALL_IC && TryUpdateExtraICState(lookup, object, &extra_ic_state)) { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else if (kind_ == Code::CALL_IC && TryRemoveInvalidPrototypeDependentStub(target(), *object, *name)) { had_proto_failure = true; code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } else { code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, kind_, extra_ic_state); } } else { code = ComputeMonomorphicStub(lookup, state, extra_ic_state, object, name); } // If there's no appropriate stub we simply avoid updating the caches. if (code.is_null()) return; // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. It is not the map which holds the stub. Handle cache_object = object->IsJSObject() ? Handle::cast(object) : Handle(JSObject::cast(object->GetPrototype())); // Update the stub cache. isolate()->stub_cache()->Set(*name, cache_object->map(), *code); } if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE; TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC", name, state, target()); } MaybeObject* KeyedCallIC::LoadFunction(State state, Handle object, Handle key) { if (key->IsSymbol()) { return CallICBase::LoadFunction(state, Code::kNoExtraICState, object, Handle::cast(key)); } if (object->IsUndefined() || object->IsNull()) { return TypeError("non_object_property_call", object, key); } if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) { int argc = target()->arguments_count(); Handle map = isolate()->factory()->non_strict_arguments_elements_map(); if (object->IsJSObject() && Handle::cast(object)->elements()->map() == *map) { Handle code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = isolate()->stub_cache()->ComputeCallArguments( argc, Code::KEYED_CALL_IC); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } else if (!object->IsAccessCheckNeeded()) { Handle code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = isolate()->stub_cache()->ComputeCallMegamorphic( argc, Code::KEYED_CALL_IC, Code::kNoExtraICState); set_target(*code); TRACE_IC("KeyedCallIC", key, state, target()); } } Handle result = GetProperty(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(State state, 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->IsString() || object->IsStringWrapper()) && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = object->IsString() ? isolate()->builtins()->LoadIC_StringLength() : isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state == MONOMORPHIC && object->IsStringWrapper()) { stub = isolate()->builtins()->LoadIC_StringWrapperLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n"); #endif } // Get the string if we have a string wrapper object. Handle string = object->IsJSValue() ? Handle(Handle::cast(object)->value()) : object; return Smi::FromInt(String::cast(*string)->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_ArrayLength(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n"); #endif } return JSArray::cast(*object)->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub; if (state == UNINITIALIZED) { stub = pre_monomorphic_stub(); } else if (state == PREMONOMORPHIC) { stub = isolate()->builtins()->LoadIC_FunctionPrototype(); } else if (state != MEGAMORPHIC) { stub = megamorphic_stub(); } if (!stub.is_null()) { set_target(*stub); #ifdef DEBUG if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n"); #endif } return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element if so. uint32_t index; if (name->AsArrayIndex(&index)) return object->GetElement(index); // 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.IsProperty()) { if (IsContextual(object)) { return ReferenceError("not_defined", name); } LOG(isolate(), SuspectReadEvent(*name, *object)); } // Update inline cache and stub cache. if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } // Get the property. return object->GetProperty(*object, &lookup, *name, &attr); } void LoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if the result is not cacheable. if (!lookup->IsCacheable()) return; // Loading properties from values is not common, so don't try to // deal with non-JS objects here. if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code; 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. code = pre_monomorphic_stub(); } else if (!lookup->IsProperty()) { // Nonexistent property. The result is undefined. code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver); } else { // Compute monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeLoadConstant( name, receiver, holder, constant); break; } case NORMAL: if (holder->IsGlobalObject()) { Handle global = Handle::cast(holder); Handle cell(global->GetPropertyCell(lookup)); code = isolate()->stub_cache()->ComputeLoadGlobal( name, receiver, global, cell, lookup->IsDontDelete()); } else { // 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 receiver for the stub to be // applicable. if (!holder.is_identical_to(receiver)) return; code = isolate()->stub_cache()->ComputeLoadNormal(); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(*holder)); code = isolate()->stub_cache()->ComputeLoadInterceptor( name, receiver, holder); break; default: return; } } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == PREMONOMORPHIC || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // We are transitioning from monomorphic to megamorphic case. // Place the current monomorphic stub and stub compiled for // the receiver into stub cache. Map* map = target()->FindFirstMap(); if (map != NULL) { isolate()->stub_cache()->Set(*name, map, target()); } isolate()->stub_cache()->Set(*name, receiver->map(), *code); set_target(*megamorphic_stub()); } else if (state == MEGAMORPHIC) { // Cache code holding map should be consistent with // GenerateMonomorphicCacheProbe. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("LoadIC", name, state, target()); } Handle KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedLoadIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH); return KeyedLoadElementStub(elements_kind).GetCode(); } Handle KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedLoadIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode growth_mode) { CodeHandleList handler_ics(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map = receiver_maps->at(i); Handle cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
cached_stub = ComputeMonomorphicStubWithoutMapCheck( receiver_map, strict_mode, growth_mode); handler_ics.Add(cached_stub); } KeyedLoadStubCompiler compiler(isolate()); Handle code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = compiler.CompileLoadPolymorphic( receiver_maps, &handler_ics); isolate()->counters()->keyed_load_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } MaybeObject* KeyedLoadIC::Load(State state, Handle object, Handle key, bool force_generic_stub) { // Check for values that can be converted into a symbol. // TODO(1295): Remove this code. if (key->IsHeapNumber() && isnan(Handle::cast(key)->value())) { key = isolate()->factory()->nan_symbol(); } else if (key->IsUndefined()) { key = isolate()->factory()->undefined_symbol(); } if (key->IsSymbol()) { Handle name = Handle::cast(key); // 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) { // TODO(1073): don't ignore the current stub state. // Use specialized code for getting the length of strings. if (object->IsString() && name->Equals(isolate()->heap()->length_symbol())) { Handle string = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Smi::FromInt(string->length()); } // Use specialized code for getting the length of arrays. if (object->IsJSArray() && name->Equals(isolate()->heap()->length_symbol())) { Handle array = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return array->length(); } // Use specialized code for getting prototype of functions. if (object->IsJSFunction() && name->Equals(isolate()->heap()->prototype_symbol()) && Handle::cast(object)->should_have_prototype()) { Handle function = Handle::cast(object); Handle code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype( name, function); ASSERT(!code.is_null()); set_target(*code); TRACE_IC("KeyedLoadIC", name, state, target()); return Accessors::FunctionGetPrototype(*object, 0); } } // Check if the name is trivially convertible to an index and get // the element or char if so. uint32_t index = 0; if (name->AsArrayIndex(&index)) { // Rewrite to the generic keyed load stub. if (FLAG_use_ic) set_target(*generic_stub()); return Runtime::GetElementOrCharAt(isolate(), object, index); } // Named lookup. LookupResult lookup(isolate()); LookupForRead(object, name, &lookup); // If we did not find a property, check if we need to throw an exception. if (!lookup.IsProperty() && IsContextual(object)) { return ReferenceError("not_defined", name); } if (FLAG_use_ic) { UpdateCaches(&lookup, state, object, name); } PropertyAttributes attr; if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { // 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 (attr == ABSENT && IsContextual(object)) { return ReferenceError("not_defined", name); } return *result; } return object->GetProperty(*object, &lookup, *name, &attr); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); if (use_ic) { Handle stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub = generic_stub(); if (!force_generic_stub) { 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->IsSmi() && (target() != *non_strict_arguments_stub())) { stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub); } } } else { TRACE_GENERIC_IC("KeyedLoadIC", "force generic"); } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedLoadIC", key, state, target()); // Get the property. return Runtime::GetObjectProperty(isolate(), object, key); } void KeyedLoadIC::UpdateCaches(LookupResult* lookup, State state, Handle object, Handle name) { // Bail out if we didn't find a result. if (!lookup->IsProperty() || !lookup->IsCacheable()) return; if (!object->IsJSObject()) return; Handle receiver = Handle::cast(object); if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return; // Compute the code stub for this load. Handle code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code; 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. code = pre_monomorphic_stub(); } else { // Compute a monomorphic stub. Handle holder(lookup->holder()); switch (lookup->type()) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedLoadField( name, receiver, holder, lookup->GetFieldIndex()); break; case CONSTANT_FUNCTION: { Handle constant(lookup->GetConstantFunction()); code = isolate()->stub_cache()->ComputeKeyedLoadConstant( name, receiver, holder, constant); break; } case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->getter()) == 0) return; code = isolate()->stub_cache()->ComputeKeyedLoadCallback( name, receiver, holder, callback); break; } case INTERCEPTOR: ASSERT(HasInterceptorGetter(lookup->holder())); code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor( name, receiver, holder); break; default: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = generic_stub(); break; } } // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target(*megamorphic_stub()); } TRACE_IC("KeyedLoadIC", name, state, target()); } static bool StoreICableLookup(LookupResult* lookup) { // Bail out if we didn't find a result. if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false; // Bail out if inline caching is not allowed. if (!lookup->IsCacheable()) return false; // If the property is read-only, we leave the IC in its current state. if (lookup->IsReadOnly()) return false; return true; } static bool LookupForWrite(Handle receiver, Handle name, LookupResult* lookup) { receiver->LocalLookup(*name, lookup); if (!StoreICableLookup(lookup)) { return false; } if (lookup->type() == INTERCEPTOR && receiver->GetNamedInterceptor()->setter()->IsUndefined()) { receiver->LocalLookupRealNamedProperty(*name, lookup); return StoreICableLookup(lookup); } return true; } MaybeObject* StoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle name, Handle value) { if (!object->IsJSObject()) { // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)-> SetProperty(*name, *value, NONE, strict_mode); } // 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_symbol())) { 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. 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; } // Use specialized code for setting the length of arrays with fast // properties. Slow properties might indicate redefinition of the // length property. if (receiver->IsJSArray() && name->Equals(isolate()->heap()->length_symbol()) && Handle::cast(receiver)->AllowsSetElementsLength() && receiver->HasFastProperties()) { #ifdef DEBUG if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n"); #endif Handle stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub = (strict_mode == kStrictMode) ? isolate()->builtins()->StoreIC_ArrayLength_Strict() : isolate()->builtins()->StoreIC_ArrayLength(); set_target(*stub); return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Lookup the property locally in the receiver. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { // Generate a stub for this store. UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } else { // Strict mode doesn't allow setting non-existent global property // or an assignment to a read only property. if (strict_mode == kStrictMode) { if (lookup.IsProperty() && lookup.IsReadOnly()) { return TypeError("strict_read_only_property", object, name); } else if (IsContextual(object)) { return ReferenceError("not_defined", name); } } } } if (receiver->IsJSGlobalProxy()) { // TODO(ulan): find out why we patch this site even with --no-use-ic // Generate a generic stub that goes to the runtime when we see a global // proxy as receiver. Handle stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub = (strict_mode == kStrictMode) ? global_proxy_stub_strict() : global_proxy_stub(); if (target() != *stub) { set_target(*stub); TRACE_IC("StoreIC", name, state, target()); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } void StoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeStoreField(name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: { if (lookup->GetAttributes() != NONE) return; Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeStoreField( name, receiver, index, transition, strict_mode); break; } case NORMAL: 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)); code = isolate()->stub_cache()->ComputeStoreGlobal( name, global, cell, strict_mode); } else { if (lookup->holder() != *receiver) return; code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode); } break; case CALLBACKS: { Handle callback_object(lookup->GetCallbackObject()); if (!callback_object->IsAccessorInfo()) return; Handle callback = Handle::cast(callback_object); if (v8::ToCData(callback->setter()) == 0) return; code = isolate()->stub_cache()->ComputeStoreCallback( name, receiver, callback, strict_mode); break; } case INTERCEPTOR: ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined()); code = isolate()->stub_cache()->ComputeStoreInterceptor( name, receiver, strict_mode); break; case CONSTANT_FUNCTION: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: return; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } // Patch the call site depending on the state of the cache. if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) { set_target(*code); } else if (state == MONOMORPHIC) { // Only move to megamorphic if the target changes. if (target() != *code) { set_target((strict_mode == kStrictMode) ? megamorphic_stub_strict() : megamorphic_stub()); } } else if (state == MEGAMORPHIC) { // Update the stub cache. isolate()->stub_cache()->Set(*name, receiver->map(), *code); } TRACE_IC("StoreIC", name, state, target()); } 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; } void KeyedIC::GetReceiverMapsForStub(Handle stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub, MapHandleList* result) { ASSERT(stub->is_inline_cache_stub()); if (!string_stub().is_null() && stub.is_identical_to(string_stub())) { return result->Add(isolate()->factory()->string_map()); } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) { if (stub->ic_state() == MONOMORPHIC) { result->Add(Handle(stub->FindFirstMap())); } else { ASSERT(stub->ic_state() == MEGAMORPHIC); AssertNoAllocation no_allocation; int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); for (RelocIterator it(*stub, mask); !it.done(); it.next()) { RelocInfo* info = it.rinfo(); Handle object(info->target_object()); ASSERT(object->IsMap()); AddOneReceiverMapIfMissing(result, Handle::cast(object)); } } } } Handle KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedIC::ComputeStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
generic_stub) { State ic_state = target()->ic_state(); KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind) ? ALLOW_JSARRAY_GROWTH : DO_NOT_ALLOW_JSARRAY_GROWTH; // 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() != NORMAL) { TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type"); return generic_stub; } bool monomorphic = false; MapHandleList target_receiver_maps; if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) { GetReceiverMapsForStub(Handle(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
(target()), &target_receiver_maps); } if (!IsTransitionStubKind(stub_kind)) { if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) { monomorphic = true; } else { if (ic_state == MONOMORPHIC) { // 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. monomorphic = IsMoreGeneralElementsKindTransition( target_receiver_maps.at(0)->elements_kind(), receiver->GetElementsKind()); } } } if (monomorphic) { return ComputeMonomorphicStub( receiver, stub_kind, strict_mode, generic_stub); } ASSERT(target() != *generic_stub); // Determine the list of receiver maps that this call site has seen, // adding the map that was just encountered. Handle receiver_map(receiver->map()); bool map_added = AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map); if (IsTransitionStubKind(stub_kind)) { Handle new_map = ComputeTransitionedMap(receiver, stub_kind); map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_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("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("KeyedIC", "max polymorph exceeded"); return generic_stub; } if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) == ALLOW_JSARRAY_GROWTH)) { grow_mode = ALLOW_JSARRAY_GROWTH; } Handle cache = isolate()->factory()->polymorphic_code_cache(); Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode, strict_mode); Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state); Handle probe = cache->Lookup(&target_receiver_maps, flags); if (probe->IsCode()) return Handle::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
::cast(probe); Handle stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub = ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode); PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub); return stub; } Handle KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedIC::ComputeMonomorphicStubWithoutMapCheck( Handle receiver_map, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { if ((receiver_map->instance_type() & kNotStringTag) == 0) { ASSERT(!string_stub().is_null()); return string_stub(); } else { ASSERT(receiver_map->has_dictionary_elements() || receiver_map->has_fast_elements() || receiver_map->has_fast_smi_only_elements() || receiver_map->has_fast_double_elements() || receiver_map->has_external_array_elements()); bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE; return GetElementStubWithoutMapCheck(is_js_array, receiver_map->elements_kind(), grow_mode); } } Handle KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedIC::ComputeMonomorphicStub(Handle receiver, StubKind stub_kind, StrictModeFlag strict_mode, Handle generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
generic_stub) { if (receiver->HasFastElements() || receiver->HasFastSmiOnlyElements() || receiver->HasExternalArrayElements() || receiver->HasFastDoubleElements() || receiver->HasDictionaryElements()) { return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement( receiver, stub_kind, strict_mode); } else { return generic_stub; } } Handle KeyedIC::ComputeTransitionedMap(Handle receiver, StubKind stub_kind) { switch (stub_kind) { case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT: case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT: return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS); break; case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE: case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE: return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS); break; default: UNREACHABLE(); return Handle::null(); } } Handle KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedStoreIC::GetElementStubWithoutMapCheck( bool is_js_array, ElementsKind elements_kind, KeyedAccessGrowMode grow_mode) { return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode(); } Handle KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
KeyedStoreIC::ComputePolymorphicStub( MapHandleList* receiver_maps, StrictModeFlag strict_mode, KeyedAccessGrowMode grow_mode) { // Collect MONOMORPHIC stubs for all target_receiver_maps. CodeHandleList handler_ics(receiver_maps->length()); MapHandleList transitioned_maps(receiver_maps->length()); for (int i = 0; i < receiver_maps->length(); ++i) { Handle receiver_map(receiver_maps->at(i)); Handle cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
cached_stub; Handle transitioned_map = receiver_map->FindTransitionedMap(receiver_maps); if (!transitioned_map.is_null()) { cached_stub = ElementsTransitionAndStoreStub( receiver_map->elements_kind(), // original elements_kind transitioned_map->elements_kind(), receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array strict_mode, grow_mode).GetCode(); } else { cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map, strict_mode, grow_mode); } ASSERT(!cached_stub.is_null()); handler_ics.Add(cached_stub); transitioned_maps.Add(transitioned_map); } KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode); Handle code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = compiler.CompileStorePolymorphic( receiver_maps, &handler_ics, &transitioned_maps); isolate()->counters()->keyed_store_polymorphic_stubs()->Increment(); PROFILE(isolate(), CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0)); return code; } KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle receiver, Handle key, Handle value) { ASSERT(key->IsSmi()); int index = Smi::cast(*key)->value(); bool allow_growth = receiver->IsJSArray() && JSArray::cast(*receiver)->length()->IsSmi() && index >= Smi::cast(JSArray::cast(*receiver)->length())->value(); if (allow_growth) { // Handle growing array in stub if necessary. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE; } if (value->IsHeapObject()) { return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_AND_GROW_NO_TRANSITION; } else { // Handle only in-bounds elements accesses. if (receiver->HasFastSmiOnlyElements()) { if (value->IsHeapNumber()) { return STORE_TRANSITION_SMI_TO_DOUBLE; } else if (value->IsHeapObject()) { return STORE_TRANSITION_SMI_TO_OBJECT; } } else if (receiver->HasFastDoubleElements()) { if (!value->IsSmi() && !value->IsHeapNumber()) { return STORE_TRANSITION_DOUBLE_TO_OBJECT; } } return STORE_NO_TRANSITION; } } MaybeObject* KeyedStoreIC::Store(State state, StrictModeFlag strict_mode, Handle object, Handle key, Handle value, bool force_generic) { if (key->IsSymbol()) { Handle name = Handle::cast(key); // Handle proxies. if (object->IsJSProxy()) { return JSProxy::cast(*object)->SetProperty( *name, *value, NONE, strict_mode); } // 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); } // Ignore stores where the receiver is not a JSObject. 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; } // Update inline cache and stub cache. if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) { LookupResult lookup(isolate()); if (LookupForWrite(receiver, name, &lookup)) { UpdateCaches(&lookup, state, strict_mode, receiver, name, value); } } // Set the property. return receiver->SetProperty(*name, *value, NONE, strict_mode); } // Do not use ICs for objects that require access checks (including // the global object). bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded(); ASSERT(!(use_ic && object->IsJSGlobalProxy())); if (use_ic) { Handle stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
stub = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); 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 (!force_generic) { if (key->IsSmi() && (target() != *non_strict_arguments_stub())) { StubKind stub_kind = GetStubKind(receiver, key, value); stub = ComputeStub(receiver, stub_kind, strict_mode, stub); } } else { TRACE_GENERIC_IC("KeyedStoreIC", "force generic"); } } if (!stub.is_null()) set_target(*stub); } TRACE_IC("KeyedStoreIC", key, state, target()); // Set the property. return Runtime::SetObjectProperty( isolate(), object , key, value, NONE, strict_mode); } void KeyedStoreIC::UpdateCaches(LookupResult* lookup, State state, StrictModeFlag strict_mode, Handle receiver, Handle name, Handle value) { ASSERT(!receiver->IsJSGlobalProxy()); ASSERT(StoreICableLookup(lookup)); // These are not cacheable, so we never see such LookupResults here. ASSERT(lookup->type() != HANDLER); // We get only called for properties or transitions, see StoreICableLookup. ASSERT(lookup->type() != NULL_DESCRIPTOR); // If the property has a non-field type allowing map transitions // where there is extra room in the object, we leave the IC in its // current state. PropertyType type = lookup->type(); // Compute the code stub for this store; used for rewriting to // monomorphic state and making sure that the code stub is in the // stub cache. Handle code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code; switch (type) { case FIELD: code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, lookup->GetFieldIndex(), Handle::null(), strict_mode); break; case MAP_TRANSITION: if (lookup->GetAttributes() == NONE) { Handle transition(lookup->GetTransitionMap()); int index = transition->PropertyIndexFor(*name); code = isolate()->stub_cache()->ComputeKeyedStoreField( name, receiver, index, transition, strict_mode); break; } // fall through. case NORMAL: case CONSTANT_FUNCTION: case CALLBACKS: case INTERCEPTOR: case CONSTANT_TRANSITION: case ELEMENTS_TRANSITION: // Always rewrite to the generic case so that we do not // repeatedly try to rewrite. code = (strict_mode == kStrictMode) ? generic_stub_strict() : generic_stub(); break; case HANDLER: case NULL_DESCRIPTOR: UNREACHABLE(); return; } ASSERT(!code.is_null()); // Patch the call site depending on the state of the cache. Make // sure to always rewrite from monomorphic to megamorphic. ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE); if (state == UNINITIALIZED || state == PREMONOMORPHIC) { set_target(*code); } else if (state == MONOMORPHIC) { set_target((strict_mode == kStrictMode) ? *megamorphic_stub_strict() : *megamorphic_stub()); } TRACE_IC("KeyedStoreIC", name, state, target()); } #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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); MaybeObject* maybe_result = ic.LoadFunction(state, extra_ic_state, args.at(0), args.at(1)); // Result could be a function or a failure. JSFunction* raw_function = NULL; 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); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); MaybeObject* maybe_result = ic.LoadFunction(state, args.at(0), args.at(1)); // 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); 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(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1)); } // Used from ic-.cc RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 2); KeyedLoadIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); return ic.Load(state, args.at(0), args.at(1), true); } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) { HandleScope scope; ASSERT(args.length() == 3); StoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2)); } RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) { NoHandleAllocation nha; ASSERT(args.length() == 2); JSArray* receiver = JSArray::cast(args[0]); Object* len = args[1]; // The generated code should filter out non-Smis before we get here. ASSERT(len->IsSmi()); #ifdef DEBUG // The length property has to be a writable callback property. LookupResult debug_lookup(isolate); receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup); ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly()); #endif Object* result; { MaybeObject* maybe_result = receiver->SetElementsLength(len); if (!maybe_result->ToObject(&result)) return maybe_result; } return len; } // Extend storage is called in a store inline cache when // it is necessary to extend the properties array of a // JSObject. RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) { NoHandleAllocation na; ASSERT(args.length() == 3); // Convert the parameters JSObject* object = JSObject::cast(args[0]); Map* transition = Map::cast(args[1]); Object* value = args[2]; // Check the object has run out out property space. ASSERT(object->HasFastProperties()); ASSERT(object->map()->unused_property_fields() == 0); // Expand the properties array. FixedArray* old_storage = object->properties(); int new_unused = transition->unused_property_fields(); int new_size = old_storage->length() + new_unused + 1; Object* result; { MaybeObject* maybe_result = old_storage->CopySize(new_size); if (!maybe_result->ToObject(&result)) return maybe_result; } FixedArray* new_storage = FixedArray::cast(result); new_storage->set(old_storage->length(), value); // Set the new property value and do the map transition. object->set_properties(new_storage); object->set_map(transition); // Return the stored value. return value; } // Used from ic-.cc. RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), false); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) { NoHandleAllocation na; ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); Handle object = args.at(0); Handle key = args.at(1); Handle value = args.at(2); StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state); return Runtime::SetObjectProperty(isolate, object, key, value, NONE, strict_mode); } RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) { HandleScope scope(isolate); ASSERT(args.length() == 3); KeyedStoreIC ic(isolate); IC::State state = IC::StateFrom(ic.target(), args[0], args[1]); Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state(); return ic.Store(state, Code::GetStrictMode(extra_ic_state), args.at(0), args.at(1), args.at(2), true); } void UnaryOpIC::patch(Code* code) { set_target(code); } const char* UnaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "Smi"; case HEAP_NUMBER: return "HeapNumbers"; case GENERIC: return "Generic"; default: return "Invalid"; } } UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case HEAP_NUMBER: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle operand) { ::v8::internal::TypeInfo operand_type = ::v8::internal::TypeInfo::TypeFromValue(operand); if (operand_type.IsSmi()) { return SMI; } else if (operand_type.IsNumber()) { return HEAP_NUMBER; } else { return GENERIC; } } UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType( UnaryOpIC::TypeInfo current_type, UnaryOpIC::TypeInfo previous_type) { switch (previous_type) { case UnaryOpIC::UNINITIALIZED: return current_type; case UnaryOpIC::SMI: return (current_type == UnaryOpIC::GENERIC) ? UnaryOpIC::GENERIC : UnaryOpIC::HEAP_NUMBER; case UnaryOpIC::HEAP_NUMBER: return UnaryOpIC::GENERIC; case UnaryOpIC::GENERIC: // We should never do patching if we are in GENERIC state. UNREACHABLE(); return UnaryOpIC::GENERIC; } UNREACHABLE(); return UnaryOpIC::GENERIC; } void BinaryOpIC::patch(Code* code) { set_target(code); } const char* BinaryOpIC::GetName(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return "Uninitialized"; case SMI: return "SMI"; case INT32: return "Int32s"; case HEAP_NUMBER: return "HeapNumbers"; case ODDBALL: return "Oddball"; case BOTH_STRING: return "BothStrings"; case STRING: return "Strings"; case GENERIC: return "Generic"; default: return "Invalid"; } } BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) { switch (type_info) { case UNINITIALIZED: return ::v8::internal::UNINITIALIZED; case SMI: case INT32: case HEAP_NUMBER: case ODDBALL: case BOTH_STRING: case STRING: return MONOMORPHIC; case GENERIC: return MEGAMORPHIC; } UNREACHABLE(); return ::v8::internal::UNINITIALIZED; } BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x, BinaryOpIC::TypeInfo y) { if (x == UNINITIALIZED) return y; if (y == UNINITIALIZED) return x; if (x == y) return x; if (x == BOTH_STRING && y == STRING) return STRING; if (x == STRING && y == BOTH_STRING) return STRING; if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) { return GENERIC; } if (x > y) return x; return y; } BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle left, Handle right) { ::v8::internal::TypeInfo left_type = ::v8::internal::TypeInfo::TypeFromValue(left); ::v8::internal::TypeInfo right_type = ::v8::internal::TypeInfo::TypeFromValue(right); if (left_type.IsSmi() && right_type.IsSmi()) { return SMI; } if (left_type.IsInteger32() && right_type.IsInteger32()) { // Platforms with 32-bit Smis have no distinct INT32 type. if (kSmiValueSize == 32) return SMI; return INT32; } if (left_type.IsNumber() && right_type.IsNumber()) { return HEAP_NUMBER; } // Patching for fast string ADD makes sense even if only one of the // arguments is a string. if (left_type.IsString()) { return right_type.IsString() ? BOTH_STRING : STRING; } else if (right_type.IsString()) { return STRING; } // Check for oddball objects. if (left->IsUndefined() && right->IsNumber()) return ODDBALL; if (left->IsNumber() && right->IsUndefined()) return ODDBALL; return GENERIC; } RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) { ASSERT(args.length() == 4); HandleScope scope(isolate); Handle operand = args.at(0); Token::Value op = static_cast(args.smi_at(1)); UnaryOverwriteMode mode = static_cast(args.smi_at(2)); UnaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(3)); UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand); type = UnaryOpIC::ComputeNewType(type, previous_type); UnaryOpStub stub(op, mode, type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[UnaryOpIC (%s->%s)#%s]\n", UnaryOpIC::GetName(previous_type), UnaryOpIC::GetName(type), Token::Name(op)); } UnaryOpIC ic(isolate); ic.patch(*code); } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::SUB: builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS); break; case Token::BIT_NOT: builtin = builtins->javascript_builtin(Builtins::BIT_NOT); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle result = Execution::Call(builtin_function, operand, 0, NULL, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) { ASSERT(args.length() == 5); HandleScope scope(isolate); Handle left = args.at(0); Handle right = args.at(1); int key = args.smi_at(2); Token::Value op = static_cast(args.smi_at(3)); BinaryOpIC::TypeInfo previous_type = static_cast(args.smi_at(4)); BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right); type = BinaryOpIC::JoinTypes(type, previous_type); BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED; if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) && op != Token::ADD) { type = BinaryOpIC::GENERIC; } if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) { if (op == Token::DIV || op == Token::MUL || op == Token::SHR || kSmiValueSize == 32) { // Arithmetic on two Smi inputs has yielded a heap number. // That is the only way to get here from the Smi stub. // With 32-bit Smis, all overflows give heap numbers, but with // 31-bit Smis, most operations overflow to int32 results. result_type = BinaryOpIC::HEAP_NUMBER; } else { // Other operations on SMIs that overflow yield int32s. result_type = BinaryOpIC::INT32; } } if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) { // We must be here because an operation on two INT32 types overflowed. result_type = BinaryOpIC::HEAP_NUMBER; } BinaryOpStub stub(key, type, result_type); Handle code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
code = stub.GetCode(); if (!code.is_null()) { if (FLAG_trace_ic) { PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n", BinaryOpIC::GetName(previous_type), BinaryOpIC::GetName(type), BinaryOpIC::GetName(result_type), Token::Name(op)); } BinaryOpIC ic(isolate); ic.patch(*code); // Activate inlined smi code. if (previous_type == BinaryOpIC::UNINITIALIZED) { PatchInlinedSmiCode(ic.address()); } } Handle builtins = Handle( isolate->thread_local_top()->context_->builtins(), isolate); Object* builtin = NULL; // Initialization calms down the compiler. switch (op) { case Token::ADD: builtin = builtins->javascript_builtin(Builtins::ADD); break; case Token::SUB: builtin = builtins->javascript_builtin(Builtins::SUB); break; case Token::MUL: builtin = builtins->javascript_builtin(Builtins::MUL); break; case Token::DIV: builtin = builtins->javascript_builtin(Builtins::DIV); break; case Token::MOD: builtin = builtins->javascript_builtin(Builtins::MOD); break; case Token::BIT_AND: builtin = builtins->javascript_builtin(Builtins::BIT_AND); break; case Token::BIT_OR: builtin = builtins->javascript_builtin(Builtins::BIT_OR); break; case Token::BIT_XOR: builtin = builtins->javascript_builtin(Builtins::BIT_XOR); break; case Token::SHR: builtin = builtins->javascript_builtin(Builtins::SHR); break; case Token::SAR: builtin = builtins->javascript_builtin(Builtins::SAR); break; case Token::SHL: builtin = builtins->javascript_builtin(Builtins::SHL); break; default: UNREACHABLE(); } Handle builtin_function(JSFunction::cast(builtin), isolate); bool caught_exception; Handle builtin_args[] = { right }; Handle result = Execution::Call(builtin_function, left, ARRAY_SIZE(builtin_args), builtin_args, &caught_exception); if (caught_exception) { return Failure::Exception(); } return *result; } Handle CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle
CompareIC::GetUninitialized(Token::Value op) { ICCompareStub stub(op, UNINITIALIZED); return stub.GetCode(); } CompareIC::State CompareIC::ComputeState(Code* target) { int key = target->major_key(); if (key == CodeStub::Compare) return GENERIC; ASSERT(key == CodeStub::CompareIC); return static_cast(target->compare_state()); } const char* CompareIC::GetStateName(State state) { switch (state) { case UNINITIALIZED: return "UNINITIALIZED"; case SMIS: return "SMIS"; case HEAP_NUMBERS: return "HEAP_NUMBERS"; case OBJECTS: return "OBJECTS"; case KNOWN_OBJECTS: return "OBJECTS"; case SYMBOLS: return "SYMBOLS"; case STRINGS: return "STRINGS"; case GENERIC: return "GENERIC"; default: UNREACHABLE(); return NULL; } } CompareIC::State CompareIC::TargetState(State state, bool has_inlined_smi_code, Handle x, Handle y) { switch (state) { case UNINITIALIZED: if (x->IsSmi() && y->IsSmi()) return SMIS; if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS; if (Token::IsOrderedRelationalCompareOp(op_)) { // Ordered comparisons treat undefined as NaN, so the // HEAP_NUMBER stub will do the right thing. if ((x->IsNumber() && y->IsUndefined()) || (y->IsNumber() && x->IsUndefined())) { return HEAP_NUMBERS; } } if (x->IsSymbol() && y->IsSymbol()) { // We compare symbols as strings if we need to determine // the order in a non-equality compare. return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS; } if (x->IsString() && y->IsString()) return STRINGS; if (!Token::IsEqualityOp(op_)) return GENERIC; if (x->IsJSObject() && y->IsJSObject()) { if (Handle::cast(x)->map() == Handle