1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "src/ic/ic-state.h"
9 #include "src/macro-assembler.h"
15 // IC_UTIL_LIST defines all utility functions called from generated
16 // inline caching code. The argument for the macro, ICU, is the function name.
17 #define IC_UTIL_LIST(ICU) \
19 ICU(KeyedLoadIC_Miss) \
21 ICU(CallIC_Customization_Miss) \
24 ICU(SharedStoreIC_ExtendStorage) \
25 ICU(KeyedStoreIC_Miss) \
26 ICU(KeyedStoreIC_Slow) \
27 /* Utilities for IC stubs. */ \
28 ICU(StoreCallbackProperty) \
29 ICU(LoadPropertyWithInterceptorOnly) \
30 ICU(LoadPropertyWithInterceptor) \
31 ICU(LoadElementWithInterceptor) \
32 ICU(StorePropertyWithInterceptor) \
34 ICU(BinaryOpIC_Miss) \
35 ICU(CompareNilIC_Miss) \
39 // IC is the base class for LoadIC, StoreIC, KeyedLoadIC, and KeyedStoreIC.
43 // The ids for utility called from the generated code.
45 #define CONST_NAME(name) k##name,
46 IC_UTIL_LIST(CONST_NAME)
51 // Looks up the address of the named utility.
52 static Address AddressFromUtilityId(UtilityId id);
54 // Alias the inline cache state type to make the IC code more readable.
55 typedef InlineCacheState State;
57 // The IC code is either invoked with no extra frames on the stack
58 // or with a single extra frame for supporting calls.
59 enum FrameDepth { NO_EXTRA_FRAME = 0, EXTRA_CALL_FRAME = 1 };
61 // Construct the IC structure with the given number of extra
62 // JavaScript frames on the stack.
63 IC(FrameDepth depth, Isolate* isolate);
66 State state() const { return state_; }
67 inline Address address() const;
69 // Compute the current IC state based on the target stub, receiver and name.
70 void UpdateState(Handle<Object> receiver, Handle<Object> name);
72 bool IsNameCompatibleWithPrototypeFailure(Handle<Object> name);
73 void MarkPrototypeFailure(Handle<Object> name) {
74 DCHECK(IsNameCompatibleWithPrototypeFailure(name));
75 state_ = PROTOTYPE_FAILURE;
78 // If the stub contains weak maps then this function adds the stub to
79 // the dependent code array of each weak map.
80 static void RegisterWeakMapDependency(Handle<Code> stub);
82 // This function is called when a weak map in the stub is dying,
83 // invalidates the stub by setting maps in it to undefined.
84 static void InvalidateMaps(Code* stub);
86 // Clear the inline cache to initial state.
87 static void Clear(Isolate* isolate, Address address,
88 ConstantPoolArray* constant_pool);
91 bool IsLoadStub() const {
92 return target()->is_load_stub() || target()->is_keyed_load_stub();
95 bool IsStoreStub() const {
96 return target()->is_store_stub() || target()->is_keyed_store_stub();
99 bool IsCallStub() const { return target()->is_call_stub(); }
102 template <class TypeClass>
103 static JSFunction* GetRootConstructor(TypeClass* type,
104 Context* native_context);
105 static inline Handle<Map> GetHandlerCacheHolder(HeapType* type,
106 bool receiver_is_holder,
108 CacheHolderFlag* flag);
109 static inline Handle<Map> GetICCacheHolder(HeapType* type, Isolate* isolate,
110 CacheHolderFlag* flag);
112 static bool IsCleared(Code* code) {
113 InlineCacheState state = code->ic_state();
114 return state == UNINITIALIZED || state == PREMONOMORPHIC;
117 // Utility functions to convert maps to types and back. There are two special
119 // - The heap_number_map is used as a marker which includes heap numbers as
121 // - The oddball map is only used for booleans.
122 static Handle<Map> TypeToMap(HeapType* type, Isolate* isolate);
124 static typename T::TypeHandle MapToType(Handle<Map> map,
125 typename T::Region* region);
127 static Handle<HeapType> CurrentTypeOf(Handle<Object> object,
131 // Get the call-site target; used for determining the state.
132 Handle<Code> target() const { return target_; }
134 Address fp() const { return fp_; }
135 Address pc() const { return *pc_address_; }
136 Isolate* isolate() const { return isolate_; }
138 // Get the shared function info of the caller.
139 SharedFunctionInfo* GetSharedFunctionInfo() const;
140 // Get the code object of the caller.
141 Code* GetCode() const;
142 // Get the original (non-breakpointed) code object of the caller.
143 Code* GetOriginalCode() const;
145 // Set the call-site target.
146 inline void set_target(Code* code);
147 bool is_target_set() { return target_set_; }
149 char TransitionMarkFromState(IC::State state);
150 void TraceIC(const char* type, Handle<Object> name);
151 void TraceIC(const char* type, Handle<Object> name, State old_state,
154 MaybeHandle<Object> TypeError(const char* type, Handle<Object> object,
156 MaybeHandle<Object> ReferenceError(const char* type, Handle<Name> name);
158 // Access the target code for the given IC address.
159 static inline Code* GetTargetAtAddress(Address address,
160 ConstantPoolArray* constant_pool);
161 static inline void SetTargetAtAddress(Address address, Code* target,
162 ConstantPoolArray* constant_pool);
163 static void OnTypeFeedbackChanged(Isolate* isolate, Address address,
164 State old_state, State new_state,
165 bool target_remains_ic_stub);
166 static void PostPatching(Address address, Code* target, Code* old_target);
168 // Compute the handler either by compiling or by retrieving a cached version.
169 Handle<Code> ComputeHandler(LookupIterator* lookup,
170 Handle<Object> value = Handle<Code>::null());
171 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
172 Handle<Object> value,
173 CacheHolderFlag cache_holder) {
175 return Handle<Code>::null();
178 void UpdateMonomorphicIC(Handle<Code> handler, Handle<Name> name);
179 bool UpdatePolymorphicIC(Handle<Name> name, Handle<Code> code);
180 void UpdateMegamorphicCache(HeapType* type, Name* name, Code* code);
182 void CopyICToMegamorphicCache(Handle<Name> name);
183 bool IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map);
184 void PatchCache(Handle<Name> name, Handle<Code> code);
185 Code::Kind kind() const { return kind_; }
186 Code::Kind handler_kind() const {
187 if (kind_ == Code::KEYED_LOAD_IC) return Code::LOAD_IC;
188 DCHECK(kind_ == Code::LOAD_IC || kind_ == Code::STORE_IC ||
189 kind_ == Code::KEYED_STORE_IC);
192 virtual Handle<Code> megamorphic_stub() {
194 return Handle<Code>::null();
197 bool TryRemoveInvalidPrototypeDependentStub(Handle<Object> receiver,
198 Handle<String> name);
200 ExtraICState extra_ic_state() const { return extra_ic_state_; }
201 void set_extra_ic_state(ExtraICState state) { extra_ic_state_ = state; }
203 Handle<HeapType> receiver_type() { return receiver_type_; }
204 void update_receiver_type(Handle<Object> receiver) {
205 receiver_type_ = CurrentTypeOf(receiver, isolate_);
208 void TargetMaps(MapHandleList* list) {
210 for (int i = 0; i < target_maps_.length(); i++) {
211 list->Add(target_maps_.at(i));
215 void TargetTypes(TypeHandleList* list) {
217 for (int i = 0; i < target_maps_.length(); i++) {
218 list->Add(MapToType<HeapType>(target_maps_.at(i), isolate_));
222 Map* FirstTargetMap() {
224 return target_maps_.length() > 0 ? *target_maps_.at(0) : NULL;
228 inline void UpdateTarget();
231 inline Code* raw_target() const;
232 inline ConstantPoolArray* constant_pool() const;
233 inline ConstantPoolArray* raw_constant_pool() const;
235 void FindTargetMaps() {
236 if (target_maps_set_) return;
237 target_maps_set_ = true;
238 if (state_ == MONOMORPHIC) {
239 Map* map = target_->FindFirstMap();
240 if (map != NULL) target_maps_.Add(handle(map));
241 } else if (state_ != UNINITIALIZED && state_ != PREMONOMORPHIC) {
242 target_->FindAllMaps(&target_maps_);
246 // Frame pointer for the frame that uses (calls) the IC.
249 // All access to the program counter of an IC structure is indirect
250 // to make the code GC safe. This feature is crucial since
251 // GetProperty and SetProperty are called and they in turn might
252 // invoke the garbage collector.
253 Address* pc_address_;
257 // The constant pool of the code which originally called the IC (which might
258 // be for the breakpointed copy of the original code).
259 Handle<ConstantPoolArray> raw_constant_pool_;
261 // The original code target that missed.
262 Handle<Code> target_;
266 Handle<HeapType> receiver_type_;
267 MaybeHandle<Code> maybe_handler_;
269 ExtraICState extra_ic_state_;
270 MapHandleList target_maps_;
271 bool target_maps_set_;
273 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
277 // An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
278 // cannot make forward declarations to an enum.
281 explicit IC_Utility(IC::UtilityId id)
282 : address_(IC::AddressFromUtilityId(id)), id_(id) {}
284 Address address() const { return address_; }
286 IC::UtilityId id() const { return id_; }
294 class CallIC : public IC {
296 explicit CallIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
298 void PatchMegamorphic(Handle<Object> function,
299 Handle<TypeFeedbackVector> vector, Handle<Smi> slot);
301 void HandleMiss(Handle<Object> receiver, Handle<Object> function,
302 Handle<TypeFeedbackVector> vector, Handle<Smi> slot);
304 // Returns true if a custom handler was installed.
305 bool DoCustomHandler(Handle<Object> receiver, Handle<Object> function,
306 Handle<TypeFeedbackVector> vector, Handle<Smi> slot,
307 const CallICState& state);
309 // Code generator routines.
310 static Handle<Code> initialize_stub(Isolate* isolate, int argc,
311 CallICState::CallType call_type);
313 static void Clear(Isolate* isolate, Address address, Code* target,
314 ConstantPoolArray* constant_pool);
317 inline IC::State FeedbackToState(Handle<TypeFeedbackVector> vector,
318 Handle<Smi> slot) const;
322 class LoadIC : public IC {
324 static ExtraICState ComputeExtraICState(ContextualMode contextual_mode) {
325 return LoadICState(contextual_mode).GetExtraICState();
328 ContextualMode contextual_mode() const {
329 return LoadICState::GetContextualMode(extra_ic_state());
332 explicit LoadIC(FrameDepth depth, Isolate* isolate) : IC(depth, isolate) {
333 DCHECK(IsLoadStub());
336 // Returns if this IC is for contextual (no explicit receiver)
337 // access to properties.
338 bool IsUndeclaredGlobal(Handle<Object> receiver) {
339 if (receiver->IsGlobalObject()) {
340 return contextual_mode() == CONTEXTUAL;
342 DCHECK(contextual_mode() != CONTEXTUAL);
347 // Code generator routines.
348 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
349 static void GeneratePreMonomorphic(MacroAssembler* masm) {
352 static void GenerateMiss(MacroAssembler* masm);
353 static void GenerateNormal(MacroAssembler* masm);
354 static void GenerateRuntimeGetProperty(MacroAssembler* masm);
356 static Handle<Code> initialize_stub(Isolate* isolate,
357 ExtraICState extra_state);
359 MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
363 inline void set_target(Code* code);
365 Handle<Code> slow_stub() const {
366 if (kind() == Code::LOAD_IC) {
367 return isolate()->builtins()->LoadIC_Slow();
369 DCHECK_EQ(Code::KEYED_LOAD_IC, kind());
370 return isolate()->builtins()->KeyedLoadIC_Slow();
374 virtual Handle<Code> megamorphic_stub() OVERRIDE;
376 // Update the inline cache and the global stub cache based on the
378 void UpdateCaches(LookupIterator* lookup);
380 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
381 Handle<Object> unused,
382 CacheHolderFlag cache_holder);
385 virtual Handle<Code> pre_monomorphic_stub() const;
386 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
387 ExtraICState extra_state);
389 Handle<Code> SimpleFieldLoad(FieldIndex index);
391 static void Clear(Isolate* isolate, Address address, Code* target,
392 ConstantPoolArray* constant_pool);
398 class KeyedLoadIC : public LoadIC {
400 explicit KeyedLoadIC(FrameDepth depth, Isolate* isolate)
401 : LoadIC(depth, isolate) {
402 DCHECK(target()->is_keyed_load_stub());
405 MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
408 // Code generator routines.
409 static void GenerateMiss(MacroAssembler* masm);
410 static void GenerateRuntimeGetProperty(MacroAssembler* masm);
411 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
412 static void GeneratePreMonomorphic(MacroAssembler* masm) {
415 static void GenerateGeneric(MacroAssembler* masm);
416 static void GenerateString(MacroAssembler* masm);
418 // Bit mask to be tested against bit field for the cases when
419 // generic stub should go into slow case.
420 // Access check is necessary explicitly since generic stub does not perform
422 static const int kSlowCaseBitFieldMask =
423 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
425 static Handle<Code> generic_stub(Isolate* isolate);
426 static Handle<Code> pre_monomorphic_stub(Isolate* isolate);
429 Handle<Code> LoadElementStub(Handle<JSObject> receiver);
430 virtual Handle<Code> pre_monomorphic_stub() const {
431 return pre_monomorphic_stub(isolate());
435 Handle<Code> generic_stub() const { return generic_stub(isolate()); }
436 Handle<Code> string_stub() {
437 return isolate()->builtins()->KeyedLoadIC_String();
440 static void Clear(Isolate* isolate, Address address, Code* target,
441 ConstantPoolArray* constant_pool);
447 class StoreIC : public IC {
449 class StrictModeState : public BitField<StrictMode, 1, 1> {};
450 static ExtraICState ComputeExtraICState(StrictMode flag) {
451 return StrictModeState::encode(flag);
453 static StrictMode GetStrictMode(ExtraICState state) {
454 return StrictModeState::decode(state);
457 // For convenience, a statically declared encoding of strict mode extra
459 static const ExtraICState kStrictModeState = 1 << StrictModeState::kShift;
461 StoreIC(FrameDepth depth, Isolate* isolate) : IC(depth, isolate) {
462 DCHECK(IsStoreStub());
465 StrictMode strict_mode() const {
466 return StrictModeState::decode(extra_ic_state());
469 // Code generators for stub routines. Only called once at startup.
470 static void GenerateSlow(MacroAssembler* masm);
471 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
472 static void GeneratePreMonomorphic(MacroAssembler* masm) {
475 static void GenerateMiss(MacroAssembler* masm);
476 static void GenerateMegamorphic(MacroAssembler* masm);
477 static void GenerateNormal(MacroAssembler* masm);
478 static void GenerateRuntimeSetProperty(MacroAssembler* masm,
479 StrictMode strict_mode);
481 static Handle<Code> initialize_stub(Isolate* isolate, StrictMode strict_mode);
483 MUST_USE_RESULT MaybeHandle<Object> Store(
484 Handle<Object> object, Handle<Name> name, Handle<Object> value,
485 JSReceiver::StoreFromKeyed store_mode =
486 JSReceiver::CERTAINLY_NOT_STORE_FROM_KEYED);
488 bool LookupForWrite(LookupIterator* it, Handle<Object> value,
489 JSReceiver::StoreFromKeyed store_mode);
492 virtual Handle<Code> megamorphic_stub() OVERRIDE;
495 Handle<Code> generic_stub() const;
497 Handle<Code> slow_stub() const;
499 virtual Handle<Code> pre_monomorphic_stub() const {
500 return pre_monomorphic_stub(isolate(), strict_mode());
503 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
504 StrictMode strict_mode);
506 // Update the inline cache and the global stub cache based on the
508 void UpdateCaches(LookupIterator* lookup, Handle<Object> value,
509 JSReceiver::StoreFromKeyed store_mode);
510 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
511 Handle<Object> value,
512 CacheHolderFlag cache_holder);
515 inline void set_target(Code* code);
517 static void Clear(Isolate* isolate, Address address, Code* target,
518 ConstantPoolArray* constant_pool);
524 enum KeyedStoreCheckMap { kDontCheckMap, kCheckMap };
527 enum KeyedStoreIncrementLength { kDontIncrementLength, kIncrementLength };
530 class KeyedStoreIC : public StoreIC {
532 // ExtraICState bits (building on IC)
534 class ExtraICStateKeyedAccessStoreMode
535 : public BitField<KeyedAccessStoreMode, 2, 4> {}; // NOLINT
537 static ExtraICState ComputeExtraICState(StrictMode flag,
538 KeyedAccessStoreMode mode) {
539 return StrictModeState::encode(flag) |
540 ExtraICStateKeyedAccessStoreMode::encode(mode);
543 static KeyedAccessStoreMode GetKeyedAccessStoreMode(
544 ExtraICState extra_state) {
545 return ExtraICStateKeyedAccessStoreMode::decode(extra_state);
548 KeyedStoreIC(FrameDepth depth, Isolate* isolate) : StoreIC(depth, isolate) {
549 DCHECK(target()->is_keyed_store_stub());
552 MUST_USE_RESULT MaybeHandle<Object> Store(Handle<Object> object,
554 Handle<Object> value);
556 // Code generators for stub routines. Only called once at startup.
557 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
558 static void GeneratePreMonomorphic(MacroAssembler* masm) {
561 static void GenerateMiss(MacroAssembler* masm);
562 static void GenerateSlow(MacroAssembler* masm);
563 static void GenerateGeneric(MacroAssembler* masm, StrictMode strict_mode);
564 static void GenerateSloppyArguments(MacroAssembler* masm);
567 virtual Handle<Code> pre_monomorphic_stub() const {
568 return pre_monomorphic_stub(isolate(), strict_mode());
570 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
571 StrictMode strict_mode) {
572 if (strict_mode == STRICT) {
573 return isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict();
575 return isolate->builtins()->KeyedStoreIC_PreMonomorphic();
579 Handle<Code> StoreElementStub(Handle<JSObject> receiver,
580 KeyedAccessStoreMode store_mode);
583 inline void set_target(Code* code);
586 Handle<Code> sloppy_arguments_stub() {
587 return isolate()->builtins()->KeyedStoreIC_SloppyArguments();
590 static void Clear(Isolate* isolate, Address address, Code* target,
591 ConstantPoolArray* constant_pool);
593 KeyedAccessStoreMode GetStoreMode(Handle<JSObject> receiver,
594 Handle<Object> key, Handle<Object> value);
596 Handle<Map> ComputeTransitionedMap(Handle<Map> map,
597 KeyedAccessStoreMode store_mode);
603 // Type Recording BinaryOpIC, that records the types of the inputs and outputs.
604 class BinaryOpIC : public IC {
606 explicit BinaryOpIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
608 static Builtins::JavaScript TokenToJSBuiltin(Token::Value op);
610 MaybeHandle<Object> Transition(Handle<AllocationSite> allocation_site,
612 Handle<Object> right) WARN_UNUSED_RESULT;
616 class CompareIC : public IC {
618 CompareIC(Isolate* isolate, Token::Value op)
619 : IC(EXTRA_CALL_FRAME, isolate), op_(op) {}
621 // Update the inline cache for the given operands.
622 Code* UpdateCaches(Handle<Object> x, Handle<Object> y);
624 // Helper function for computing the condition for a compare operation.
625 static Condition ComputeCondition(Token::Value op);
627 // Factory method for getting an uninitialized compare stub.
628 static Handle<Code> GetUninitialized(Isolate* isolate, Token::Value op);
631 static bool HasInlinedSmiCode(Address address);
633 bool strict() const { return op_ == Token::EQ_STRICT; }
634 Condition GetCondition() const { return ComputeCondition(op_); }
636 static Code* GetRawUninitialized(Isolate* isolate, Token::Value op);
638 static void Clear(Isolate* isolate, Address address, Code* target,
639 ConstantPoolArray* constant_pool);
647 class CompareNilIC : public IC {
649 explicit CompareNilIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
651 Handle<Object> CompareNil(Handle<Object> object);
653 static Handle<Code> GetUninitialized();
655 static void Clear(Address address, Code* target,
656 ConstantPoolArray* constant_pool);
658 static Handle<Object> DoCompareNilSlow(Isolate* isolate, NilValue nil,
659 Handle<Object> object);
663 class ToBooleanIC : public IC {
665 explicit ToBooleanIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
667 Handle<Object> ToBoolean(Handle<Object> object);
671 // Helper for BinaryOpIC and CompareIC.
672 enum InlinedSmiCheck { ENABLE_INLINED_SMI_CHECK, DISABLE_INLINED_SMI_CHECK };
673 void PatchInlinedSmiCode(Address address, InlinedSmiCheck check);
675 DECLARE_RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure);
676 DECLARE_RUNTIME_FUNCTION(KeyedStoreIC_MissFromStubFailure);
677 DECLARE_RUNTIME_FUNCTION(UnaryOpIC_Miss);
678 DECLARE_RUNTIME_FUNCTION(StoreIC_MissFromStubFailure);
679 DECLARE_RUNTIME_FUNCTION(ElementsTransitionAndStoreIC_Miss);
680 DECLARE_RUNTIME_FUNCTION(BinaryOpIC_Miss);
681 DECLARE_RUNTIME_FUNCTION(BinaryOpIC_MissWithAllocationSite);
682 DECLARE_RUNTIME_FUNCTION(CompareNilIC_Miss);
683 DECLARE_RUNTIME_FUNCTION(ToBooleanIC_Miss);
684 DECLARE_RUNTIME_FUNCTION(VectorLoadIC_MissFromStubFailure);
685 DECLARE_RUNTIME_FUNCTION(VectorKeyedLoadIC_MissFromStubFailure);
687 // Support functions for callbacks handlers.
688 DECLARE_RUNTIME_FUNCTION(StoreCallbackProperty);
690 // Support functions for interceptor handlers.
691 DECLARE_RUNTIME_FUNCTION(LoadPropertyWithInterceptorOnly);
692 DECLARE_RUNTIME_FUNCTION(LoadPropertyWithInterceptor);
693 DECLARE_RUNTIME_FUNCTION(LoadElementWithInterceptor);
694 DECLARE_RUNTIME_FUNCTION(StorePropertyWithInterceptor);
696 } // namespace v8::internal