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"
10 #include "src/messages.h"
16 // IC is the base class for LoadIC, StoreIC, KeyedLoadIC, and KeyedStoreIC.
20 // Alias the inline cache state type to make the IC code more readable.
21 typedef InlineCacheState State;
23 // The IC code is either invoked with no extra frames on the stack
24 // or with a single extra frame for supporting calls.
25 enum FrameDepth { NO_EXTRA_FRAME = 0, EXTRA_CALL_FRAME = 1 };
27 // Construct the IC structure with the given number of extra
28 // JavaScript frames on the stack.
29 IC(FrameDepth depth, Isolate* isolate, FeedbackNexus* nexus = NULL);
32 State state() const { return state_; }
33 inline Address address() const;
35 // Compute the current IC state based on the target stub, receiver and name.
36 void UpdateState(Handle<Object> receiver, Handle<Object> name);
38 bool IsNameCompatibleWithPrototypeFailure(Handle<Object> name);
39 void MarkPrototypeFailure(Handle<Object> name) {
40 DCHECK(IsNameCompatibleWithPrototypeFailure(name));
42 state_ = PROTOTYPE_FAILURE;
45 // Clear the inline cache to initial state.
46 static void Clear(Isolate* isolate, Address address, Address constant_pool);
49 bool IsLoadStub() const {
50 return target()->is_load_stub() || target()->is_keyed_load_stub();
53 bool IsStoreStub() const {
54 return target()->is_store_stub() || target()->is_keyed_store_stub();
57 bool IsCallStub() const { return target()->is_call_stub(); }
60 static inline JSFunction* GetRootConstructor(Map* receiver_map,
61 Context* native_context);
62 static inline Handle<Map> GetHandlerCacheHolder(Handle<Map> receiver_map,
63 bool receiver_is_holder,
65 CacheHolderFlag* flag);
66 static inline Handle<Map> GetICCacheHolder(Handle<Map> receiver_map,
68 CacheHolderFlag* flag);
70 static bool IsCleared(Code* code) {
71 InlineCacheState state = code->ic_state();
72 return !FLAG_use_ic || state == UNINITIALIZED || state == PREMONOMORPHIC;
75 static bool IsCleared(FeedbackNexus* nexus) {
76 InlineCacheState state = nexus->StateFromFeedback();
77 return !FLAG_use_ic || state == UNINITIALIZED || state == PREMONOMORPHIC;
80 static bool ICUseVector(Code::Kind kind) {
81 return kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC ||
82 kind == Code::CALL_IC ||
83 (FLAG_vector_stores &&
84 (kind == Code::STORE_IC || kind == Code::KEYED_STORE_IC));
88 // Get the call-site target; used for determining the state.
89 Handle<Code> target() const { return target_; }
91 Address fp() const { return fp_; }
92 Address pc() const { return *pc_address_; }
93 Isolate* isolate() const { return isolate_; }
95 // Get the shared function info of the caller.
96 SharedFunctionInfo* GetSharedFunctionInfo() const;
97 // Get the code object of the caller.
98 Code* GetCode() const;
100 bool AddressIsOptimizedCode() const;
101 inline bool AddressIsDeoptimizedCode() const;
102 inline static bool AddressIsDeoptimizedCode(Isolate* isolate,
105 // Set the call-site target.
106 inline void set_target(Code* code);
107 bool is_target_set() { return target_set_; }
108 bool is_vector_set() { return vector_set_; }
110 bool UseVector() const {
111 bool use = ICUseVector(kind());
112 // If we are supposed to use the nexus, verify the nexus is non-null.
113 DCHECK(!use || nexus_ != NULL);
117 // Configure for most states.
118 void ConfigureVectorState(IC::State new_state);
119 // Configure the vector for MONOMORPHIC.
120 void ConfigureVectorState(Handle<Name> name, Handle<Map> map,
121 Handle<Code> handler);
122 // Configure the vector for POLYMORPHIC.
123 void ConfigureVectorState(Handle<Name> name, MapHandleList* maps,
124 CodeHandleList* handlers);
125 // Configure the vector for POLYMORPHIC with transitions (only for element
127 void ConfigureVectorState(MapHandleList* maps,
128 MapHandleList* transitioned_maps,
129 CodeHandleList* handlers);
131 char TransitionMarkFromState(IC::State state);
132 void TraceIC(const char* type, Handle<Object> name);
133 void TraceIC(const char* type, Handle<Object> name, State old_state,
136 MaybeHandle<Object> TypeError(MessageTemplate::Template,
137 Handle<Object> object, Handle<Object> key);
138 MaybeHandle<Object> ReferenceError(Handle<Name> name);
140 // Access the target code for the given IC address.
141 static inline Code* GetTargetAtAddress(Address address,
142 Address constant_pool);
143 static inline void SetTargetAtAddress(Address address, Code* target,
144 Address constant_pool);
145 static void OnTypeFeedbackChanged(Isolate* isolate, Address address,
146 State old_state, State new_state,
147 bool target_remains_ic_stub);
148 // As a vector-based IC, type feedback must be updated differently.
149 static void OnTypeFeedbackChanged(Isolate* isolate, Code* host,
150 TypeFeedbackVector* vector, State old_state,
152 static void PostPatching(Address address, Code* target, Code* old_target);
154 // Compute the handler either by compiling or by retrieving a cached version.
155 Handle<Code> ComputeHandler(LookupIterator* lookup,
156 Handle<Object> value = Handle<Code>::null());
157 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
158 Handle<Object> value,
159 CacheHolderFlag cache_holder) {
161 return Handle<Code>::null();
164 void UpdateMonomorphicIC(Handle<Code> handler, Handle<Name> name);
165 bool UpdatePolymorphicIC(Handle<Name> name, Handle<Code> code);
166 void UpdateMegamorphicCache(Map* map, Name* name, Code* code);
168 void CopyICToMegamorphicCache(Handle<Name> name);
169 bool IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map);
170 void PatchCache(Handle<Name> name, Handle<Code> code);
171 Code::Kind kind() const { return kind_; }
172 Code::Kind handler_kind() const {
173 if (kind_ == Code::KEYED_LOAD_IC) return Code::LOAD_IC;
174 DCHECK(kind_ == Code::LOAD_IC || kind_ == Code::STORE_IC ||
175 kind_ == Code::KEYED_STORE_IC);
178 virtual Handle<Code> megamorphic_stub() {
180 return Handle<Code>::null();
183 bool TryRemoveInvalidPrototypeDependentStub(Handle<Object> receiver,
184 Handle<String> name);
186 ExtraICState extra_ic_state() const { return extra_ic_state_; }
187 void set_extra_ic_state(ExtraICState state) { extra_ic_state_ = state; }
189 Handle<Map> receiver_map() { return receiver_map_; }
190 void update_receiver_map(Handle<Object> receiver) {
191 if (receiver->IsSmi()) {
192 receiver_map_ = isolate_->factory()->heap_number_map();
194 receiver_map_ = handle(HeapObject::cast(*receiver)->map());
198 void TargetMaps(MapHandleList* list) {
200 for (int i = 0; i < target_maps_.length(); i++) {
201 list->Add(target_maps_.at(i));
205 Map* FirstTargetMap() {
207 return target_maps_.length() > 0 ? *target_maps_.at(0) : NULL;
210 inline void UpdateTarget();
212 Handle<TypeFeedbackVector> vector() const { return nexus()->vector_handle(); }
213 FeedbackVectorICSlot slot() const { return nexus()->slot(); }
214 State saved_state() const {
215 return state() == PROTOTYPE_FAILURE ? old_state_ : state();
218 template <class NexusClass>
219 NexusClass* casted_nexus() {
220 return static_cast<NexusClass*>(nexus_);
222 FeedbackNexus* nexus() const { return nexus_; }
224 inline Code* get_host();
227 inline Code* raw_target() const;
228 inline Address constant_pool() const;
229 inline Address raw_constant_pool() const;
231 void FindTargetMaps() {
232 if (target_maps_set_) return;
233 target_maps_set_ = true;
235 nexus()->ExtractMaps(&target_maps_);
237 if (state_ == MONOMORPHIC) {
238 Map* map = target_->FindFirstMap();
239 if (map != NULL) target_maps_.Add(handle(map));
240 } else if (state_ != UNINITIALIZED && state_ != PREMONOMORPHIC) {
241 target_->FindAllMaps(&target_maps_);
246 // Frame pointer for the frame that uses (calls) the IC.
249 // All access to the program counter and constant pool of an IC structure is
250 // indirect 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_;
255 // The constant pool of the code which originally called the IC (which might
256 // be for the breakpointed copy of the original code).
257 Address* constant_pool_address_;
261 // The original code target that missed.
262 Handle<Code> target_;
265 State old_state_; // For saving if we marked as prototype failure.
268 Handle<Map> receiver_map_;
269 MaybeHandle<Code> maybe_handler_;
271 ExtraICState extra_ic_state_;
272 MapHandleList target_maps_;
273 bool target_maps_set_;
275 FeedbackNexus* nexus_;
277 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
281 class CallIC : public IC {
283 CallIC(Isolate* isolate, CallICNexus* nexus)
284 : IC(EXTRA_CALL_FRAME, isolate, nexus) {
285 DCHECK(nexus != NULL);
288 void HandleMiss(Handle<Object> function);
290 // Code generator routines.
291 static Handle<Code> initialize_stub(Isolate* isolate, int argc,
292 CallICState::CallType call_type);
293 static Handle<Code> initialize_stub_in_optimized_code(
294 Isolate* isolate, int argc, CallICState::CallType call_type);
296 static void Clear(Isolate* isolate, Code* host, CallICNexus* nexus);
300 class LoadIC : public IC {
302 static ExtraICState ComputeExtraICState(TypeofMode typeof_mode,
303 LanguageMode language_mode) {
304 return LoadICState(typeof_mode, language_mode).GetExtraICState();
307 TypeofMode typeof_mode() const {
308 return LoadICState::GetTypeofMode(extra_ic_state());
311 LanguageMode language_mode() const {
312 return LoadICState::GetLanguageMode(extra_ic_state());
315 LoadIC(FrameDepth depth, Isolate* isolate, FeedbackNexus* nexus = NULL)
316 : IC(depth, isolate, nexus) {
317 DCHECK(nexus != NULL);
318 DCHECK(IsLoadStub());
321 bool ShouldThrowReferenceError(Handle<Object> receiver) {
322 return receiver->IsGlobalObject() && typeof_mode() == NOT_INSIDE_TYPEOF;
325 // Code generator routines.
327 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
328 static void GenerateMiss(MacroAssembler* masm);
329 static void GenerateRuntimeGetProperty(MacroAssembler* masm,
330 LanguageMode language_mode);
331 static void GenerateNormal(MacroAssembler* masm, LanguageMode language_mode);
333 static Handle<Code> initialize_stub(Isolate* isolate,
334 ExtraICState extra_state);
335 static Handle<Code> initialize_stub_in_optimized_code(
336 Isolate* isolate, ExtraICState extra_state, State initialization_state);
338 MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
341 static void Clear(Isolate* isolate, Code* host, LoadICNexus* nexus);
344 inline void set_target(Code* code);
346 Handle<Code> slow_stub() const {
347 if (kind() == Code::LOAD_IC) {
348 return is_strong(language_mode())
349 ? isolate()->builtins()->LoadIC_Slow_Strong()
350 : isolate()->builtins()->LoadIC_Slow();
352 DCHECK_EQ(Code::KEYED_LOAD_IC, kind());
353 return is_strong(language_mode())
354 ? isolate()->builtins()->KeyedLoadIC_Slow_Strong()
355 : isolate()->builtins()->KeyedLoadIC_Slow();
359 Handle<Code> megamorphic_stub() override;
361 // Update the inline cache and the global stub cache based on the
363 void UpdateCaches(LookupIterator* lookup);
365 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
366 Handle<Object> unused,
367 CacheHolderFlag cache_holder) override;
370 Handle<Code> SimpleFieldLoad(FieldIndex index);
372 static void Clear(Isolate* isolate, Address address, Code* target,
373 Address constant_pool);
379 class KeyedLoadIC : public LoadIC {
381 // ExtraICState bits (building on IC)
382 class IcCheckTypeField
383 : public BitField<IcCheckType, LoadICState::kNextBitFieldOffset, 1> {};
385 static ExtraICState ComputeExtraICState(TypeofMode typeof_mode,
386 LanguageMode language_mode,
387 IcCheckType key_type) {
388 return LoadICState(typeof_mode, language_mode).GetExtraICState() |
389 IcCheckTypeField::encode(key_type);
392 static IcCheckType GetKeyType(ExtraICState extra_state) {
393 return IcCheckTypeField::decode(extra_state);
396 KeyedLoadIC(FrameDepth depth, Isolate* isolate,
397 KeyedLoadICNexus* nexus = NULL)
398 : LoadIC(depth, isolate, nexus) {
399 DCHECK(nexus != NULL);
400 DCHECK(target()->is_keyed_load_stub());
403 MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
406 // Code generator routines.
407 static void GenerateMiss(MacroAssembler* masm);
408 static void GenerateRuntimeGetProperty(MacroAssembler* masm,
409 LanguageMode language_mode);
410 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
411 static void GenerateMegamorphic(MacroAssembler* masm,
412 LanguageMode language_mode);
414 // Bit mask to be tested against bit field for the cases when
415 // generic stub should go into slow case.
416 // Access check is necessary explicitly since generic stub does not perform
418 static const int kSlowCaseBitFieldMask =
419 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
421 static Handle<Code> initialize_stub(Isolate* isolate,
422 ExtraICState extra_state);
423 static Handle<Code> initialize_stub_in_optimized_code(
424 Isolate* isolate, State initialization_state, ExtraICState extra_state);
425 static Handle<Code> ChooseMegamorphicStub(Isolate* isolate,
426 ExtraICState extra_state);
428 static void Clear(Isolate* isolate, Code* host, KeyedLoadICNexus* nexus);
431 // receiver is HeapObject because it could be a String or a JSObject
432 Handle<Code> LoadElementStub(Handle<HeapObject> receiver);
435 static void Clear(Isolate* isolate, Address address, Code* target,
436 Address constant_pool);
442 class StoreIC : public IC {
444 static ExtraICState ComputeExtraICState(LanguageMode flag) {
445 return StoreICState(flag).GetExtraICState();
448 StoreIC(FrameDepth depth, Isolate* isolate, FeedbackNexus* nexus = NULL)
449 : IC(depth, isolate, nexus) {
450 DCHECK(IsStoreStub());
453 LanguageMode language_mode() const {
454 return StoreICState::GetLanguageMode(extra_ic_state());
457 // Code generators for stub routines. Only called once at startup.
458 static void GenerateSlow(MacroAssembler* masm);
459 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
460 static void GeneratePreMonomorphic(MacroAssembler* masm) {
463 static void GenerateMiss(MacroAssembler* masm);
464 static void GenerateMegamorphic(MacroAssembler* masm);
465 static void GenerateNormal(MacroAssembler* masm);
466 static void GenerateRuntimeSetProperty(MacroAssembler* masm,
467 LanguageMode language_mode);
469 static Handle<Code> initialize_stub(Isolate* isolate,
470 LanguageMode language_mode,
471 State initialization_state);
472 static Handle<Code> initialize_stub_in_optimized_code(
473 Isolate* isolate, LanguageMode language_mode, State initialization_state);
475 MUST_USE_RESULT MaybeHandle<Object> Store(
476 Handle<Object> object, Handle<Name> name, Handle<Object> value,
477 JSReceiver::StoreFromKeyed store_mode =
478 JSReceiver::CERTAINLY_NOT_STORE_FROM_KEYED);
480 bool LookupForWrite(LookupIterator* it, Handle<Object> value,
481 JSReceiver::StoreFromKeyed store_mode);
483 static void Clear(Isolate* isolate, Code* host, StoreICNexus* nexus);
487 Handle<Code> megamorphic_stub() override;
488 Handle<Code> slow_stub() const;
490 virtual Handle<Code> pre_monomorphic_stub() const {
491 return pre_monomorphic_stub(isolate(), language_mode());
494 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
495 LanguageMode language_mode);
497 // Update the inline cache and the global stub cache based on the
499 void UpdateCaches(LookupIterator* lookup, Handle<Object> value,
500 JSReceiver::StoreFromKeyed store_mode);
501 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
502 Handle<Object> value,
503 CacheHolderFlag cache_holder) override;
506 inline void set_target(Code* code);
508 static void Clear(Isolate* isolate, Address address, Code* target,
509 Address constant_pool);
515 enum KeyedStoreCheckMap { kDontCheckMap, kCheckMap };
518 enum KeyedStoreIncrementLength { kDontIncrementLength, kIncrementLength };
521 class KeyedStoreIC : public StoreIC {
523 // ExtraICState bits (building on IC)
525 // When more language modes are added, these BitFields need to move too.
526 STATIC_ASSERT(i::LANGUAGE_END == 3);
527 class ExtraICStateKeyedAccessStoreMode
528 : public BitField<KeyedAccessStoreMode, 3, 3> {}; // NOLINT
530 class IcCheckTypeField : public BitField<IcCheckType, 6, 1> {};
532 static ExtraICState ComputeExtraICState(LanguageMode flag,
533 KeyedAccessStoreMode mode) {
534 return StoreICState(flag).GetExtraICState() |
535 ExtraICStateKeyedAccessStoreMode::encode(mode) |
536 IcCheckTypeField::encode(ELEMENT);
539 static KeyedAccessStoreMode GetKeyedAccessStoreMode(
540 ExtraICState extra_state) {
541 DCHECK(!FLAG_vector_stores);
542 return ExtraICStateKeyedAccessStoreMode::decode(extra_state);
545 KeyedAccessStoreMode GetKeyedAccessStoreMode() {
546 DCHECK(FLAG_vector_stores);
547 return casted_nexus<KeyedStoreICNexus>()->GetKeyedAccessStoreMode();
550 static IcCheckType GetKeyType(ExtraICState extra_state) {
551 DCHECK(!FLAG_vector_stores);
552 return IcCheckTypeField::decode(extra_state);
555 KeyedStoreIC(FrameDepth depth, Isolate* isolate,
556 KeyedStoreICNexus* nexus = NULL)
557 : StoreIC(depth, isolate, nexus) {
558 DCHECK(target()->is_keyed_store_stub());
561 MUST_USE_RESULT MaybeHandle<Object> Store(Handle<Object> object,
563 Handle<Object> value);
565 // Code generators for stub routines. Only called once at startup.
566 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
567 static void GeneratePreMonomorphic(MacroAssembler* masm) {
570 static void GenerateMiss(MacroAssembler* masm);
571 static void GenerateSlow(MacroAssembler* masm);
572 static void GenerateMegamorphic(MacroAssembler* masm,
573 LanguageMode language_mode);
575 static Handle<Code> initialize_stub(Isolate* isolate,
576 LanguageMode language_mode,
577 State initialization_state);
579 static Handle<Code> initialize_stub_in_optimized_code(
580 Isolate* isolate, LanguageMode language_mode, State initialization_state);
581 static Handle<Code> ChooseMegamorphicStub(Isolate* isolate,
582 ExtraICState extra_state);
584 static void Clear(Isolate* isolate, Code* host, KeyedStoreICNexus* nexus);
587 virtual Handle<Code> pre_monomorphic_stub() const {
588 return pre_monomorphic_stub(isolate(), language_mode());
590 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
591 LanguageMode language_mode) {
592 if (is_strict(language_mode)) {
593 return isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict();
595 return isolate->builtins()->KeyedStoreIC_PreMonomorphic();
599 Handle<Code> StoreElementStub(Handle<Map> receiver_map,
600 KeyedAccessStoreMode store_mode);
603 inline void set_target(Code* code);
605 static void Clear(Isolate* isolate, Address address, Code* target,
606 Address constant_pool);
608 Handle<Map> ComputeTransitionedMap(Handle<Map> map,
609 KeyedAccessStoreMode store_mode);
611 void ValidateStoreMode(Handle<Code> stub);
617 // Type Recording BinaryOpIC, that records the types of the inputs and outputs.
618 class BinaryOpIC : public IC {
620 explicit BinaryOpIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
622 MaybeHandle<Object> Transition(Handle<AllocationSite> allocation_site,
624 Handle<Object> right) WARN_UNUSED_RESULT;
628 class CompareIC : public IC {
630 CompareIC(Isolate* isolate, Token::Value op)
631 : IC(EXTRA_CALL_FRAME, isolate), op_(op) {}
633 // Update the inline cache for the given operands.
634 Code* UpdateCaches(Handle<Object> x, Handle<Object> y);
636 // Helper function for computing the condition for a compare operation.
637 static Condition ComputeCondition(Token::Value op);
639 // Factory method for getting an uninitialized compare stub.
640 static Handle<Code> GetUninitialized(Isolate* isolate, Token::Value op,
644 static bool HasInlinedSmiCode(Address address);
646 bool strict() const { return op_ == Token::EQ_STRICT; }
647 Condition GetCondition() const { return ComputeCondition(op_); }
649 static Code* GetRawUninitialized(Isolate* isolate, Token::Value op,
652 static void Clear(Isolate* isolate, Address address, Code* target,
653 Address constant_pool);
661 class CompareNilIC : public IC {
663 explicit CompareNilIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
665 Handle<Object> CompareNil(Handle<Object> object);
667 static Handle<Code> GetUninitialized();
669 static void Clear(Address address, Code* target, Address constant_pool);
671 static Handle<Object> DoCompareNilSlow(Isolate* isolate, NilValue nil,
672 Handle<Object> object);
676 class ToBooleanIC : public IC {
678 explicit ToBooleanIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
680 Handle<Object> ToBoolean(Handle<Object> object);
684 // Helper for BinaryOpIC and CompareIC.
685 enum InlinedSmiCheck { ENABLE_INLINED_SMI_CHECK, DISABLE_INLINED_SMI_CHECK };
686 void PatchInlinedSmiCode(Address address, InlinedSmiCheck check);
688 } // namespace internal