1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "accessors.h"
32 #include "arguments.h"
34 #include "execution.h"
37 #include "stub-cache.h"
43 char IC::TransitionMarkFromState(IC::State state) {
45 case UNINITIALIZED: return '0';
46 case PREMONOMORPHIC: return 'P';
47 case MONOMORPHIC: return '1';
48 case MONOMORPHIC_PROTOTYPE_FAILURE: return '^';
49 case MEGAMORPHIC: return IsGeneric() ? 'G' : 'N';
51 // We never see the debugger states here, because the state is
52 // computed from the original code - not the patched code. Let
53 // these cases fall through to the unreachable code below.
54 case DEBUG_BREAK: break;
55 case DEBUG_PREPARE_STEP_IN: break;
61 void IC::TraceIC(const char* type,
66 State new_state = StateFrom(new_target,
67 HEAP->undefined_value(),
68 HEAP->undefined_value());
69 PrintF("[%s in ", type);
70 StackFrameIterator it;
71 while (it.frame()->fp() != this->fp()) it.Advance();
72 StackFrame* raw_frame = it.frame();
73 if (raw_frame->is_internal()) {
74 Isolate* isolate = new_target->GetIsolate();
75 Code* apply_builtin = isolate->builtins()->builtin(
76 Builtins::kFunctionApply);
77 if (raw_frame->unchecked_code() == apply_builtin) {
78 PrintF("apply from ");
80 raw_frame = it.frame();
83 JavaScriptFrame::PrintTop(stdout, false, true);
85 Code::GetKeyedAccessGrowMode(new_target->extra_ic_state()) ==
88 TransitionMarkFromState(old_state),
89 TransitionMarkFromState(new_state),
90 new_can_grow ? ".GROW" : "");
96 #define TRACE_GENERIC_IC(type, reason) \
98 if (FLAG_trace_ic) { \
99 PrintF("[%s patching generic stub in ", type); \
100 JavaScriptFrame::PrintTop(stdout, false, true); \
101 PrintF(" (%s)]\n", reason); \
106 #define TRACE_GENERIC_IC(type, reason)
109 #define TRACE_IC(type, name, old_state, new_target) \
110 ASSERT((TraceIC(type, name, old_state, new_target), true))
112 IC::IC(FrameDepth depth, Isolate* isolate) : isolate_(isolate) {
113 ASSERT(isolate == Isolate::Current());
114 // To improve the performance of the (much used) IC code, we unfold
115 // a few levels of the stack frame iteration code. This yields a
116 // ~35% speedup when running DeltaBlue with the '--nouse-ic' flag.
117 const Address entry =
118 Isolate::c_entry_fp(isolate->thread_local_top());
119 Address* pc_address =
120 reinterpret_cast<Address*>(entry + ExitFrameConstants::kCallerPCOffset);
121 Address fp = Memory::Address_at(entry + ExitFrameConstants::kCallerFPOffset);
122 // If there's another JavaScript frame on the stack, we need to look
123 // one frame further down the stack to find the frame pointer and
124 // the return address stack slot.
125 if (depth == EXTRA_CALL_FRAME) {
126 const int kCallerPCOffset = StandardFrameConstants::kCallerPCOffset;
127 pc_address = reinterpret_cast<Address*>(fp + kCallerPCOffset);
128 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
131 StackFrameIterator it;
132 for (int i = 0; i < depth + 1; i++) it.Advance();
133 StackFrame* frame = it.frame();
134 ASSERT(fp == frame->fp() && pc_address == frame->pc_address());
137 pc_address_ = pc_address;
141 #ifdef ENABLE_DEBUGGER_SUPPORT
142 Address IC::OriginalCodeAddress() const {
144 // Compute the JavaScript frame for the frame pointer of this IC
145 // structure. We need this to be able to find the function
146 // corresponding to the frame.
147 StackFrameIterator it;
148 while (it.frame()->fp() != this->fp()) it.Advance();
149 JavaScriptFrame* frame = JavaScriptFrame::cast(it.frame());
150 // Find the function on the stack and both the active code for the
151 // function and the original code.
152 JSFunction* function = JSFunction::cast(frame->function());
153 Handle<SharedFunctionInfo> shared(function->shared());
154 Code* code = shared->code();
155 ASSERT(Debug::HasDebugInfo(shared));
156 Code* original_code = Debug::GetDebugInfo(shared)->original_code();
157 ASSERT(original_code->IsCode());
158 // Get the address of the call site in the active code. This is the
159 // place where the call to DebugBreakXXX is and where the IC
160 // normally would be.
161 Address addr = pc() - Assembler::kCallTargetAddressOffset;
162 // Return the address in the original code. This is the place where
163 // the call which has been overwritten by the DebugBreakXXX resides
164 // and the place where the inline cache system should look.
166 original_code->instruction_start() - code->instruction_start();
172 static bool HasNormalObjectsInPrototypeChain(Isolate* isolate,
173 LookupResult* lookup,
175 Object* end = lookup->IsProperty()
176 ? lookup->holder() : Object::cast(isolate->heap()->null_value());
177 for (Object* current = receiver;
179 current = current->GetPrototype()) {
180 if (current->IsJSObject() &&
181 !JSObject::cast(current)->HasFastProperties() &&
182 !current->IsJSGlobalProxy() &&
183 !current->IsJSGlobalObject()) {
192 static bool TryRemoveInvalidPrototypeDependentStub(Code* target,
195 InlineCacheHolderFlag cache_holder =
196 Code::ExtractCacheHolderFromFlags(target->flags());
198 if (cache_holder == OWN_MAP && !receiver->IsJSObject()) {
199 // The stub was generated for JSObject but called for non-JSObject.
200 // IC::GetCodeCacheHolder is not applicable.
202 } else if (cache_holder == PROTOTYPE_MAP &&
203 receiver->GetPrototype()->IsNull()) {
204 // IC::GetCodeCacheHolder is not applicable.
207 Map* map = IC::GetCodeCacheHolder(receiver, cache_holder)->map();
209 // Decide whether the inline cache failed because of changes to the
210 // receiver itself or changes to one of its prototypes.
212 // If there are changes to the receiver itself, the map of the
213 // receiver will have changed and the current target will not be in
214 // the receiver map's code cache. Therefore, if the current target
215 // is in the receiver map's code cache, the inline cache failed due
216 // to prototype check failure.
217 int index = map->IndexInCodeCache(name, target);
219 map->RemoveFromCodeCache(String::cast(name), target, index);
227 IC::State IC::StateFrom(Code* target, Object* receiver, Object* name) {
228 IC::State state = target->ic_state();
230 if (state != MONOMORPHIC || !name->IsString()) return state;
231 if (receiver->IsUndefined() || receiver->IsNull()) return state;
233 // For keyed load/store/call, the most likely cause of cache failure is
234 // that the key has changed. We do not distinguish between
235 // prototype and non-prototype failures for keyed access.
236 Code::Kind kind = target->kind();
237 if (kind == Code::KEYED_LOAD_IC ||
238 kind == Code::KEYED_STORE_IC ||
239 kind == Code::KEYED_CALL_IC) {
243 // Remove the target from the code cache if it became invalid
244 // because of changes in the prototype chain to avoid hitting it
246 // Call stubs handle this later to allow extra IC state
248 if (kind != Code::CALL_IC &&
249 TryRemoveInvalidPrototypeDependentStub(target, receiver, name)) {
250 return MONOMORPHIC_PROTOTYPE_FAILURE;
253 // The builtins object is special. It only changes when JavaScript
254 // builtins are loaded lazily. It is important to keep inline
255 // caches for the builtins object monomorphic. Therefore, if we get
256 // an inline cache miss for the builtins object after lazily loading
257 // JavaScript builtins, we return uninitialized as the state to
258 // force the inline cache back to monomorphic state.
259 if (receiver->IsJSBuiltinsObject()) {
260 return UNINITIALIZED;
267 RelocInfo::Mode IC::ComputeMode() {
268 Address addr = address();
269 Code* code = Code::cast(isolate()->heap()->FindCodeObject(addr));
270 for (RelocIterator it(code, RelocInfo::kCodeTargetMask);
271 !it.done(); it.next()) {
272 RelocInfo* info = it.rinfo();
273 if (info->pc() == addr) return info->rmode();
276 return RelocInfo::NONE;
280 Failure* IC::TypeError(const char* type,
281 Handle<Object> object,
282 Handle<Object> key) {
283 HandleScope scope(isolate());
284 Handle<Object> args[2] = { key, object };
285 Handle<Object> error = isolate()->factory()->NewTypeError(
286 type, HandleVector(args, 2));
287 return isolate()->Throw(*error);
291 Failure* IC::ReferenceError(const char* type, Handle<String> name) {
292 HandleScope scope(isolate());
293 Handle<Object> error = isolate()->factory()->NewReferenceError(
294 type, HandleVector(&name, 1));
295 return isolate()->Throw(*error);
299 static int ComputeTypeInfoCountDelta(IC::State old_state, IC::State new_state) {
300 bool was_uninitialized =
301 old_state == UNINITIALIZED || old_state == PREMONOMORPHIC;
302 bool is_uninitialized =
303 new_state == UNINITIALIZED || new_state == PREMONOMORPHIC;
304 return (was_uninitialized && !is_uninitialized) ? 1 :
305 (!was_uninitialized && is_uninitialized) ? -1 : 0;
309 void IC::PostPatching(Address address, Code* target, Code* old_target) {
310 if (FLAG_type_info_threshold == 0 && !FLAG_watch_ic_patching) {
313 Code* host = target->GetHeap()->isolate()->
314 inner_pointer_to_code_cache()->GetCacheEntry(address)->code;
315 if (host->kind() != Code::FUNCTION) return;
317 if (FLAG_type_info_threshold > 0 &&
318 old_target->is_inline_cache_stub() &&
319 target->is_inline_cache_stub()) {
320 int delta = ComputeTypeInfoCountDelta(old_target->ic_state(),
322 // Not all Code objects have TypeFeedbackInfo.
323 if (delta != 0 && host->type_feedback_info()->IsTypeFeedbackInfo()) {
324 TypeFeedbackInfo* info =
325 TypeFeedbackInfo::cast(host->type_feedback_info());
326 info->set_ic_with_type_info_count(
327 info->ic_with_type_info_count() + delta);
330 if (FLAG_watch_ic_patching) {
331 host->set_profiler_ticks(0);
332 Isolate::Current()->runtime_profiler()->NotifyICChanged();
334 // TODO(2029): When an optimized function is patched, it would
335 // be nice to propagate the corresponding type information to its
336 // unoptimized version for the benefit of later inlining.
340 void IC::Clear(Address address) {
341 Code* target = GetTargetAtAddress(address);
343 // Don't clear debug break inline cache as it will remove the break point.
344 if (target->ic_state() == DEBUG_BREAK) return;
346 switch (target->kind()) {
347 case Code::LOAD_IC: return LoadIC::Clear(address, target);
348 case Code::KEYED_LOAD_IC:
349 return KeyedLoadIC::Clear(address, target);
350 case Code::STORE_IC: return StoreIC::Clear(address, target);
351 case Code::KEYED_STORE_IC:
352 return KeyedStoreIC::Clear(address, target);
353 case Code::CALL_IC: return CallIC::Clear(address, target);
354 case Code::KEYED_CALL_IC: return KeyedCallIC::Clear(address, target);
355 case Code::COMPARE_IC: return CompareIC::Clear(address, target);
356 case Code::UNARY_OP_IC:
357 case Code::BINARY_OP_IC:
358 case Code::TO_BOOLEAN_IC:
359 // Clearing these is tricky and does not
360 // make any performance difference.
362 default: UNREACHABLE();
367 void CallICBase::Clear(Address address, Code* target) {
368 if (target->ic_state() == UNINITIALIZED) return;
369 bool contextual = CallICBase::Contextual::decode(target->extra_ic_state());
371 Isolate::Current()->stub_cache()->FindCallInitialize(
372 target->arguments_count(),
373 contextual ? RelocInfo::CODE_TARGET_CONTEXT : RelocInfo::CODE_TARGET,
375 SetTargetAtAddress(address, code);
379 void KeyedLoadIC::Clear(Address address, Code* target) {
380 if (target->ic_state() == UNINITIALIZED) return;
381 // Make sure to also clear the map used in inline fast cases. If we
382 // do not clear these maps, cached code can keep objects alive
383 // through the embedded maps.
384 SetTargetAtAddress(address, initialize_stub());
388 void LoadIC::Clear(Address address, Code* target) {
389 if (target->ic_state() == UNINITIALIZED) return;
390 SetTargetAtAddress(address, initialize_stub());
394 void StoreIC::Clear(Address address, Code* target) {
395 if (target->ic_state() == UNINITIALIZED) return;
396 SetTargetAtAddress(address,
397 (Code::GetStrictMode(target->extra_ic_state()) == kStrictMode)
398 ? initialize_stub_strict()
399 : initialize_stub());
403 void KeyedStoreIC::Clear(Address address, Code* target) {
404 if (target->ic_state() == UNINITIALIZED) return;
405 SetTargetAtAddress(address,
406 (Code::GetStrictMode(target->extra_ic_state()) == kStrictMode)
407 ? initialize_stub_strict()
408 : initialize_stub());
412 void CompareIC::Clear(Address address, Code* target) {
413 // Only clear ICCompareStubs, we currently cannot clear generic CompareStubs.
414 if (target->major_key() != CodeStub::CompareIC) return;
415 // Only clear CompareICs that can retain objects.
416 if (target->compare_state() != KNOWN_OBJECTS) return;
417 Token::Value op = CompareIC::ComputeOperation(target);
418 SetTargetAtAddress(address, GetRawUninitialized(op));
419 PatchInlinedSmiCode(address, DISABLE_INLINED_SMI_CHECK);
423 static bool HasInterceptorGetter(JSObject* object) {
424 return !object->GetNamedInterceptor()->getter()->IsUndefined();
428 static void LookupForRead(Handle<Object> object,
430 LookupResult* lookup) {
431 // Skip all the objects with named interceptors, but
432 // without actual getter.
434 object->Lookup(*name, lookup);
435 // Besides normal conditions (property not found or it's not
436 // an interceptor), bail out if lookup is not cacheable: we won't
437 // be able to IC it anyway and regular lookup should work fine.
438 if (!lookup->IsFound()
439 || (lookup->type() != INTERCEPTOR)
440 || !lookup->IsCacheable()) {
444 Handle<JSObject> holder(lookup->holder());
445 if (HasInterceptorGetter(*holder)) {
449 holder->LocalLookupRealNamedProperty(*name, lookup);
450 if (lookup->IsProperty()) {
451 ASSERT(lookup->type() != INTERCEPTOR);
455 Handle<Object> proto(holder->GetPrototype());
456 if (proto->IsNull()) {
466 Handle<Object> CallICBase::TryCallAsFunction(Handle<Object> object) {
467 Handle<Object> delegate = Execution::GetFunctionDelegate(object);
469 if (delegate->IsJSFunction() && !object->IsJSFunctionProxy()) {
470 // Patch the receiver and use the delegate as the function to
471 // invoke. This is used for invoking objects as if they were functions.
472 const int argc = target()->arguments_count();
473 StackFrameLocator locator;
474 JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
475 int index = frame->ComputeExpressionsCount() - (argc + 1);
476 frame->SetExpression(index, *object);
483 void CallICBase::ReceiverToObjectIfRequired(Handle<Object> callee,
484 Handle<Object> object) {
485 while (callee->IsJSFunctionProxy()) {
486 callee = Handle<Object>(JSFunctionProxy::cast(*callee)->call_trap());
489 if (callee->IsJSFunction()) {
490 Handle<JSFunction> function = Handle<JSFunction>::cast(callee);
491 if (!function->shared()->is_classic_mode() || function->IsBuiltin()) {
492 // Do not wrap receiver for strict mode functions or for builtins.
497 // And only wrap string, number or boolean.
498 if (object->IsString() || object->IsNumber() || object->IsBoolean()) {
499 // Change the receiver to the result of calling ToObject on it.
500 const int argc = this->target()->arguments_count();
501 StackFrameLocator locator;
502 JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
503 int index = frame->ComputeExpressionsCount() - (argc + 1);
504 frame->SetExpression(index, *isolate()->factory()->ToObject(object));
509 MaybeObject* CallICBase::LoadFunction(State state,
510 Code::ExtraICState extra_ic_state,
511 Handle<Object> object,
512 Handle<String> name) {
513 // If the object is undefined or null it's illegal to try to get any
514 // of its properties; throw a TypeError in that case.
515 if (object->IsUndefined() || object->IsNull()) {
516 return TypeError("non_object_property_call", object, name);
519 // Check if the name is trivially convertible to an index and get
520 // the element if so.
522 if (name->AsArrayIndex(&index)) {
523 Handle<Object> result = Object::GetElement(object, index);
524 RETURN_IF_EMPTY_HANDLE(isolate(), result);
525 if (result->IsJSFunction()) return *result;
527 // Try to find a suitable function delegate for the object at hand.
528 result = TryCallAsFunction(result);
529 if (result->IsJSFunction()) return *result;
531 // Otherwise, it will fail in the lookup step.
534 // Lookup the property in the object.
535 LookupResult lookup(isolate());
536 LookupForRead(object, name, &lookup);
538 if (!lookup.IsProperty()) {
539 // If the object does not have the requested property, check which
540 // exception we need to throw.
541 return IsContextual(object)
542 ? ReferenceError("not_defined", name)
543 : TypeError("undefined_method", object, name);
546 // Lookup is valid: Update inline cache and stub cache.
548 UpdateCaches(&lookup, state, extra_ic_state, object, name);
552 PropertyAttributes attr;
553 Handle<Object> result =
554 Object::GetProperty(object, object, &lookup, name, &attr);
555 RETURN_IF_EMPTY_HANDLE(isolate(), result);
557 if (lookup.type() == INTERCEPTOR && attr == ABSENT) {
558 // If the object does not have the requested property, check which
559 // exception we need to throw.
560 return IsContextual(object)
561 ? ReferenceError("not_defined", name)
562 : TypeError("undefined_method", object, name);
565 ASSERT(!result->IsTheHole());
567 // Make receiver an object if the callee requires it. Strict mode or builtin
568 // functions do not wrap the receiver, non-strict functions and objects
569 // called as functions do.
570 ReceiverToObjectIfRequired(result, object);
572 if (result->IsJSFunction()) {
573 Handle<JSFunction> function = Handle<JSFunction>::cast(result);
574 #ifdef ENABLE_DEBUGGER_SUPPORT
575 // Handle stepping into a function if step into is active.
576 Debug* debug = isolate()->debug();
577 if (debug->StepInActive()) {
578 // Protect the result in a handle as the debugger can allocate and might
580 debug->HandleStepIn(function, object, fp(), false);
586 // Try to find a suitable function delegate for the object at hand.
587 result = TryCallAsFunction(result);
588 if (result->IsJSFunction()) return *result;
590 return TypeError("property_not_function", object, name);
594 bool CallICBase::TryUpdateExtraICState(LookupResult* lookup,
595 Handle<Object> object,
596 Code::ExtraICState* extra_ic_state) {
597 ASSERT(kind_ == Code::CALL_IC);
598 if (lookup->type() != CONSTANT_FUNCTION) return false;
599 JSFunction* function = lookup->GetConstantFunction();
600 if (!function->shared()->HasBuiltinFunctionId()) return false;
602 // Fetch the arguments passed to the called function.
603 const int argc = target()->arguments_count();
604 Address entry = isolate()->c_entry_fp(isolate()->thread_local_top());
605 Address fp = Memory::Address_at(entry + ExitFrameConstants::kCallerFPOffset);
606 Arguments args(argc + 1,
607 &Memory::Object_at(fp +
608 StandardFrameConstants::kCallerSPOffset +
609 argc * kPointerSize));
610 switch (function->shared()->builtin_function_id()) {
611 case kStringCharCodeAt:
613 if (object->IsString()) {
614 String* string = String::cast(*object);
615 // Check there's the right string value or wrapper in the receiver slot.
616 ASSERT(string == args[0] || string == JSValue::cast(args[0])->value());
617 // If we're in the default (fastest) state and the index is
618 // out of bounds, update the state to record this fact.
619 if (StringStubState::decode(*extra_ic_state) == DEFAULT_STRING_STUB &&
620 argc >= 1 && args[1]->IsNumber()) {
621 double index = DoubleToInteger(args.number_at(1));
622 if (index < 0 || index >= string->length()) {
624 StringStubState::update(*extra_ic_state,
625 STRING_INDEX_OUT_OF_BOUNDS);
638 Handle<Code> CallICBase::ComputeMonomorphicStub(LookupResult* lookup,
640 Code::ExtraICState extra_state,
641 Handle<Object> object,
642 Handle<String> name) {
643 int argc = target()->arguments_count();
644 Handle<JSObject> holder(lookup->holder());
645 switch (lookup->type()) {
647 int index = lookup->GetFieldIndex();
648 return isolate()->stub_cache()->ComputeCallField(
649 argc, kind_, extra_state, name, object, holder, index);
651 case CONSTANT_FUNCTION: {
652 // Get the constant function and compute the code stub for this
653 // call; used for rewriting to monomorphic state and making sure
654 // that the code stub is in the stub cache.
655 Handle<JSFunction> function(lookup->GetConstantFunction());
656 return isolate()->stub_cache()->ComputeCallConstant(
657 argc, kind_, extra_state, name, object, holder, function);
660 // If we return a null handle, the IC will not be patched.
661 if (!object->IsJSObject()) return Handle<Code>::null();
662 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
664 if (holder->IsGlobalObject()) {
665 Handle<GlobalObject> global = Handle<GlobalObject>::cast(holder);
666 Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(lookup));
667 if (!cell->value()->IsJSFunction()) return Handle<Code>::null();
668 Handle<JSFunction> function(JSFunction::cast(cell->value()));
669 return isolate()->stub_cache()->ComputeCallGlobal(
670 argc, kind_, extra_state, name, receiver, global, cell, function);
672 // There is only one shared stub for calling normalized
673 // properties. It does not traverse the prototype chain, so the
674 // property must be found in the receiver for the stub to be
676 if (!holder.is_identical_to(receiver)) return Handle<Code>::null();
677 return isolate()->stub_cache()->ComputeCallNormal(
678 argc, kind_, extra_state, IsQmlGlobal(holder));
683 ASSERT(HasInterceptorGetter(*holder));
684 return isolate()->stub_cache()->ComputeCallInterceptor(
685 argc, kind_, extra_state, name, object, holder);
687 return Handle<Code>::null();
692 void CallICBase::UpdateCaches(LookupResult* lookup,
694 Code::ExtraICState extra_ic_state,
695 Handle<Object> object,
696 Handle<String> name) {
697 // Bail out if we didn't find a result.
698 if (!lookup->IsProperty() || !lookup->IsCacheable()) return;
700 if (lookup->holder() != *object &&
701 HasNormalObjectsInPrototypeChain(
702 isolate(), lookup, object->GetPrototype())) {
703 // Suppress optimization for prototype chains with slow properties objects
708 // Compute the number of arguments.
709 int argc = target()->arguments_count();
710 bool had_proto_failure = false;
712 if (state == UNINITIALIZED) {
713 // This is the first time we execute this inline cache.
714 // Set the target to the pre monomorphic stub to delay
715 // setting the monomorphic state.
716 code = isolate()->stub_cache()->ComputeCallPreMonomorphic(
717 argc, kind_, extra_ic_state);
718 } else if (state == MONOMORPHIC) {
719 if (kind_ == Code::CALL_IC &&
720 TryUpdateExtraICState(lookup, object, &extra_ic_state)) {
721 code = ComputeMonomorphicStub(lookup, state, extra_ic_state,
723 } else if (kind_ == Code::CALL_IC &&
724 TryRemoveInvalidPrototypeDependentStub(target(),
727 had_proto_failure = true;
728 code = ComputeMonomorphicStub(lookup, state, extra_ic_state,
731 code = isolate()->stub_cache()->ComputeCallMegamorphic(
732 argc, kind_, extra_ic_state);
735 code = ComputeMonomorphicStub(lookup, state, extra_ic_state,
739 // If there's no appropriate stub we simply avoid updating the caches.
740 if (code.is_null()) return;
742 // Patch the call site depending on the state of the cache.
743 if (state == UNINITIALIZED ||
744 state == PREMONOMORPHIC ||
745 state == MONOMORPHIC ||
746 state == MONOMORPHIC_PROTOTYPE_FAILURE) {
748 } else if (state == MEGAMORPHIC) {
749 // Cache code holding map should be consistent with
750 // GenerateMonomorphicCacheProbe. It is not the map which holds the stub.
751 Handle<JSObject> cache_object = object->IsJSObject()
752 ? Handle<JSObject>::cast(object)
753 : Handle<JSObject>(JSObject::cast(object->GetPrototype()));
754 // Update the stub cache.
755 isolate()->stub_cache()->Set(*name, cache_object->map(), *code);
758 if (had_proto_failure) state = MONOMORPHIC_PROTOTYPE_FAILURE;
759 TRACE_IC(kind_ == Code::CALL_IC ? "CallIC" : "KeyedCallIC",
760 name, state, target());
764 MaybeObject* KeyedCallIC::LoadFunction(State state,
765 Handle<Object> object,
766 Handle<Object> key) {
767 if (key->IsSymbol()) {
768 return CallICBase::LoadFunction(state,
769 Code::kNoExtraICState,
771 Handle<String>::cast(key));
774 if (object->IsUndefined() || object->IsNull()) {
775 return TypeError("non_object_property_call", object, key);
778 if (FLAG_use_ic && state != MEGAMORPHIC && object->IsHeapObject()) {
779 int argc = target()->arguments_count();
781 isolate()->factory()->non_strict_arguments_elements_map();
782 if (object->IsJSObject() &&
783 Handle<JSObject>::cast(object)->elements()->map() == *map) {
784 Handle<Code> code = isolate()->stub_cache()->ComputeCallArguments(
785 argc, Code::KEYED_CALL_IC);
787 TRACE_IC("KeyedCallIC", key, state, target());
788 } else if (!object->IsAccessCheckNeeded()) {
789 Handle<Code> code = isolate()->stub_cache()->ComputeCallMegamorphic(
790 argc, Code::KEYED_CALL_IC, Code::kNoExtraICState);
792 TRACE_IC("KeyedCallIC", key, state, target());
796 Handle<Object> result = GetProperty(object, key);
797 RETURN_IF_EMPTY_HANDLE(isolate(), result);
799 // Make receiver an object if the callee requires it. Strict mode or builtin
800 // functions do not wrap the receiver, non-strict functions and objects
801 // called as functions do.
802 ReceiverToObjectIfRequired(result, object);
803 if (result->IsJSFunction()) return *result;
805 result = TryCallAsFunction(result);
806 if (result->IsJSFunction()) return *result;
808 return TypeError("property_not_function", object, key);
812 MaybeObject* LoadIC::Load(State state,
813 Handle<Object> object,
814 Handle<String> name) {
815 // If the object is undefined or null it's illegal to try to get any
816 // of its properties; throw a TypeError in that case.
817 if (object->IsUndefined() || object->IsNull()) {
818 return TypeError("non_object_property_load", object, name);
822 // Use specialized code for getting the length of strings and
823 // string wrapper objects. The length property of string wrapper
824 // objects is read-only and therefore always returns the length of
825 // the underlying string value. See ECMA-262 15.5.5.1.
826 if ((object->IsString() || object->IsStringWrapper()) &&
827 name->Equals(isolate()->heap()->length_symbol())) {
829 if (state == UNINITIALIZED) {
830 stub = pre_monomorphic_stub();
831 } else if (state == PREMONOMORPHIC) {
832 stub = object->IsString()
833 ? isolate()->builtins()->LoadIC_StringLength()
834 : isolate()->builtins()->LoadIC_StringWrapperLength();
835 } else if (state == MONOMORPHIC && object->IsStringWrapper()) {
836 stub = isolate()->builtins()->LoadIC_StringWrapperLength();
837 } else if (state != MEGAMORPHIC) {
838 stub = megamorphic_stub();
840 if (!stub.is_null()) {
843 if (FLAG_trace_ic) PrintF("[LoadIC : +#length /string]\n");
846 // Get the string if we have a string wrapper object.
847 Handle<Object> string = object->IsJSValue()
848 ? Handle<Object>(Handle<JSValue>::cast(object)->value())
850 return Smi::FromInt(String::cast(*string)->length());
853 // Use specialized code for getting the length of arrays.
854 if (object->IsJSArray() &&
855 name->Equals(isolate()->heap()->length_symbol())) {
857 if (state == UNINITIALIZED) {
858 stub = pre_monomorphic_stub();
859 } else if (state == PREMONOMORPHIC) {
860 stub = isolate()->builtins()->LoadIC_ArrayLength();
861 } else if (state != MEGAMORPHIC) {
862 stub = megamorphic_stub();
864 if (!stub.is_null()) {
867 if (FLAG_trace_ic) PrintF("[LoadIC : +#length /array]\n");
870 return JSArray::cast(*object)->length();
873 // Use specialized code for getting prototype of functions.
874 if (object->IsJSFunction() &&
875 name->Equals(isolate()->heap()->prototype_symbol()) &&
876 Handle<JSFunction>::cast(object)->should_have_prototype()) {
878 if (state == UNINITIALIZED) {
879 stub = pre_monomorphic_stub();
880 } else if (state == PREMONOMORPHIC) {
881 stub = isolate()->builtins()->LoadIC_FunctionPrototype();
882 } else if (state != MEGAMORPHIC) {
883 stub = megamorphic_stub();
885 if (!stub.is_null()) {
888 if (FLAG_trace_ic) PrintF("[LoadIC : +#prototype /function]\n");
891 return Accessors::FunctionGetPrototype(*object, 0);
895 // Check if the name is trivially convertible to an index and get
896 // the element if so.
898 if (name->AsArrayIndex(&index)) return object->GetElement(index);
900 // Named lookup in the object.
901 LookupResult lookup(isolate());
902 LookupForRead(object, name, &lookup);
904 // If we did not find a property, check if we need to throw an exception.
905 if (!lookup.IsProperty()) {
906 if (IsContextual(object)) {
907 return ReferenceError("not_defined", name);
909 LOG(isolate(), SuspectReadEvent(*name, *object));
912 // Update inline cache and stub cache.
914 UpdateCaches(&lookup, state, object, name);
917 PropertyAttributes attr;
918 if (lookup.IsFound() &&
919 (lookup.type() == INTERCEPTOR || lookup.type() == HANDLER)) {
921 Handle<Object> result =
922 Object::GetProperty(object, object, &lookup, name, &attr);
923 RETURN_IF_EMPTY_HANDLE(isolate(), result);
924 // If the property is not present, check if we need to throw an
926 if (attr == ABSENT && IsContextual(object)) {
927 return ReferenceError("not_defined", name);
933 return object->GetProperty(*object, &lookup, *name, &attr);
937 void LoadIC::UpdateCaches(LookupResult* lookup,
939 Handle<Object> object,
940 Handle<String> name) {
941 // Bail out if the result is not cacheable.
942 if (!lookup->IsCacheable()) return;
944 // Loading properties from values is not common, so don't try to
945 // deal with non-JS objects here.
946 if (!object->IsJSObject()) return;
947 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
949 if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return;
951 // Compute the code stub for this load.
953 if (state == UNINITIALIZED) {
954 // This is the first time we execute this inline cache.
955 // Set the target to the pre monomorphic stub to delay
956 // setting the monomorphic state.
957 code = pre_monomorphic_stub();
958 } else if (!lookup->IsProperty()) {
959 // Nonexistent property. The result is undefined.
960 code = isolate()->stub_cache()->ComputeLoadNonexistent(name, receiver);
962 // Compute monomorphic stub.
963 Handle<JSObject> holder(lookup->holder());
964 switch (lookup->type()) {
966 code = isolate()->stub_cache()->ComputeLoadField(
967 name, receiver, holder, lookup->GetFieldIndex());
969 case CONSTANT_FUNCTION: {
970 Handle<JSFunction> constant(lookup->GetConstantFunction());
971 code = isolate()->stub_cache()->ComputeLoadConstant(
972 name, receiver, holder, constant);
976 if (holder->IsGlobalObject()) {
977 Handle<GlobalObject> global = Handle<GlobalObject>::cast(holder);
978 Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(lookup));
979 code = isolate()->stub_cache()->ComputeLoadGlobal(
980 name, receiver, global, cell, lookup->IsDontDelete());
982 // There is only one shared stub for loading normalized
983 // properties. It does not traverse the prototype chain, so the
984 // property must be found in the receiver for the stub to be
986 if (!holder.is_identical_to(receiver)) return;
987 code = isolate()->stub_cache()->ComputeLoadNormal();
991 Handle<Object> callback_object(lookup->GetCallbackObject());
992 if (!callback_object->IsAccessorInfo()) return;
993 Handle<AccessorInfo> callback =
994 Handle<AccessorInfo>::cast(callback_object);
995 if (v8::ToCData<Address>(callback->getter()) == 0) return;
996 code = isolate()->stub_cache()->ComputeLoadCallback(
997 name, receiver, holder, callback);
1001 ASSERT(HasInterceptorGetter(*holder));
1002 code = isolate()->stub_cache()->ComputeLoadInterceptor(
1003 name, receiver, holder);
1010 // Patch the call site depending on the state of the cache.
1011 if (state == UNINITIALIZED ||
1012 state == PREMONOMORPHIC ||
1013 state == MONOMORPHIC_PROTOTYPE_FAILURE) {
1015 } else if (state == MONOMORPHIC) {
1016 // We are transitioning from monomorphic to megamorphic case.
1017 // Place the current monomorphic stub and stub compiled for
1018 // the receiver into stub cache.
1019 Map* map = target()->FindFirstMap();
1021 isolate()->stub_cache()->Set(*name, map, target());
1023 isolate()->stub_cache()->Set(*name, receiver->map(), *code);
1025 set_target(*megamorphic_stub());
1026 } else if (state == MEGAMORPHIC) {
1027 // Cache code holding map should be consistent with
1028 // GenerateMonomorphicCacheProbe.
1029 isolate()->stub_cache()->Set(*name, receiver->map(), *code);
1032 TRACE_IC("LoadIC", name, state, target());
1036 Handle<Code> KeyedLoadIC::GetElementStubWithoutMapCheck(
1038 ElementsKind elements_kind,
1039 KeyedAccessGrowMode grow_mode) {
1040 ASSERT(grow_mode == DO_NOT_ALLOW_JSARRAY_GROWTH);
1041 return KeyedLoadElementStub(elements_kind).GetCode();
1045 Handle<Code> KeyedLoadIC::ComputePolymorphicStub(
1046 MapHandleList* receiver_maps,
1047 StrictModeFlag strict_mode,
1048 KeyedAccessGrowMode growth_mode) {
1049 CodeHandleList handler_ics(receiver_maps->length());
1050 for (int i = 0; i < receiver_maps->length(); ++i) {
1051 Handle<Map> receiver_map = receiver_maps->at(i);
1052 Handle<Code> cached_stub = ComputeMonomorphicStubWithoutMapCheck(
1053 receiver_map, strict_mode, growth_mode);
1054 handler_ics.Add(cached_stub);
1056 KeyedLoadStubCompiler compiler(isolate());
1057 Handle<Code> code = compiler.CompileLoadPolymorphic(
1058 receiver_maps, &handler_ics);
1059 isolate()->counters()->keyed_load_polymorphic_stubs()->Increment();
1061 CodeCreateEvent(Logger::KEYED_LOAD_MEGAMORPHIC_IC_TAG, *code, 0));
1066 static Handle<Object> TryConvertKey(Handle<Object> key, Isolate* isolate) {
1067 // This helper implements a few common fast cases for converting
1068 // non-smi keys of keyed loads/stores to a smi or a string.
1069 if (key->IsHeapNumber()) {
1070 double value = Handle<HeapNumber>::cast(key)->value();
1072 key = isolate->factory()->nan_symbol();
1074 int int_value = FastD2I(value);
1075 if (value == int_value && Smi::IsValid(int_value)) {
1076 key = Handle<Smi>(Smi::FromInt(int_value));
1079 } else if (key->IsUndefined()) {
1080 key = isolate->factory()->undefined_symbol();
1086 MaybeObject* KeyedLoadIC::Load(State state,
1087 Handle<Object> object,
1089 bool force_generic_stub) {
1090 // Check for values that can be converted into a symbol directly or
1091 // is representable as a smi.
1092 key = TryConvertKey(key, isolate());
1094 if (key->IsSymbol()) {
1095 Handle<String> name = Handle<String>::cast(key);
1097 // If the object is undefined or null it's illegal to try to get any
1098 // of its properties; throw a TypeError in that case.
1099 if (object->IsUndefined() || object->IsNull()) {
1100 return TypeError("non_object_property_load", object, name);
1104 // TODO(1073): don't ignore the current stub state.
1106 // Use specialized code for getting the length of strings.
1107 if (object->IsString() &&
1108 name->Equals(isolate()->heap()->length_symbol())) {
1109 Handle<String> string = Handle<String>::cast(object);
1111 isolate()->stub_cache()->ComputeKeyedLoadStringLength(name, string);
1112 ASSERT(!code.is_null());
1114 TRACE_IC("KeyedLoadIC", name, state, target());
1115 return Smi::FromInt(string->length());
1118 // Use specialized code for getting the length of arrays.
1119 if (object->IsJSArray() &&
1120 name->Equals(isolate()->heap()->length_symbol())) {
1121 Handle<JSArray> array = Handle<JSArray>::cast(object);
1123 isolate()->stub_cache()->ComputeKeyedLoadArrayLength(name, array);
1124 ASSERT(!code.is_null());
1126 TRACE_IC("KeyedLoadIC", name, state, target());
1127 return array->length();
1130 // Use specialized code for getting prototype of functions.
1131 if (object->IsJSFunction() &&
1132 name->Equals(isolate()->heap()->prototype_symbol()) &&
1133 Handle<JSFunction>::cast(object)->should_have_prototype()) {
1134 Handle<JSFunction> function = Handle<JSFunction>::cast(object);
1136 isolate()->stub_cache()->ComputeKeyedLoadFunctionPrototype(
1138 ASSERT(!code.is_null());
1140 TRACE_IC("KeyedLoadIC", name, state, target());
1141 return Accessors::FunctionGetPrototype(*object, 0);
1145 // Check if the name is trivially convertible to an index and get
1146 // the element or char if so.
1148 if (name->AsArrayIndex(&index)) {
1149 // Rewrite to the generic keyed load stub.
1150 if (FLAG_use_ic) set_target(*generic_stub());
1151 return Runtime::GetElementOrCharAt(isolate(), object, index);
1155 LookupResult lookup(isolate());
1156 LookupForRead(object, name, &lookup);
1158 // If we did not find a property, check if we need to throw an exception.
1159 if (!lookup.IsProperty() && IsContextual(object)) {
1160 return ReferenceError("not_defined", name);
1164 UpdateCaches(&lookup, state, object, name);
1167 PropertyAttributes attr;
1168 if (lookup.IsFound() && lookup.type() == INTERCEPTOR) {
1169 // Get the property.
1170 Handle<Object> result =
1171 Object::GetProperty(object, object, &lookup, name, &attr);
1172 RETURN_IF_EMPTY_HANDLE(isolate(), result);
1173 // If the property is not present, check if we need to throw an
1175 if (attr == ABSENT && IsContextual(object)) {
1176 return ReferenceError("not_defined", name);
1181 return object->GetProperty(*object, &lookup, *name, &attr);
1184 // Do not use ICs for objects that require access checks (including
1185 // the global object).
1186 bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded();
1189 Handle<Code> stub = generic_stub();
1190 if (!force_generic_stub) {
1191 if (object->IsString() && key->IsNumber()) {
1192 if (state == UNINITIALIZED) {
1193 stub = string_stub();
1195 } else if (object->IsJSObject()) {
1196 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1197 if (receiver->elements()->map() ==
1198 isolate()->heap()->non_strict_arguments_elements_map()) {
1199 stub = non_strict_arguments_stub();
1200 } else if (receiver->HasIndexedInterceptor()) {
1201 stub = indexed_interceptor_stub();
1202 } else if (key->IsSmi() && (target() != *non_strict_arguments_stub())) {
1203 stub = ComputeStub(receiver, LOAD, kNonStrictMode, stub);
1207 TRACE_GENERIC_IC("KeyedLoadIC", "force generic");
1209 if (!stub.is_null()) set_target(*stub);
1212 TRACE_IC("KeyedLoadIC", key, state, target());
1214 // Get the property.
1215 return Runtime::GetObjectProperty(isolate(), object, key);
1219 void KeyedLoadIC::UpdateCaches(LookupResult* lookup,
1221 Handle<Object> object,
1222 Handle<String> name) {
1223 // Bail out if we didn't find a result.
1224 if (!lookup->IsProperty() || !lookup->IsCacheable()) return;
1226 if (!object->IsJSObject()) return;
1227 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1229 if (HasNormalObjectsInPrototypeChain(isolate(), lookup, *object)) return;
1231 // Compute the code stub for this load.
1234 if (state == UNINITIALIZED) {
1235 // This is the first time we execute this inline cache.
1236 // Set the target to the pre monomorphic stub to delay
1237 // setting the monomorphic state.
1238 code = pre_monomorphic_stub();
1240 // Compute a monomorphic stub.
1241 Handle<JSObject> holder(lookup->holder());
1242 switch (lookup->type()) {
1244 code = isolate()->stub_cache()->ComputeKeyedLoadField(
1245 name, receiver, holder, lookup->GetFieldIndex());
1247 case CONSTANT_FUNCTION: {
1248 Handle<JSFunction> constant(lookup->GetConstantFunction());
1249 code = isolate()->stub_cache()->ComputeKeyedLoadConstant(
1250 name, receiver, holder, constant);
1254 Handle<Object> callback_object(lookup->GetCallbackObject());
1255 if (!callback_object->IsAccessorInfo()) return;
1256 Handle<AccessorInfo> callback =
1257 Handle<AccessorInfo>::cast(callback_object);
1258 if (v8::ToCData<Address>(callback->getter()) == 0) return;
1259 code = isolate()->stub_cache()->ComputeKeyedLoadCallback(
1260 name, receiver, holder, callback);
1264 ASSERT(HasInterceptorGetter(lookup->holder()));
1265 code = isolate()->stub_cache()->ComputeKeyedLoadInterceptor(
1266 name, receiver, holder);
1269 // Always rewrite to the generic case so that we do not
1270 // repeatedly try to rewrite.
1271 code = generic_stub();
1276 // Patch the call site depending on the state of the cache. Make
1277 // sure to always rewrite from monomorphic to megamorphic.
1278 ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE);
1279 if (state == UNINITIALIZED || state == PREMONOMORPHIC) {
1281 } else if (state == MONOMORPHIC) {
1282 set_target(*megamorphic_stub());
1285 TRACE_IC("KeyedLoadIC", name, state, target());
1289 static bool StoreICableLookup(LookupResult* lookup) {
1290 // Bail out if we didn't find a result.
1291 if (!lookup->IsFound() || lookup->type() == NULL_DESCRIPTOR) return false;
1293 // Bail out if inline caching is not allowed.
1294 if (!lookup->IsCacheable()) return false;
1296 // If the property is read-only, we leave the IC in its current state.
1297 if (lookup->IsReadOnly()) return false;
1303 static bool LookupForWrite(Handle<JSObject> receiver,
1304 Handle<String> name,
1305 LookupResult* lookup) {
1306 receiver->LocalLookup(*name, lookup);
1307 if (!StoreICableLookup(lookup)) {
1311 if (lookup->type() == INTERCEPTOR &&
1312 receiver->GetNamedInterceptor()->setter()->IsUndefined()) {
1313 receiver->LocalLookupRealNamedProperty(*name, lookup);
1314 return StoreICableLookup(lookup);
1321 MaybeObject* StoreIC::Store(State state,
1322 StrictModeFlag strict_mode,
1323 Handle<Object> object,
1324 Handle<String> name,
1325 Handle<Object> value) {
1326 if (!object->IsJSObject()) {
1328 if (object->IsJSProxy()) {
1329 return JSProxy::cast(*object)->
1330 SetProperty(*name, *value, NONE, strict_mode);
1333 // If the object is undefined or null it's illegal to try to set any
1334 // properties on it; throw a TypeError in that case.
1335 if (object->IsUndefined() || object->IsNull()) {
1336 return TypeError("non_object_property_store", object, name);
1339 // The length property of string values is read-only. Throw in strict mode.
1340 if (strict_mode == kStrictMode && object->IsString() &&
1341 name->Equals(isolate()->heap()->length_symbol())) {
1342 return TypeError("strict_read_only_property", object, name);
1344 // Ignore other stores where the receiver is not a JSObject.
1345 // TODO(1475): Must check prototype chains of object wrappers.
1349 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1351 // Check if the given name is an array index.
1353 if (name->AsArrayIndex(&index)) {
1354 Handle<Object> result =
1355 JSObject::SetElement(receiver, index, value, NONE, strict_mode);
1356 RETURN_IF_EMPTY_HANDLE(isolate(), result);
1360 // Use specialized code for setting the length of arrays with fast
1361 // properties. Slow properties might indicate redefinition of the
1363 if (receiver->IsJSArray() &&
1364 name->Equals(isolate()->heap()->length_symbol()) &&
1365 Handle<JSArray>::cast(receiver)->AllowsSetElementsLength() &&
1366 receiver->HasFastProperties()) {
1368 if (FLAG_trace_ic) PrintF("[StoreIC : +#length /array]\n");
1370 Handle<Code> stub = (strict_mode == kStrictMode)
1371 ? isolate()->builtins()->StoreIC_ArrayLength_Strict()
1372 : isolate()->builtins()->StoreIC_ArrayLength();
1374 return receiver->SetProperty(*name, *value, NONE, strict_mode);
1377 // Lookup the property locally in the receiver.
1378 if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) {
1379 LookupResult lookup(isolate());
1381 if (LookupForWrite(receiver, name, &lookup)) {
1382 // Generate a stub for this store.
1383 UpdateCaches(&lookup, state, strict_mode, receiver, name, value);
1385 // Strict mode doesn't allow setting non-existent global property
1386 // or an assignment to a read only property.
1387 if (strict_mode == kStrictMode) {
1388 if (lookup.IsProperty() && lookup.IsReadOnly()) {
1389 return TypeError("strict_read_only_property", object, name);
1390 } else if (IsContextual(object)) {
1391 return ReferenceError("not_defined", name);
1397 if (receiver->IsJSGlobalProxy()) {
1398 // TODO(ulan): find out why we patch this site even with --no-use-ic
1399 // Generate a generic stub that goes to the runtime when we see a global
1400 // proxy as receiver.
1401 Handle<Code> stub = (strict_mode == kStrictMode)
1402 ? global_proxy_stub_strict()
1403 : global_proxy_stub();
1404 if (target() != *stub) {
1406 TRACE_IC("StoreIC", name, state, target());
1410 // Set the property.
1411 return receiver->SetProperty(*name, *value, NONE, strict_mode);
1415 void StoreIC::UpdateCaches(LookupResult* lookup,
1417 StrictModeFlag strict_mode,
1418 Handle<JSObject> receiver,
1419 Handle<String> name,
1420 Handle<Object> value) {
1421 ASSERT(!receiver->IsJSGlobalProxy());
1422 ASSERT(StoreICableLookup(lookup));
1423 // These are not cacheable, so we never see such LookupResults here.
1424 ASSERT(lookup->type() != HANDLER);
1425 // We get only called for properties or transitions, see StoreICableLookup.
1426 ASSERT(lookup->type() != NULL_DESCRIPTOR);
1428 // If the property has a non-field type allowing map transitions
1429 // where there is extra room in the object, we leave the IC in its
1431 PropertyType type = lookup->type();
1433 // Compute the code stub for this store; used for rewriting to
1434 // monomorphic state and making sure that the code stub is in the
1439 code = isolate()->stub_cache()->ComputeStoreField(name,
1441 lookup->GetFieldIndex(),
1442 Handle<Map>::null(),
1445 case MAP_TRANSITION: {
1446 if (lookup->GetAttributes() != NONE) return;
1447 Handle<Map> transition(lookup->GetTransitionMap());
1448 int index = transition->PropertyIndexFor(*name);
1449 code = isolate()->stub_cache()->ComputeStoreField(
1450 name, receiver, index, transition, strict_mode);
1454 if (receiver->IsGlobalObject()) {
1455 // The stub generated for the global object picks the value directly
1456 // from the property cell. So the property must be directly on the
1458 Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver);
1459 Handle<JSGlobalPropertyCell> cell(global->GetPropertyCell(lookup));
1460 code = isolate()->stub_cache()->ComputeStoreGlobal(
1461 name, global, cell, strict_mode);
1463 if (lookup->holder() != *receiver) return;
1464 code = isolate()->stub_cache()->ComputeStoreNormal(strict_mode);
1468 Handle<Object> callback_object(lookup->GetCallbackObject());
1469 if (!callback_object->IsAccessorInfo()) return;
1470 Handle<AccessorInfo> callback =
1471 Handle<AccessorInfo>::cast(callback_object);
1472 if (v8::ToCData<Address>(callback->setter()) == 0) return;
1473 code = isolate()->stub_cache()->ComputeStoreCallback(
1474 name, receiver, callback, strict_mode);
1478 ASSERT(!receiver->GetNamedInterceptor()->setter()->IsUndefined());
1479 code = isolate()->stub_cache()->ComputeStoreInterceptor(
1480 name, receiver, strict_mode);
1482 case CONSTANT_FUNCTION:
1483 case CONSTANT_TRANSITION:
1484 case ELEMENTS_TRANSITION:
1487 case NULL_DESCRIPTOR:
1492 // Patch the call site depending on the state of the cache.
1493 if (state == UNINITIALIZED || state == MONOMORPHIC_PROTOTYPE_FAILURE) {
1495 } else if (state == MONOMORPHIC) {
1496 // Only move to megamorphic if the target changes.
1497 if (target() != *code) {
1498 set_target((strict_mode == kStrictMode)
1499 ? megamorphic_stub_strict()
1500 : megamorphic_stub());
1502 } else if (state == MEGAMORPHIC) {
1503 // Update the stub cache.
1504 isolate()->stub_cache()->Set(*name, receiver->map(), *code);
1507 TRACE_IC("StoreIC", name, state, target());
1511 static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps,
1512 Handle<Map> new_receiver_map) {
1513 ASSERT(!new_receiver_map.is_null());
1514 for (int current = 0; current < receiver_maps->length(); ++current) {
1515 if (!receiver_maps->at(current).is_null() &&
1516 receiver_maps->at(current).is_identical_to(new_receiver_map)) {
1520 receiver_maps->Add(new_receiver_map);
1525 void KeyedIC::GetReceiverMapsForStub(Handle<Code> stub,
1526 MapHandleList* result) {
1527 ASSERT(stub->is_inline_cache_stub());
1528 if (!string_stub().is_null() && stub.is_identical_to(string_stub())) {
1529 return result->Add(isolate()->factory()->string_map());
1530 } else if (stub->is_keyed_load_stub() || stub->is_keyed_store_stub()) {
1531 if (stub->ic_state() == MONOMORPHIC) {
1532 result->Add(Handle<Map>(stub->FindFirstMap()));
1534 ASSERT(stub->ic_state() == MEGAMORPHIC);
1535 AssertNoAllocation no_allocation;
1536 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
1537 for (RelocIterator it(*stub, mask); !it.done(); it.next()) {
1538 RelocInfo* info = it.rinfo();
1539 Handle<Object> object(info->target_object());
1540 ASSERT(object->IsMap());
1541 AddOneReceiverMapIfMissing(result, Handle<Map>::cast(object));
1548 Handle<Code> KeyedIC::ComputeStub(Handle<JSObject> receiver,
1550 StrictModeFlag strict_mode,
1551 Handle<Code> generic_stub) {
1552 State ic_state = target()->ic_state();
1553 KeyedAccessGrowMode grow_mode = IsGrowStubKind(stub_kind)
1554 ? ALLOW_JSARRAY_GROWTH
1555 : DO_NOT_ALLOW_JSARRAY_GROWTH;
1557 // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS
1558 // via megamorphic stubs, since they don't have a map in their relocation info
1559 // and so the stubs can't be harvested for the object needed for a map check.
1560 if (target()->type() != NORMAL) {
1561 TRACE_GENERIC_IC("KeyedIC", "non-NORMAL target type");
1562 return generic_stub;
1565 bool monomorphic = false;
1566 MapHandleList target_receiver_maps;
1567 if (ic_state != UNINITIALIZED && ic_state != PREMONOMORPHIC) {
1568 GetReceiverMapsForStub(Handle<Code>(target()), &target_receiver_maps);
1570 if (!IsTransitionStubKind(stub_kind)) {
1571 if (ic_state == UNINITIALIZED || ic_state == PREMONOMORPHIC) {
1574 if (ic_state == MONOMORPHIC) {
1575 // The first time a receiver is seen that is a transitioned version of
1576 // the previous monomorphic receiver type, assume the new ElementsKind
1577 // is the monomorphic type. This benefits global arrays that only
1578 // transition once, and all call sites accessing them are faster if they
1579 // remain monomorphic. If this optimistic assumption is not true, the IC
1580 // will miss again and it will become polymorphic and support both the
1581 // untransitioned and transitioned maps.
1582 monomorphic = IsMoreGeneralElementsKindTransition(
1583 target_receiver_maps.at(0)->elements_kind(),
1584 receiver->GetElementsKind());
1590 return ComputeMonomorphicStub(
1591 receiver, stub_kind, strict_mode, generic_stub);
1593 ASSERT(target() != *generic_stub);
1595 // Determine the list of receiver maps that this call site has seen,
1596 // adding the map that was just encountered.
1597 Handle<Map> receiver_map(receiver->map());
1599 AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map);
1600 if (IsTransitionStubKind(stub_kind)) {
1601 Handle<Map> new_map = ComputeTransitionedMap(receiver, stub_kind);
1602 map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps, new_map);
1605 // If the miss wasn't due to an unseen map, a polymorphic stub
1606 // won't help, use the generic stub.
1607 TRACE_GENERIC_IC("KeyedIC", "same map added twice");
1608 return generic_stub;
1611 // If the maximum number of receiver maps has been exceeded, use the generic
1612 // version of the IC.
1613 if (target_receiver_maps.length() > kMaxKeyedPolymorphism) {
1614 TRACE_GENERIC_IC("KeyedIC", "max polymorph exceeded");
1615 return generic_stub;
1618 if ((Code::GetKeyedAccessGrowMode(target()->extra_ic_state()) ==
1619 ALLOW_JSARRAY_GROWTH)) {
1620 grow_mode = ALLOW_JSARRAY_GROWTH;
1623 Handle<PolymorphicCodeCache> cache =
1624 isolate()->factory()->polymorphic_code_cache();
1625 Code::ExtraICState extra_state = Code::ComputeExtraICState(grow_mode,
1627 Code::Flags flags = Code::ComputeFlags(kind(), MEGAMORPHIC, extra_state);
1628 Handle<Object> probe = cache->Lookup(&target_receiver_maps, flags);
1629 if (probe->IsCode()) return Handle<Code>::cast(probe);
1632 ComputePolymorphicStub(&target_receiver_maps, strict_mode, grow_mode);
1633 PolymorphicCodeCache::Update(cache, &target_receiver_maps, flags, stub);
1638 Handle<Code> KeyedIC::ComputeMonomorphicStubWithoutMapCheck(
1639 Handle<Map> receiver_map,
1640 StrictModeFlag strict_mode,
1641 KeyedAccessGrowMode grow_mode) {
1642 if ((receiver_map->instance_type() & kNotStringTag) == 0) {
1643 ASSERT(!string_stub().is_null());
1644 return string_stub();
1646 ASSERT(receiver_map->has_dictionary_elements() ||
1647 receiver_map->has_fast_elements() ||
1648 receiver_map->has_fast_smi_only_elements() ||
1649 receiver_map->has_fast_double_elements() ||
1650 receiver_map->has_external_array_elements());
1651 bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE;
1652 return GetElementStubWithoutMapCheck(is_js_array,
1653 receiver_map->elements_kind(),
1659 Handle<Code> KeyedIC::ComputeMonomorphicStub(Handle<JSObject> receiver,
1661 StrictModeFlag strict_mode,
1662 Handle<Code> generic_stub) {
1663 if (receiver->HasFastElements() ||
1664 receiver->HasFastSmiOnlyElements() ||
1665 receiver->HasExternalArrayElements() ||
1666 receiver->HasFastDoubleElements() ||
1667 receiver->HasDictionaryElements()) {
1668 return isolate()->stub_cache()->ComputeKeyedLoadOrStoreElement(
1669 receiver, stub_kind, strict_mode);
1671 return generic_stub;
1676 Handle<Map> KeyedIC::ComputeTransitionedMap(Handle<JSObject> receiver,
1677 StubKind stub_kind) {
1678 switch (stub_kind) {
1679 case KeyedIC::STORE_TRANSITION_SMI_TO_OBJECT:
1680 case KeyedIC::STORE_TRANSITION_DOUBLE_TO_OBJECT:
1681 case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT:
1682 case KeyedIC::STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT:
1683 return JSObject::GetElementsTransitionMap(receiver, FAST_ELEMENTS);
1685 case KeyedIC::STORE_TRANSITION_SMI_TO_DOUBLE:
1686 case KeyedIC::STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE:
1687 return JSObject::GetElementsTransitionMap(receiver, FAST_DOUBLE_ELEMENTS);
1691 return Handle<Map>::null();
1696 Handle<Code> KeyedStoreIC::GetElementStubWithoutMapCheck(
1698 ElementsKind elements_kind,
1699 KeyedAccessGrowMode grow_mode) {
1700 return KeyedStoreElementStub(is_js_array, elements_kind, grow_mode).GetCode();
1704 Handle<Code> KeyedStoreIC::ComputePolymorphicStub(
1705 MapHandleList* receiver_maps,
1706 StrictModeFlag strict_mode,
1707 KeyedAccessGrowMode grow_mode) {
1708 // Collect MONOMORPHIC stubs for all target_receiver_maps.
1709 CodeHandleList handler_ics(receiver_maps->length());
1710 MapHandleList transitioned_maps(receiver_maps->length());
1711 for (int i = 0; i < receiver_maps->length(); ++i) {
1712 Handle<Map> receiver_map(receiver_maps->at(i));
1713 Handle<Code> cached_stub;
1714 Handle<Map> transitioned_map =
1715 receiver_map->FindTransitionedMap(receiver_maps);
1716 if (!transitioned_map.is_null()) {
1717 cached_stub = ElementsTransitionAndStoreStub(
1718 receiver_map->elements_kind(), // original elements_kind
1719 transitioned_map->elements_kind(),
1720 receiver_map->instance_type() == JS_ARRAY_TYPE, // is_js_array
1721 strict_mode, grow_mode).GetCode();
1723 cached_stub = ComputeMonomorphicStubWithoutMapCheck(receiver_map,
1727 ASSERT(!cached_stub.is_null());
1728 handler_ics.Add(cached_stub);
1729 transitioned_maps.Add(transitioned_map);
1731 KeyedStoreStubCompiler compiler(isolate(), strict_mode, grow_mode);
1732 Handle<Code> code = compiler.CompileStorePolymorphic(
1733 receiver_maps, &handler_ics, &transitioned_maps);
1734 isolate()->counters()->keyed_store_polymorphic_stubs()->Increment();
1736 CodeCreateEvent(Logger::KEYED_STORE_MEGAMORPHIC_IC_TAG, *code, 0));
1741 KeyedIC::StubKind KeyedStoreIC::GetStubKind(Handle<JSObject> receiver,
1743 Handle<Object> value) {
1744 ASSERT(key->IsSmi());
1745 int index = Smi::cast(*key)->value();
1746 bool allow_growth = receiver->IsJSArray() &&
1747 JSArray::cast(*receiver)->length()->IsSmi() &&
1748 index >= Smi::cast(JSArray::cast(*receiver)->length())->value();
1751 // Handle growing array in stub if necessary.
1752 if (receiver->HasFastSmiOnlyElements()) {
1753 if (value->IsHeapNumber()) {
1754 return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE;
1756 if (value->IsHeapObject()) {
1757 return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT;
1759 } else if (receiver->HasFastDoubleElements()) {
1760 if (!value->IsSmi() && !value->IsHeapNumber()) {
1761 return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT;
1764 return STORE_AND_GROW_NO_TRANSITION;
1766 // Handle only in-bounds elements accesses.
1767 if (receiver->HasFastSmiOnlyElements()) {
1768 if (value->IsHeapNumber()) {
1769 return STORE_TRANSITION_SMI_TO_DOUBLE;
1770 } else if (value->IsHeapObject()) {
1771 return STORE_TRANSITION_SMI_TO_OBJECT;
1773 } else if (receiver->HasFastDoubleElements()) {
1774 if (!value->IsSmi() && !value->IsHeapNumber()) {
1775 return STORE_TRANSITION_DOUBLE_TO_OBJECT;
1778 return STORE_NO_TRANSITION;
1783 MaybeObject* KeyedStoreIC::Store(State state,
1784 StrictModeFlag strict_mode,
1785 Handle<Object> object,
1787 Handle<Object> value,
1788 bool force_generic) {
1789 // Check for values that can be converted into a symbol directly or
1790 // is representable as a smi.
1791 key = TryConvertKey(key, isolate());
1793 if (key->IsSymbol()) {
1794 Handle<String> name = Handle<String>::cast(key);
1797 if (object->IsJSProxy()) {
1798 return JSProxy::cast(*object)->SetProperty(
1799 *name, *value, NONE, strict_mode);
1802 // If the object is undefined or null it's illegal to try to set any
1803 // properties on it; throw a TypeError in that case.
1804 if (object->IsUndefined() || object->IsNull()) {
1805 return TypeError("non_object_property_store", object, name);
1808 // Ignore stores where the receiver is not a JSObject.
1809 if (!object->IsJSObject()) return *value;
1810 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1812 // Check if the given name is an array index.
1814 if (name->AsArrayIndex(&index)) {
1815 Handle<Object> result =
1816 JSObject::SetElement(receiver, index, value, NONE, strict_mode);
1817 RETURN_IF_EMPTY_HANDLE(isolate(), result);
1821 // Update inline cache and stub cache.
1822 if (FLAG_use_ic && !receiver->IsJSGlobalProxy()) {
1823 LookupResult lookup(isolate());
1824 if (LookupForWrite(receiver, name, &lookup)) {
1825 UpdateCaches(&lookup, state, strict_mode, receiver, name, value);
1829 // Set the property.
1830 return receiver->SetProperty(*name, *value, NONE, strict_mode);
1833 // Do not use ICs for objects that require access checks (including
1834 // the global object).
1835 bool use_ic = FLAG_use_ic && !object->IsAccessCheckNeeded();
1836 ASSERT(!(use_ic && object->IsJSGlobalProxy()));
1839 Handle<Code> stub = (strict_mode == kStrictMode)
1840 ? generic_stub_strict()
1842 if (object->IsJSObject()) {
1843 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1844 if (receiver->elements()->map() ==
1845 isolate()->heap()->non_strict_arguments_elements_map()) {
1846 stub = non_strict_arguments_stub();
1847 } else if (!force_generic) {
1848 if (key->IsSmi() && (target() != *non_strict_arguments_stub())) {
1849 StubKind stub_kind = GetStubKind(receiver, key, value);
1850 stub = ComputeStub(receiver, stub_kind, strict_mode, stub);
1853 TRACE_GENERIC_IC("KeyedStoreIC", "force generic");
1856 if (!stub.is_null()) set_target(*stub);
1859 TRACE_IC("KeyedStoreIC", key, state, target());
1861 // Set the property.
1862 return Runtime::SetObjectProperty(
1863 isolate(), object , key, value, NONE, strict_mode);
1867 void KeyedStoreIC::UpdateCaches(LookupResult* lookup,
1869 StrictModeFlag strict_mode,
1870 Handle<JSObject> receiver,
1871 Handle<String> name,
1872 Handle<Object> value) {
1873 ASSERT(!receiver->IsJSGlobalProxy());
1874 ASSERT(StoreICableLookup(lookup));
1875 // These are not cacheable, so we never see such LookupResults here.
1876 ASSERT(lookup->type() != HANDLER);
1877 // We get only called for properties or transitions, see StoreICableLookup.
1878 ASSERT(lookup->type() != NULL_DESCRIPTOR);
1880 // If the property has a non-field type allowing map transitions
1881 // where there is extra room in the object, we leave the IC in its
1883 PropertyType type = lookup->type();
1885 // Compute the code stub for this store; used for rewriting to
1886 // monomorphic state and making sure that the code stub is in the
1892 code = isolate()->stub_cache()->ComputeKeyedStoreField(
1893 name, receiver, lookup->GetFieldIndex(),
1894 Handle<Map>::null(), strict_mode);
1896 case MAP_TRANSITION:
1897 if (lookup->GetAttributes() == NONE) {
1898 Handle<Map> transition(lookup->GetTransitionMap());
1899 int index = transition->PropertyIndexFor(*name);
1900 code = isolate()->stub_cache()->ComputeKeyedStoreField(
1901 name, receiver, index, transition, strict_mode);
1906 case CONSTANT_FUNCTION:
1909 case CONSTANT_TRANSITION:
1910 case ELEMENTS_TRANSITION:
1911 // Always rewrite to the generic case so that we do not
1912 // repeatedly try to rewrite.
1913 code = (strict_mode == kStrictMode)
1914 ? generic_stub_strict()
1918 case NULL_DESCRIPTOR:
1923 ASSERT(!code.is_null());
1925 // Patch the call site depending on the state of the cache. Make
1926 // sure to always rewrite from monomorphic to megamorphic.
1927 ASSERT(state != MONOMORPHIC_PROTOTYPE_FAILURE);
1928 if (state == UNINITIALIZED || state == PREMONOMORPHIC) {
1930 } else if (state == MONOMORPHIC) {
1931 set_target((strict_mode == kStrictMode)
1932 ? *megamorphic_stub_strict()
1933 : *megamorphic_stub());
1936 TRACE_IC("KeyedStoreIC", name, state, target());
1943 // ----------------------------------------------------------------------------
1944 // Static IC stub generators.
1947 // Used from ic-<arch>.cc.
1948 RUNTIME_FUNCTION(MaybeObject*, CallIC_Miss) {
1949 HandleScope scope(isolate);
1950 ASSERT(args.length() == 2);
1952 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
1953 Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state();
1954 MaybeObject* maybe_result = ic.LoadFunction(state,
1957 args.at<String>(1));
1958 // Result could be a function or a failure.
1959 JSFunction* raw_function = NULL;
1960 if (!maybe_result->To(&raw_function)) return maybe_result;
1962 // The first time the inline cache is updated may be the first time the
1963 // function it references gets called. If the function is lazily compiled
1964 // then the first call will trigger a compilation. We check for this case
1965 // and we do the compilation immediately, instead of waiting for the stub
1966 // currently attached to the JSFunction object to trigger compilation.
1967 if (raw_function->is_compiled()) return raw_function;
1969 Handle<JSFunction> function(raw_function);
1970 JSFunction::CompileLazy(function, CLEAR_EXCEPTION);
1975 // Used from ic-<arch>.cc.
1976 RUNTIME_FUNCTION(MaybeObject*, KeyedCallIC_Miss) {
1977 HandleScope scope(isolate);
1978 ASSERT(args.length() == 2);
1979 KeyedCallIC ic(isolate);
1980 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
1981 MaybeObject* maybe_result =
1982 ic.LoadFunction(state, args.at<Object>(0), args.at<Object>(1));
1983 // Result could be a function or a failure.
1984 JSFunction* raw_function = NULL;
1985 if (!maybe_result->To(&raw_function)) return maybe_result;
1987 if (raw_function->is_compiled()) return raw_function;
1989 Handle<JSFunction> function(raw_function);
1990 JSFunction::CompileLazy(function, CLEAR_EXCEPTION);
1995 // Used from ic-<arch>.cc.
1996 RUNTIME_FUNCTION(MaybeObject*, LoadIC_Miss) {
1997 HandleScope scope(isolate);
1998 ASSERT(args.length() == 2);
2000 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
2001 return ic.Load(state, args.at<Object>(0), args.at<String>(1));
2005 // Used from ic-<arch>.cc
2006 RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_Miss) {
2007 HandleScope scope(isolate);
2008 ASSERT(args.length() == 2);
2009 KeyedLoadIC ic(isolate);
2010 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
2011 return ic.Load(state, args.at<Object>(0), args.at<Object>(1), false);
2015 RUNTIME_FUNCTION(MaybeObject*, KeyedLoadIC_MissForceGeneric) {
2016 HandleScope scope(isolate);
2017 ASSERT(args.length() == 2);
2018 KeyedLoadIC ic(isolate);
2019 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
2020 return ic.Load(state, args.at<Object>(0), args.at<Object>(1), true);
2024 // Used from ic-<arch>.cc.
2025 RUNTIME_FUNCTION(MaybeObject*, StoreIC_Miss) {
2027 ASSERT(args.length() == 3);
2028 StoreIC ic(isolate);
2029 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
2030 Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state();
2031 return ic.Store(state,
2032 Code::GetStrictMode(extra_ic_state),
2035 args.at<Object>(2));
2039 RUNTIME_FUNCTION(MaybeObject*, StoreIC_ArrayLength) {
2040 NoHandleAllocation nha;
2042 ASSERT(args.length() == 2);
2043 JSArray* receiver = JSArray::cast(args[0]);
2044 Object* len = args[1];
2046 // The generated code should filter out non-Smis before we get here.
2047 ASSERT(len->IsSmi());
2050 // The length property has to be a writable callback property.
2051 LookupResult debug_lookup(isolate);
2052 receiver->LocalLookup(isolate->heap()->length_symbol(), &debug_lookup);
2053 ASSERT(debug_lookup.type() == CALLBACKS && !debug_lookup.IsReadOnly());
2057 { MaybeObject* maybe_result = receiver->SetElementsLength(len);
2058 if (!maybe_result->ToObject(&result)) return maybe_result;
2064 // Extend storage is called in a store inline cache when
2065 // it is necessary to extend the properties array of a
2067 RUNTIME_FUNCTION(MaybeObject*, SharedStoreIC_ExtendStorage) {
2068 NoHandleAllocation na;
2069 ASSERT(args.length() == 3);
2071 // Convert the parameters
2072 JSObject* object = JSObject::cast(args[0]);
2073 Map* transition = Map::cast(args[1]);
2074 Object* value = args[2];
2076 // Check the object has run out out property space.
2077 ASSERT(object->HasFastProperties());
2078 ASSERT(object->map()->unused_property_fields() == 0);
2080 // Expand the properties array.
2081 FixedArray* old_storage = object->properties();
2082 int new_unused = transition->unused_property_fields();
2083 int new_size = old_storage->length() + new_unused + 1;
2085 { MaybeObject* maybe_result = old_storage->CopySize(new_size);
2086 if (!maybe_result->ToObject(&result)) return maybe_result;
2088 FixedArray* new_storage = FixedArray::cast(result);
2089 new_storage->set(old_storage->length(), value);
2091 // Set the new property value and do the map transition.
2092 object->set_properties(new_storage);
2093 object->set_map(transition);
2095 // Return the stored value.
2100 // Used from ic-<arch>.cc.
2101 RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Miss) {
2102 HandleScope scope(isolate);
2103 ASSERT(args.length() == 3);
2104 KeyedStoreIC ic(isolate);
2105 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
2106 Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state();
2107 return ic.Store(state,
2108 Code::GetStrictMode(extra_ic_state),
2116 RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_Slow) {
2117 NoHandleAllocation na;
2118 ASSERT(args.length() == 3);
2119 KeyedStoreIC ic(isolate);
2120 Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state();
2121 Handle<Object> object = args.at<Object>(0);
2122 Handle<Object> key = args.at<Object>(1);
2123 Handle<Object> value = args.at<Object>(2);
2124 StrictModeFlag strict_mode = Code::GetStrictMode(extra_ic_state);
2125 return Runtime::SetObjectProperty(isolate,
2134 RUNTIME_FUNCTION(MaybeObject*, KeyedStoreIC_MissForceGeneric) {
2135 HandleScope scope(isolate);
2136 ASSERT(args.length() == 3);
2137 KeyedStoreIC ic(isolate);
2138 IC::State state = IC::StateFrom(ic.target(), args[0], args[1]);
2139 Code::ExtraICState extra_ic_state = ic.target()->extra_ic_state();
2140 return ic.Store(state,
2141 Code::GetStrictMode(extra_ic_state),
2149 void UnaryOpIC::patch(Code* code) {
2154 const char* UnaryOpIC::GetName(TypeInfo type_info) {
2155 switch (type_info) {
2156 case UNINITIALIZED: return "Uninitialized";
2157 case SMI: return "Smi";
2158 case HEAP_NUMBER: return "HeapNumbers";
2159 case GENERIC: return "Generic";
2160 default: return "Invalid";
2165 UnaryOpIC::State UnaryOpIC::ToState(TypeInfo type_info) {
2166 switch (type_info) {
2168 return ::v8::internal::UNINITIALIZED;
2176 return ::v8::internal::UNINITIALIZED;
2179 UnaryOpIC::TypeInfo UnaryOpIC::GetTypeInfo(Handle<Object> operand) {
2180 ::v8::internal::TypeInfo operand_type =
2181 ::v8::internal::TypeInfo::TypeFromValue(operand);
2182 if (operand_type.IsSmi()) {
2184 } else if (operand_type.IsNumber()) {
2192 UnaryOpIC::TypeInfo UnaryOpIC::ComputeNewType(
2193 UnaryOpIC::TypeInfo current_type,
2194 UnaryOpIC::TypeInfo previous_type) {
2195 switch (previous_type) {
2196 case UnaryOpIC::UNINITIALIZED:
2197 return current_type;
2198 case UnaryOpIC::SMI:
2199 return (current_type == UnaryOpIC::GENERIC)
2200 ? UnaryOpIC::GENERIC
2201 : UnaryOpIC::HEAP_NUMBER;
2202 case UnaryOpIC::HEAP_NUMBER:
2203 return UnaryOpIC::GENERIC;
2204 case UnaryOpIC::GENERIC:
2205 // We should never do patching if we are in GENERIC state.
2207 return UnaryOpIC::GENERIC;
2210 return UnaryOpIC::GENERIC;
2214 void BinaryOpIC::patch(Code* code) {
2219 const char* BinaryOpIC::GetName(TypeInfo type_info) {
2220 switch (type_info) {
2221 case UNINITIALIZED: return "Uninitialized";
2222 case SMI: return "SMI";
2223 case INT32: return "Int32s";
2224 case HEAP_NUMBER: return "HeapNumbers";
2225 case ODDBALL: return "Oddball";
2226 case BOTH_STRING: return "BothStrings";
2227 case STRING: return "Strings";
2228 case GENERIC: return "Generic";
2229 default: return "Invalid";
2234 BinaryOpIC::State BinaryOpIC::ToState(TypeInfo type_info) {
2235 switch (type_info) {
2237 return ::v8::internal::UNINITIALIZED;
2249 return ::v8::internal::UNINITIALIZED;
2253 BinaryOpIC::TypeInfo BinaryOpIC::JoinTypes(BinaryOpIC::TypeInfo x,
2254 BinaryOpIC::TypeInfo y) {
2255 if (x == UNINITIALIZED) return y;
2256 if (y == UNINITIALIZED) return x;
2257 if (x == y) return x;
2258 if (x == BOTH_STRING && y == STRING) return STRING;
2259 if (x == STRING && y == BOTH_STRING) return STRING;
2260 if (x == STRING || x == BOTH_STRING || y == STRING || y == BOTH_STRING) {
2263 if (x > y) return x;
2268 BinaryOpIC::TypeInfo BinaryOpIC::GetTypeInfo(Handle<Object> left,
2269 Handle<Object> right) {
2270 ::v8::internal::TypeInfo left_type =
2271 ::v8::internal::TypeInfo::TypeFromValue(left);
2272 ::v8::internal::TypeInfo right_type =
2273 ::v8::internal::TypeInfo::TypeFromValue(right);
2275 if (left_type.IsSmi() && right_type.IsSmi()) {
2279 if (left_type.IsInteger32() && right_type.IsInteger32()) {
2280 // Platforms with 32-bit Smis have no distinct INT32 type.
2281 if (kSmiValueSize == 32) return SMI;
2285 if (left_type.IsNumber() && right_type.IsNumber()) {
2289 // Patching for fast string ADD makes sense even if only one of the
2290 // arguments is a string.
2291 if (left_type.IsString()) {
2292 return right_type.IsString() ? BOTH_STRING : STRING;
2293 } else if (right_type.IsString()) {
2297 // Check for oddball objects.
2298 if (left->IsUndefined() && right->IsNumber()) return ODDBALL;
2299 if (left->IsNumber() && right->IsUndefined()) return ODDBALL;
2305 RUNTIME_FUNCTION(MaybeObject*, UnaryOp_Patch) {
2306 ASSERT(args.length() == 4);
2308 HandleScope scope(isolate);
2309 Handle<Object> operand = args.at<Object>(0);
2310 Token::Value op = static_cast<Token::Value>(args.smi_at(1));
2311 UnaryOverwriteMode mode = static_cast<UnaryOverwriteMode>(args.smi_at(2));
2312 UnaryOpIC::TypeInfo previous_type =
2313 static_cast<UnaryOpIC::TypeInfo>(args.smi_at(3));
2315 UnaryOpIC::TypeInfo type = UnaryOpIC::GetTypeInfo(operand);
2316 type = UnaryOpIC::ComputeNewType(type, previous_type);
2318 UnaryOpStub stub(op, mode, type);
2319 Handle<Code> code = stub.GetCode();
2320 if (!code.is_null()) {
2321 if (FLAG_trace_ic) {
2322 PrintF("[UnaryOpIC (%s->%s)#%s]\n",
2323 UnaryOpIC::GetName(previous_type),
2324 UnaryOpIC::GetName(type),
2327 UnaryOpIC ic(isolate);
2331 Handle<JSBuiltinsObject> builtins = Handle<JSBuiltinsObject>(
2332 isolate->thread_local_top()->context_->builtins(), isolate);
2333 Object* builtin = NULL; // Initialization calms down the compiler.
2336 builtin = builtins->javascript_builtin(Builtins::UNARY_MINUS);
2338 case Token::BIT_NOT:
2339 builtin = builtins->javascript_builtin(Builtins::BIT_NOT);
2345 Handle<JSFunction> builtin_function(JSFunction::cast(builtin), isolate);
2347 bool caught_exception;
2348 Handle<Object> result = Execution::Call(builtin_function, operand, 0, NULL,
2350 if (caught_exception) {
2351 return Failure::Exception();
2356 RUNTIME_FUNCTION(MaybeObject*, BinaryOp_Patch) {
2357 ASSERT(args.length() == 5);
2359 HandleScope scope(isolate);
2360 Handle<Object> left = args.at<Object>(0);
2361 Handle<Object> right = args.at<Object>(1);
2362 int key = args.smi_at(2);
2363 Token::Value op = static_cast<Token::Value>(args.smi_at(3));
2364 BinaryOpIC::TypeInfo previous_type =
2365 static_cast<BinaryOpIC::TypeInfo>(args.smi_at(4));
2367 BinaryOpIC::TypeInfo type = BinaryOpIC::GetTypeInfo(left, right);
2368 type = BinaryOpIC::JoinTypes(type, previous_type);
2369 BinaryOpIC::TypeInfo result_type = BinaryOpIC::UNINITIALIZED;
2370 if ((type == BinaryOpIC::STRING || type == BinaryOpIC::BOTH_STRING) &&
2372 type = BinaryOpIC::GENERIC;
2374 if (type == BinaryOpIC::SMI && previous_type == BinaryOpIC::SMI) {
2375 if (op == Token::DIV ||
2378 kSmiValueSize == 32) {
2379 // Arithmetic on two Smi inputs has yielded a heap number.
2380 // That is the only way to get here from the Smi stub.
2381 // With 32-bit Smis, all overflows give heap numbers, but with
2382 // 31-bit Smis, most operations overflow to int32 results.
2383 result_type = BinaryOpIC::HEAP_NUMBER;
2385 // Other operations on SMIs that overflow yield int32s.
2386 result_type = BinaryOpIC::INT32;
2389 if (type == BinaryOpIC::INT32 && previous_type == BinaryOpIC::INT32) {
2390 // We must be here because an operation on two INT32 types overflowed.
2391 result_type = BinaryOpIC::HEAP_NUMBER;
2394 BinaryOpStub stub(key, type, result_type);
2395 Handle<Code> code = stub.GetCode();
2396 if (!code.is_null()) {
2397 if (FLAG_trace_ic) {
2398 PrintF("[BinaryOpIC (%s->(%s->%s))#%s]\n",
2399 BinaryOpIC::GetName(previous_type),
2400 BinaryOpIC::GetName(type),
2401 BinaryOpIC::GetName(result_type),
2404 BinaryOpIC ic(isolate);
2407 // Activate inlined smi code.
2408 if (previous_type == BinaryOpIC::UNINITIALIZED) {
2409 PatchInlinedSmiCode(ic.address(), ENABLE_INLINED_SMI_CHECK);
2413 Handle<JSBuiltinsObject> builtins = Handle<JSBuiltinsObject>(
2414 isolate->thread_local_top()->context_->builtins(), isolate);
2415 Object* builtin = NULL; // Initialization calms down the compiler.
2418 builtin = builtins->javascript_builtin(Builtins::ADD);
2421 builtin = builtins->javascript_builtin(Builtins::SUB);
2424 builtin = builtins->javascript_builtin(Builtins::MUL);
2427 builtin = builtins->javascript_builtin(Builtins::DIV);
2430 builtin = builtins->javascript_builtin(Builtins::MOD);
2432 case Token::BIT_AND:
2433 builtin = builtins->javascript_builtin(Builtins::BIT_AND);
2436 builtin = builtins->javascript_builtin(Builtins::BIT_OR);
2438 case Token::BIT_XOR:
2439 builtin = builtins->javascript_builtin(Builtins::BIT_XOR);
2442 builtin = builtins->javascript_builtin(Builtins::SHR);
2445 builtin = builtins->javascript_builtin(Builtins::SAR);
2448 builtin = builtins->javascript_builtin(Builtins::SHL);
2454 Handle<JSFunction> builtin_function(JSFunction::cast(builtin), isolate);
2456 bool caught_exception;
2457 Handle<Object> builtin_args[] = { right };
2458 Handle<Object> result = Execution::Call(builtin_function,
2460 ARRAY_SIZE(builtin_args),
2463 if (caught_exception) {
2464 return Failure::Exception();
2470 Code* CompareIC::GetRawUninitialized(Token::Value op) {
2471 ICCompareStub stub(op, UNINITIALIZED);
2473 CHECK(stub.FindCodeInCache(&code));
2478 Handle<Code> CompareIC::GetUninitialized(Token::Value op) {
2479 ICCompareStub stub(op, UNINITIALIZED);
2480 return stub.GetCode();
2484 CompareIC::State CompareIC::ComputeState(Code* target) {
2485 int key = target->major_key();
2486 if (key == CodeStub::Compare) return GENERIC;
2487 ASSERT(key == CodeStub::CompareIC);
2488 return static_cast<State>(target->compare_state());
2492 Token::Value CompareIC::ComputeOperation(Code* target) {
2493 ASSERT(target->major_key() == CodeStub::CompareIC);
2494 return static_cast<Token::Value>(target->compare_operation());
2498 const char* CompareIC::GetStateName(State state) {
2500 case UNINITIALIZED: return "UNINITIALIZED";
2501 case SMIS: return "SMIS";
2502 case HEAP_NUMBERS: return "HEAP_NUMBERS";
2503 case OBJECTS: return "OBJECTS";
2504 case KNOWN_OBJECTS: return "OBJECTS";
2505 case SYMBOLS: return "SYMBOLS";
2506 case STRINGS: return "STRINGS";
2507 case GENERIC: return "GENERIC";
2515 CompareIC::State CompareIC::TargetState(State state,
2516 bool has_inlined_smi_code,
2521 if (x->IsSmi() && y->IsSmi()) return SMIS;
2522 if (x->IsNumber() && y->IsNumber()) return HEAP_NUMBERS;
2523 if (Token::IsOrderedRelationalCompareOp(op_)) {
2524 // Ordered comparisons treat undefined as NaN, so the
2525 // HEAP_NUMBER stub will do the right thing.
2526 if ((x->IsNumber() && y->IsUndefined()) ||
2527 (y->IsNumber() && x->IsUndefined())) {
2528 return HEAP_NUMBERS;
2531 if (x->IsSymbol() && y->IsSymbol()) {
2532 // We compare symbols as strings if we need to determine
2533 // the order in a non-equality compare.
2534 return Token::IsEqualityOp(op_) ? SYMBOLS : STRINGS;
2536 if (x->IsString() && y->IsString()) return STRINGS;
2537 if (!Token::IsEqualityOp(op_)) return GENERIC;
2538 if (x->IsJSObject() && y->IsJSObject()) {
2539 if (Handle<JSObject>::cast(x)->map() ==
2540 Handle<JSObject>::cast(y)->map() &&
2541 Token::IsEqualityOp(op_)) {
2542 return KNOWN_OBJECTS;
2549 return has_inlined_smi_code && x->IsNumber() && y->IsNumber()
2553 ASSERT(Token::IsEqualityOp(op_));
2554 return x->IsString() && y->IsString() ? STRINGS : GENERIC;
2567 // Used from ic_<arch>.cc.
2568 RUNTIME_FUNCTION(Code*, CompareIC_Miss) {
2569 NoHandleAllocation na;
2570 ASSERT(args.length() == 3);
2571 CompareIC ic(isolate, static_cast<Token::Value>(args.smi_at(2)));
2572 ic.UpdateCaches(args.at<Object>(0), args.at<Object>(1));
2577 RUNTIME_FUNCTION(MaybeObject*, ToBoolean_Patch) {
2578 ASSERT(args.length() == 3);
2580 HandleScope scope(isolate);
2581 Handle<Object> object = args.at<Object>(0);
2582 Register tos = Register::from_code(args.smi_at(1));
2583 ToBooleanStub::Types old_types(args.smi_at(2));
2585 ToBooleanStub::Types new_types(old_types);
2586 bool to_boolean_value = new_types.Record(object);
2587 old_types.TraceTransition(new_types);
2589 ToBooleanStub stub(tos, new_types);
2590 Handle<Code> code = stub.GetCode();
2591 ToBooleanIC ic(isolate);
2593 return Smi::FromInt(to_boolean_value ? 1 : 0);
2597 void ToBooleanIC::patch(Code* code) {
2602 static const Address IC_utilities[] = {
2603 #define ADDR(name) FUNCTION_ADDR(name),
2610 Address IC::AddressFromUtilityId(IC::UtilityId id) {
2611 return IC_utilities[id];
2615 } } // namespace v8::internal