Rewrite StoreIC handling using the LookupIterator. Continued from patch 494153002
[platform/upstream/v8.git] / src / ic / ic.h
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.
4
5 #ifndef V8_IC_H_
6 #define V8_IC_H_
7
8 #include "src/macro-assembler.h"
9
10 namespace v8 {
11 namespace internal {
12
13
14 const int kMaxKeyedPolymorphism = 4;
15
16
17 // IC_UTIL_LIST defines all utility functions called from generated
18 // inline caching code. The argument for the macro, ICU, is the function name.
19 #define IC_UTIL_LIST(ICU)              \
20   ICU(LoadIC_Miss)                     \
21   ICU(KeyedLoadIC_Miss)                \
22   ICU(CallIC_Miss)                     \
23   ICU(CallIC_Customization_Miss)       \
24   ICU(StoreIC_Miss)                    \
25   ICU(StoreIC_Slow)                    \
26   ICU(SharedStoreIC_ExtendStorage)     \
27   ICU(KeyedStoreIC_Miss)               \
28   ICU(KeyedStoreIC_Slow)               \
29   /* Utilities for IC stubs. */        \
30   ICU(StoreCallbackProperty)           \
31   ICU(LoadPropertyWithInterceptorOnly) \
32   ICU(LoadPropertyWithInterceptor)     \
33   ICU(LoadElementWithInterceptor)      \
34   ICU(StorePropertyWithInterceptor)    \
35   ICU(CompareIC_Miss)                  \
36   ICU(BinaryOpIC_Miss)                 \
37   ICU(CompareNilIC_Miss)               \
38   ICU(Unreachable)                     \
39   ICU(ToBooleanIC_Miss)
40 //
41 // IC is the base class for LoadIC, StoreIC, KeyedLoadIC, and KeyedStoreIC.
42 //
43 class IC {
44  public:
45   // The ids for utility called from the generated code.
46   enum UtilityId {
47 #define CONST_NAME(name) k##name,
48     IC_UTIL_LIST(CONST_NAME)
49 #undef CONST_NAME
50     kUtilityCount
51   };
52
53   // Looks up the address of the named utility.
54   static Address AddressFromUtilityId(UtilityId id);
55
56   // Alias the inline cache state type to make the IC code more readable.
57   typedef InlineCacheState State;
58
59   // The IC code is either invoked with no extra frames on the stack
60   // or with a single extra frame for supporting calls.
61   enum FrameDepth { NO_EXTRA_FRAME = 0, EXTRA_CALL_FRAME = 1 };
62
63   // Construct the IC structure with the given number of extra
64   // JavaScript frames on the stack.
65   IC(FrameDepth depth, Isolate* isolate);
66   virtual ~IC() {}
67
68   State state() const { return state_; }
69   inline Address address() const;
70
71   // Compute the current IC state based on the target stub, receiver and name.
72   void UpdateState(Handle<Object> receiver, Handle<Object> name);
73
74   bool IsNameCompatibleWithPrototypeFailure(Handle<Object> name);
75   void MarkPrototypeFailure(Handle<Object> name) {
76     DCHECK(IsNameCompatibleWithPrototypeFailure(name));
77     state_ = PROTOTYPE_FAILURE;
78   }
79
80   // If the stub contains weak maps then this function adds the stub to
81   // the dependent code array of each weak map.
82   static void RegisterWeakMapDependency(Handle<Code> stub);
83
84   // This function is called when a weak map in the stub is dying,
85   // invalidates the stub by setting maps in it to undefined.
86   static void InvalidateMaps(Code* stub);
87
88   // Clear the inline cache to initial state.
89   static void Clear(Isolate* isolate, Address address,
90                     ConstantPoolArray* constant_pool);
91
92 #ifdef DEBUG
93   bool IsLoadStub() const {
94     return target()->is_load_stub() || target()->is_keyed_load_stub();
95   }
96
97   bool IsStoreStub() const {
98     return target()->is_store_stub() || target()->is_keyed_store_stub();
99   }
100
101   bool IsCallStub() const { return target()->is_call_stub(); }
102 #endif
103
104   template <class TypeClass>
105   static JSFunction* GetRootConstructor(TypeClass* type,
106                                         Context* native_context);
107   static inline Handle<Map> GetHandlerCacheHolder(HeapType* type,
108                                                   bool receiver_is_holder,
109                                                   Isolate* isolate,
110                                                   CacheHolderFlag* flag);
111   static inline Handle<Map> GetICCacheHolder(HeapType* type, Isolate* isolate,
112                                              CacheHolderFlag* flag);
113
114   static bool IsCleared(Code* code) {
115     InlineCacheState state = code->ic_state();
116     return state == UNINITIALIZED || state == PREMONOMORPHIC;
117   }
118
119   // Utility functions to convert maps to types and back. There are two special
120   // cases:
121   // - The heap_number_map is used as a marker which includes heap numbers as
122   //   well as smis.
123   // - The oddball map is only used for booleans.
124   static Handle<Map> TypeToMap(HeapType* type, Isolate* isolate);
125   template <class T>
126   static typename T::TypeHandle MapToType(Handle<Map> map,
127                                           typename T::Region* region);
128
129   static Handle<HeapType> CurrentTypeOf(Handle<Object> object,
130                                         Isolate* isolate);
131
132  protected:
133   // Get the call-site target; used for determining the state.
134   Handle<Code> target() const { return target_; }
135
136   Address fp() const { return fp_; }
137   Address pc() const { return *pc_address_; }
138   Isolate* isolate() const { return isolate_; }
139
140   // Get the shared function info of the caller.
141   SharedFunctionInfo* GetSharedFunctionInfo() const;
142   // Get the code object of the caller.
143   Code* GetCode() const;
144   // Get the original (non-breakpointed) code object of the caller.
145   Code* GetOriginalCode() const;
146
147   // Set the call-site target.
148   inline void set_target(Code* code);
149   bool is_target_set() { return target_set_; }
150
151   char TransitionMarkFromState(IC::State state);
152   void TraceIC(const char* type, Handle<Object> name);
153   void TraceIC(const char* type, Handle<Object> name, State old_state,
154                State new_state);
155
156   MaybeHandle<Object> TypeError(const char* type, Handle<Object> object,
157                                 Handle<Object> key);
158   MaybeHandle<Object> ReferenceError(const char* type, Handle<Name> name);
159
160   // Access the target code for the given IC address.
161   static inline Code* GetTargetAtAddress(Address address,
162                                          ConstantPoolArray* constant_pool);
163   static inline void SetTargetAtAddress(Address address, Code* target,
164                                         ConstantPoolArray* constant_pool);
165   static void OnTypeFeedbackChanged(Isolate* isolate, Address address,
166                                     State old_state, State new_state,
167                                     bool target_remains_ic_stub);
168   static void PostPatching(Address address, Code* target, Code* old_target);
169
170   // Compute the handler either by compiling or by retrieving a cached version.
171   Handle<Code> ComputeHandler(LookupIterator* lookup,
172                               Handle<Object> value = Handle<Code>::null());
173   virtual Handle<Code> CompileHandler(LookupIterator* lookup,
174                                       Handle<Object> value,
175                                       CacheHolderFlag cache_holder) {
176     UNREACHABLE();
177     return Handle<Code>::null();
178   }
179
180   void UpdateMonomorphicIC(Handle<Code> handler, Handle<Name> name);
181   bool UpdatePolymorphicIC(Handle<Name> name, Handle<Code> code);
182   void UpdateMegamorphicCache(HeapType* type, Name* name, Code* code);
183
184   void CopyICToMegamorphicCache(Handle<Name> name);
185   bool IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map);
186   void PatchCache(Handle<Name> name, Handle<Code> code);
187   Code::Kind kind() const { return kind_; }
188   Code::Kind handler_kind() const {
189     if (kind_ == Code::KEYED_LOAD_IC) return Code::LOAD_IC;
190     DCHECK(kind_ == Code::LOAD_IC || kind_ == Code::STORE_IC ||
191            kind_ == Code::KEYED_STORE_IC);
192     return kind_;
193   }
194   virtual Handle<Code> megamorphic_stub() {
195     UNREACHABLE();
196     return Handle<Code>::null();
197   }
198
199   bool TryRemoveInvalidPrototypeDependentStub(Handle<Object> receiver,
200                                               Handle<String> name);
201
202   ExtraICState extra_ic_state() const { return extra_ic_state_; }
203   void set_extra_ic_state(ExtraICState state) { extra_ic_state_ = state; }
204
205   Handle<HeapType> receiver_type() { return receiver_type_; }
206   void update_receiver_type(Handle<Object> receiver) {
207     receiver_type_ = CurrentTypeOf(receiver, isolate_);
208   }
209
210   void TargetMaps(MapHandleList* list) {
211     FindTargetMaps();
212     for (int i = 0; i < target_maps_.length(); i++) {
213       list->Add(target_maps_.at(i));
214     }
215   }
216
217   void TargetTypes(TypeHandleList* list) {
218     FindTargetMaps();
219     for (int i = 0; i < target_maps_.length(); i++) {
220       list->Add(IC::MapToType<HeapType>(target_maps_.at(i), isolate_));
221     }
222   }
223
224   Map* FirstTargetMap() {
225     FindTargetMaps();
226     return target_maps_.length() > 0 ? *target_maps_.at(0) : NULL;
227   }
228
229  protected:
230   inline void UpdateTarget();
231
232  private:
233   inline Code* raw_target() const;
234   inline ConstantPoolArray* constant_pool() const;
235   inline ConstantPoolArray* raw_constant_pool() const;
236
237   void FindTargetMaps() {
238     if (target_maps_set_) return;
239     target_maps_set_ = true;
240     if (state_ == MONOMORPHIC) {
241       Map* map = target_->FindFirstMap();
242       if (map != NULL) target_maps_.Add(handle(map));
243     } else if (state_ != UNINITIALIZED && state_ != PREMONOMORPHIC) {
244       target_->FindAllMaps(&target_maps_);
245     }
246   }
247
248   // Frame pointer for the frame that uses (calls) the IC.
249   Address fp_;
250
251   // All access to the program counter of an IC structure is indirect
252   // to make the code GC safe. This feature is crucial since
253   // GetProperty and SetProperty are called and they in turn might
254   // invoke the garbage collector.
255   Address* pc_address_;
256
257   Isolate* isolate_;
258
259   // The constant pool of the code which originally called the IC (which might
260   // be for the breakpointed copy of the original code).
261   Handle<ConstantPoolArray> raw_constant_pool_;
262
263   // The original code target that missed.
264   Handle<Code> target_;
265   bool target_set_;
266   State state_;
267   Code::Kind kind_;
268   Handle<HeapType> receiver_type_;
269   MaybeHandle<Code> maybe_handler_;
270
271   ExtraICState extra_ic_state_;
272   MapHandleList target_maps_;
273   bool target_maps_set_;
274
275   DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
276 };
277
278
279 // An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
280 // cannot make forward declarations to an enum.
281 class IC_Utility {
282  public:
283   explicit IC_Utility(IC::UtilityId id)
284       : address_(IC::AddressFromUtilityId(id)), id_(id) {}
285
286   Address address() const { return address_; }
287
288   IC::UtilityId id() const { return id_; }
289
290  private:
291   Address address_;
292   IC::UtilityId id_;
293 };
294
295
296 class CallIC : public IC {
297  public:
298   enum CallType { METHOD, FUNCTION };
299
300   class State V8_FINAL BASE_EMBEDDED {
301    public:
302     explicit State(ExtraICState extra_ic_state);
303
304     State(int argc, CallType call_type) : argc_(argc), call_type_(call_type) {}
305
306     ExtraICState GetExtraICState() const;
307
308     static void GenerateAheadOfTime(Isolate*,
309                                     void (*Generate)(Isolate*, const State&));
310
311     int arg_count() const { return argc_; }
312     CallType call_type() const { return call_type_; }
313
314     bool CallAsMethod() const { return call_type_ == METHOD; }
315
316    private:
317     class ArgcBits : public BitField<int, 0, Code::kArgumentsBits> {};
318     class CallTypeBits : public BitField<CallType, Code::kArgumentsBits, 1> {};
319
320     const int argc_;
321     const CallType call_type_;
322   };
323
324   explicit CallIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
325
326   void PatchMegamorphic(Handle<Object> function, Handle<FixedArray> vector,
327                         Handle<Smi> slot);
328
329   void HandleMiss(Handle<Object> receiver, Handle<Object> function,
330                   Handle<FixedArray> vector, Handle<Smi> slot);
331
332   // Returns true if a custom handler was installed.
333   bool DoCustomHandler(Handle<Object> receiver, Handle<Object> function,
334                        Handle<FixedArray> vector, Handle<Smi> slot,
335                        const State& state);
336
337   // Code generator routines.
338   static Handle<Code> initialize_stub(Isolate* isolate, int argc,
339                                       CallType call_type);
340
341   static void Clear(Isolate* isolate, Address address, Code* target,
342                     ConstantPoolArray* constant_pool);
343
344  private:
345   inline IC::State FeedbackToState(Handle<FixedArray> vector,
346                                    Handle<Smi> slot) const;
347 };
348
349
350 OStream& operator<<(OStream& os, const CallIC::State& s);
351
352
353 class LoadIC : public IC {
354  public:
355   enum ParameterIndices { kReceiverIndex, kNameIndex, kParameterCount };
356   static const Register ReceiverRegister();
357   static const Register NameRegister();
358
359   // With flag vector-ics, there is an additional argument. And for calls from
360   // crankshaft, yet another.
361   static const Register SlotRegister();
362   static const Register VectorRegister();
363
364   class State V8_FINAL BASE_EMBEDDED {
365    public:
366     explicit State(ExtraICState extra_ic_state) : state_(extra_ic_state) {}
367
368     explicit State(ContextualMode mode)
369         : state_(ContextualModeBits::encode(mode)) {}
370
371     ExtraICState GetExtraICState() const { return state_; }
372
373     ContextualMode contextual_mode() const {
374       return ContextualModeBits::decode(state_);
375     }
376
377    private:
378     class ContextualModeBits : public BitField<ContextualMode, 0, 1> {};
379     STATIC_ASSERT(static_cast<int>(NOT_CONTEXTUAL) == 0);
380
381     const ExtraICState state_;
382   };
383
384   static ExtraICState ComputeExtraICState(ContextualMode contextual_mode) {
385     return State(contextual_mode).GetExtraICState();
386   }
387
388   static ContextualMode GetContextualMode(ExtraICState state) {
389     return State(state).contextual_mode();
390   }
391
392   ContextualMode contextual_mode() const {
393     return GetContextualMode(extra_ic_state());
394   }
395
396   explicit LoadIC(FrameDepth depth, Isolate* isolate) : IC(depth, isolate) {
397     DCHECK(IsLoadStub());
398   }
399
400   // Returns if this IC is for contextual (no explicit receiver)
401   // access to properties.
402   bool IsUndeclaredGlobal(Handle<Object> receiver) {
403     if (receiver->IsGlobalObject()) {
404       return contextual_mode() == CONTEXTUAL;
405     } else {
406       DCHECK(contextual_mode() != CONTEXTUAL);
407       return false;
408     }
409   }
410
411   // Code generator routines.
412   static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
413   static void GeneratePreMonomorphic(MacroAssembler* masm) {
414     GenerateMiss(masm);
415   }
416   static void GenerateMiss(MacroAssembler* masm);
417   static void GenerateMegamorphic(MacroAssembler* masm);
418   static void GenerateNormal(MacroAssembler* masm);
419   static void GenerateRuntimeGetProperty(MacroAssembler* masm);
420
421   static Handle<Code> initialize_stub(Isolate* isolate,
422                                       ExtraICState extra_state);
423
424   MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
425                                            Handle<Name> name);
426
427  protected:
428   inline void set_target(Code* code);
429
430   Handle<Code> slow_stub() const {
431     if (kind() == Code::LOAD_IC) {
432       return isolate()->builtins()->LoadIC_Slow();
433     } else {
434       DCHECK_EQ(Code::KEYED_LOAD_IC, kind());
435       return isolate()->builtins()->KeyedLoadIC_Slow();
436     }
437   }
438
439   virtual Handle<Code> megamorphic_stub();
440
441   // Update the inline cache and the global stub cache based on the
442   // lookup result.
443   void UpdateCaches(LookupIterator* lookup);
444
445   virtual Handle<Code> CompileHandler(LookupIterator* lookup,
446                                       Handle<Object> unused,
447                                       CacheHolderFlag cache_holder);
448
449  private:
450   virtual Handle<Code> pre_monomorphic_stub() const;
451   static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
452                                            ExtraICState extra_state);
453
454   Handle<Code> SimpleFieldLoad(FieldIndex index);
455
456   static void Clear(Isolate* isolate, Address address, Code* target,
457                     ConstantPoolArray* constant_pool);
458
459   friend class IC;
460 };
461
462
463 class KeyedLoadIC : public LoadIC {
464  public:
465   explicit KeyedLoadIC(FrameDepth depth, Isolate* isolate)
466       : LoadIC(depth, isolate) {
467     DCHECK(target()->is_keyed_load_stub());
468   }
469
470   MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
471                                            Handle<Object> key);
472
473   // Code generator routines.
474   static void GenerateMiss(MacroAssembler* masm);
475   static void GenerateRuntimeGetProperty(MacroAssembler* masm);
476   static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
477   static void GeneratePreMonomorphic(MacroAssembler* masm) {
478     GenerateMiss(masm);
479   }
480   static void GenerateGeneric(MacroAssembler* masm);
481   static void GenerateString(MacroAssembler* masm);
482   static void GenerateIndexedInterceptor(MacroAssembler* masm);
483   static void GenerateSloppyArguments(MacroAssembler* masm);
484
485   // Bit mask to be tested against bit field for the cases when
486   // generic stub should go into slow case.
487   // Access check is necessary explicitly since generic stub does not perform
488   // map checks.
489   static const int kSlowCaseBitFieldMask =
490       (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
491
492   static Handle<Code> generic_stub(Isolate* isolate);
493   static Handle<Code> pre_monomorphic_stub(Isolate* isolate);
494
495  protected:
496   Handle<Code> LoadElementStub(Handle<JSObject> receiver);
497   virtual Handle<Code> pre_monomorphic_stub() const {
498     return pre_monomorphic_stub(isolate());
499   }
500
501  private:
502   Handle<Code> generic_stub() const { return generic_stub(isolate()); }
503   Handle<Code> indexed_interceptor_stub() {
504     return isolate()->builtins()->KeyedLoadIC_IndexedInterceptor();
505   }
506   Handle<Code> sloppy_arguments_stub() {
507     return isolate()->builtins()->KeyedLoadIC_SloppyArguments();
508   }
509   Handle<Code> string_stub() {
510     return isolate()->builtins()->KeyedLoadIC_String();
511   }
512
513   static void Clear(Isolate* isolate, Address address, Code* target,
514                     ConstantPoolArray* constant_pool);
515
516   friend class IC;
517 };
518
519
520 class StoreIC : public IC {
521  public:
522   class StrictModeState : public BitField<StrictMode, 1, 1> {};
523   static ExtraICState ComputeExtraICState(StrictMode flag) {
524     return StrictModeState::encode(flag);
525   }
526   static StrictMode GetStrictMode(ExtraICState state) {
527     return StrictModeState::decode(state);
528   }
529
530   // For convenience, a statically declared encoding of strict mode extra
531   // IC state.
532   static const ExtraICState kStrictModeState = 1 << StrictModeState::kShift;
533
534   enum ParameterIndices {
535     kReceiverIndex,
536     kNameIndex,
537     kValueIndex,
538     kParameterCount
539   };
540   static const Register ReceiverRegister();
541   static const Register NameRegister();
542   static const Register ValueRegister();
543
544   StoreIC(FrameDepth depth, Isolate* isolate) : IC(depth, isolate) {
545     DCHECK(IsStoreStub());
546   }
547
548   StrictMode strict_mode() const {
549     return StrictModeState::decode(extra_ic_state());
550   }
551
552   // Code generators for stub routines. Only called once at startup.
553   static void GenerateSlow(MacroAssembler* masm);
554   static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
555   static void GeneratePreMonomorphic(MacroAssembler* masm) {
556     GenerateMiss(masm);
557   }
558   static void GenerateMiss(MacroAssembler* masm);
559   static void GenerateMegamorphic(MacroAssembler* masm);
560   static void GenerateNormal(MacroAssembler* masm);
561   static void GenerateRuntimeSetProperty(MacroAssembler* masm,
562                                          StrictMode strict_mode);
563
564   static Handle<Code> initialize_stub(Isolate* isolate, StrictMode strict_mode);
565
566   MUST_USE_RESULT MaybeHandle<Object> Store(
567       Handle<Object> object, Handle<Name> name, Handle<Object> value,
568       JSReceiver::StoreFromKeyed store_mode =
569           JSReceiver::CERTAINLY_NOT_STORE_FROM_KEYED);
570
571   bool LookupForWrite(LookupIterator* it, Handle<Object> value,
572                       JSReceiver::StoreFromKeyed store_mode);
573
574  protected:
575   virtual Handle<Code> megamorphic_stub();
576
577   // Stub accessors.
578   virtual Handle<Code> generic_stub() const;
579
580   virtual Handle<Code> slow_stub() const {
581     return isolate()->builtins()->StoreIC_Slow();
582   }
583
584   virtual Handle<Code> pre_monomorphic_stub() const {
585     return pre_monomorphic_stub(isolate(), strict_mode());
586   }
587
588   static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
589                                            StrictMode strict_mode);
590
591   // Update the inline cache and the global stub cache based on the
592   // lookup result.
593   void UpdateCaches(LookupIterator* lookup, Handle<Object> value,
594                     JSReceiver::StoreFromKeyed store_mode);
595   virtual Handle<Code> CompileHandler(LookupIterator* lookup,
596                                       Handle<Object> value,
597                                       CacheHolderFlag cache_holder);
598
599  private:
600   inline void set_target(Code* code);
601
602   static void Clear(Isolate* isolate, Address address, Code* target,
603                     ConstantPoolArray* constant_pool);
604
605   friend class IC;
606 };
607
608
609 enum KeyedStoreCheckMap { kDontCheckMap, kCheckMap };
610
611
612 enum KeyedStoreIncrementLength { kDontIncrementLength, kIncrementLength };
613
614
615 class KeyedStoreIC : public StoreIC {
616  public:
617   // ExtraICState bits (building on IC)
618   // ExtraICState bits
619   class ExtraICStateKeyedAccessStoreMode
620       : public BitField<KeyedAccessStoreMode, 2, 4> {};  // NOLINT
621
622   static ExtraICState ComputeExtraICState(StrictMode flag,
623                                           KeyedAccessStoreMode mode) {
624     return StrictModeState::encode(flag) |
625            ExtraICStateKeyedAccessStoreMode::encode(mode);
626   }
627
628   static KeyedAccessStoreMode GetKeyedAccessStoreMode(
629       ExtraICState extra_state) {
630     return ExtraICStateKeyedAccessStoreMode::decode(extra_state);
631   }
632
633   // The map register isn't part of the normal call specification, but
634   // ElementsTransitionAndStoreStub, used in polymorphic keyed store
635   // stub implementations requires it to be initialized.
636   static const Register MapRegister();
637
638   KeyedStoreIC(FrameDepth depth, Isolate* isolate) : StoreIC(depth, isolate) {
639     DCHECK(target()->is_keyed_store_stub());
640   }
641
642   MUST_USE_RESULT MaybeHandle<Object> Store(Handle<Object> object,
643                                             Handle<Object> name,
644                                             Handle<Object> value);
645
646   // Code generators for stub routines.  Only called once at startup.
647   static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
648   static void GeneratePreMonomorphic(MacroAssembler* masm) {
649     GenerateMiss(masm);
650   }
651   static void GenerateMiss(MacroAssembler* masm);
652   static void GenerateSlow(MacroAssembler* masm);
653   static void GenerateRuntimeSetProperty(MacroAssembler* masm,
654                                          StrictMode strict_mode);
655   static void GenerateGeneric(MacroAssembler* masm, StrictMode strict_mode);
656   static void GenerateSloppyArguments(MacroAssembler* masm);
657
658  protected:
659   virtual Handle<Code> pre_monomorphic_stub() const {
660     return pre_monomorphic_stub(isolate(), strict_mode());
661   }
662   static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
663                                            StrictMode strict_mode) {
664     if (strict_mode == STRICT) {
665       return isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict();
666     } else {
667       return isolate->builtins()->KeyedStoreIC_PreMonomorphic();
668     }
669   }
670   virtual Handle<Code> slow_stub() const {
671     return isolate()->builtins()->KeyedStoreIC_Slow();
672   }
673   virtual Handle<Code> megamorphic_stub() {
674     if (strict_mode() == STRICT) {
675       return isolate()->builtins()->KeyedStoreIC_Generic_Strict();
676     } else {
677       return isolate()->builtins()->KeyedStoreIC_Generic();
678     }
679   }
680
681   Handle<Code> StoreElementStub(Handle<JSObject> receiver,
682                                 KeyedAccessStoreMode store_mode);
683
684  private:
685   inline void set_target(Code* code);
686
687   // Stub accessors.
688   virtual Handle<Code> generic_stub() const {
689     if (strict_mode() == STRICT) {
690       return isolate()->builtins()->KeyedStoreIC_Generic_Strict();
691     } else {
692       return isolate()->builtins()->KeyedStoreIC_Generic();
693     }
694   }
695
696   Handle<Code> sloppy_arguments_stub() {
697     return isolate()->builtins()->KeyedStoreIC_SloppyArguments();
698   }
699
700   static void Clear(Isolate* isolate, Address address, Code* target,
701                     ConstantPoolArray* constant_pool);
702
703   KeyedAccessStoreMode GetStoreMode(Handle<JSObject> receiver,
704                                     Handle<Object> key, Handle<Object> value);
705
706   Handle<Map> ComputeTransitionedMap(Handle<Map> map,
707                                      KeyedAccessStoreMode store_mode);
708
709   friend class IC;
710 };
711
712
713 // Mode to overwrite BinaryExpression values.
714 enum OverwriteMode { NO_OVERWRITE, OVERWRITE_LEFT, OVERWRITE_RIGHT };
715
716 // Type Recording BinaryOpIC, that records the types of the inputs and outputs.
717 class BinaryOpIC : public IC {
718  public:
719   class State V8_FINAL BASE_EMBEDDED {
720    public:
721     State(Isolate* isolate, ExtraICState extra_ic_state);
722
723     State(Isolate* isolate, Token::Value op, OverwriteMode mode)
724         : op_(op),
725           mode_(mode),
726           left_kind_(NONE),
727           right_kind_(NONE),
728           result_kind_(NONE),
729           isolate_(isolate) {
730       DCHECK_LE(FIRST_TOKEN, op);
731       DCHECK_LE(op, LAST_TOKEN);
732     }
733
734     InlineCacheState GetICState() const {
735       if (Max(left_kind_, right_kind_) == NONE) {
736         return ::v8::internal::UNINITIALIZED;
737       }
738       if (Max(left_kind_, right_kind_) == GENERIC) {
739         return ::v8::internal::MEGAMORPHIC;
740       }
741       if (Min(left_kind_, right_kind_) == GENERIC) {
742         return ::v8::internal::GENERIC;
743       }
744       return ::v8::internal::MONOMORPHIC;
745     }
746
747     ExtraICState GetExtraICState() const;
748
749     static void GenerateAheadOfTime(Isolate*,
750                                     void (*Generate)(Isolate*, const State&));
751
752     bool CanReuseDoubleBox() const {
753       return (result_kind_ > SMI && result_kind_ <= NUMBER) &&
754              ((mode_ == OVERWRITE_LEFT && left_kind_ > SMI &&
755                left_kind_ <= NUMBER) ||
756               (mode_ == OVERWRITE_RIGHT && right_kind_ > SMI &&
757                right_kind_ <= NUMBER));
758     }
759
760     // Returns true if the IC _could_ create allocation mementos.
761     bool CouldCreateAllocationMementos() const {
762       if (left_kind_ == STRING || right_kind_ == STRING) {
763         DCHECK_EQ(Token::ADD, op_);
764         return true;
765       }
766       return false;
767     }
768
769     // Returns true if the IC _should_ create allocation mementos.
770     bool ShouldCreateAllocationMementos() const {
771       return FLAG_allocation_site_pretenuring &&
772              CouldCreateAllocationMementos();
773     }
774
775     bool HasSideEffects() const {
776       return Max(left_kind_, right_kind_) == GENERIC;
777     }
778
779     // Returns true if the IC should enable the inline smi code (i.e. if either
780     // parameter may be a smi).
781     bool UseInlinedSmiCode() const {
782       return KindMaybeSmi(left_kind_) || KindMaybeSmi(right_kind_);
783     }
784
785     static const int FIRST_TOKEN = Token::BIT_OR;
786     static const int LAST_TOKEN = Token::MOD;
787
788     Token::Value op() const { return op_; }
789     OverwriteMode mode() const { return mode_; }
790     Maybe<int> fixed_right_arg() const { return fixed_right_arg_; }
791
792     Type* GetLeftType(Zone* zone) const { return KindToType(left_kind_, zone); }
793     Type* GetRightType(Zone* zone) const {
794       return KindToType(right_kind_, zone);
795     }
796     Type* GetResultType(Zone* zone) const;
797
798     void Update(Handle<Object> left, Handle<Object> right,
799                 Handle<Object> result);
800
801     Isolate* isolate() const { return isolate_; }
802
803    private:
804     friend OStream& operator<<(OStream& os, const BinaryOpIC::State& s);
805
806     enum Kind { NONE, SMI, INT32, NUMBER, STRING, GENERIC };
807
808     Kind UpdateKind(Handle<Object> object, Kind kind) const;
809
810     static const char* KindToString(Kind kind);
811     static Type* KindToType(Kind kind, Zone* zone);
812     static bool KindMaybeSmi(Kind kind) {
813       return (kind >= SMI && kind <= NUMBER) || kind == GENERIC;
814     }
815
816     // We truncate the last bit of the token.
817     STATIC_ASSERT(LAST_TOKEN - FIRST_TOKEN < (1 << 4));
818     class OpField : public BitField<int, 0, 4> {};
819     class OverwriteModeField : public BitField<OverwriteMode, 4, 2> {};
820     class ResultKindField : public BitField<Kind, 6, 3> {};
821     class LeftKindField : public BitField<Kind, 9, 3> {};
822     // When fixed right arg is set, we don't need to store the right kind.
823     // Thus the two fields can overlap.
824     class HasFixedRightArgField : public BitField<bool, 12, 1> {};
825     class FixedRightArgValueField : public BitField<int, 13, 4> {};
826     class RightKindField : public BitField<Kind, 13, 3> {};
827
828     Token::Value op_;
829     OverwriteMode mode_;
830     Kind left_kind_;
831     Kind right_kind_;
832     Kind result_kind_;
833     Maybe<int> fixed_right_arg_;
834     Isolate* isolate_;
835   };
836
837   explicit BinaryOpIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
838
839   static Builtins::JavaScript TokenToJSBuiltin(Token::Value op);
840
841   MaybeHandle<Object> Transition(Handle<AllocationSite> allocation_site,
842                                  Handle<Object> left,
843                                  Handle<Object> right) V8_WARN_UNUSED_RESULT;
844 };
845
846
847 OStream& operator<<(OStream& os, const BinaryOpIC::State& s);
848
849
850 class CompareIC : public IC {
851  public:
852   // The type/state lattice is defined by the following inequations:
853   //   UNINITIALIZED < ...
854   //   ... < GENERIC
855   //   SMI < NUMBER
856   //   INTERNALIZED_STRING < STRING
857   //   KNOWN_OBJECT < OBJECT
858   enum State {
859     UNINITIALIZED,
860     SMI,
861     NUMBER,
862     STRING,
863     INTERNALIZED_STRING,
864     UNIQUE_NAME,   // Symbol or InternalizedString
865     OBJECT,        // JSObject
866     KNOWN_OBJECT,  // JSObject with specific map (faster check)
867     GENERIC
868   };
869
870   static State NewInputState(State old_state, Handle<Object> value);
871
872   static Type* StateToType(Zone* zone, State state,
873                            Handle<Map> map = Handle<Map>());
874
875   static void StubInfoToType(uint32_t stub_key, Type** left_type,
876                              Type** right_type, Type** overall_type,
877                              Handle<Map> map, Zone* zone);
878
879   CompareIC(Isolate* isolate, Token::Value op)
880       : IC(EXTRA_CALL_FRAME, isolate), op_(op) {}
881
882   // Update the inline cache for the given operands.
883   Code* UpdateCaches(Handle<Object> x, Handle<Object> y);
884
885
886   // Factory method for getting an uninitialized compare stub.
887   static Handle<Code> GetUninitialized(Isolate* isolate, Token::Value op);
888
889   // Helper function for computing the condition for a compare operation.
890   static Condition ComputeCondition(Token::Value op);
891
892   static const char* GetStateName(State state);
893
894  private:
895   static bool HasInlinedSmiCode(Address address);
896
897   State TargetState(State old_state, State old_left, State old_right,
898                     bool has_inlined_smi_code, Handle<Object> x,
899                     Handle<Object> y);
900
901   bool strict() const { return op_ == Token::EQ_STRICT; }
902   Condition GetCondition() const { return ComputeCondition(op_); }
903
904   static Code* GetRawUninitialized(Isolate* isolate, Token::Value op);
905
906   static void Clear(Isolate* isolate, Address address, Code* target,
907                     ConstantPoolArray* constant_pool);
908
909   Token::Value op_;
910
911   friend class IC;
912 };
913
914
915 class CompareNilIC : public IC {
916  public:
917   explicit CompareNilIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
918
919   Handle<Object> CompareNil(Handle<Object> object);
920
921   static Handle<Code> GetUninitialized();
922
923   static void Clear(Address address, Code* target,
924                     ConstantPoolArray* constant_pool);
925
926   static Handle<Object> DoCompareNilSlow(Isolate* isolate, NilValue nil,
927                                          Handle<Object> object);
928 };
929
930
931 class ToBooleanIC : public IC {
932  public:
933   explicit ToBooleanIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
934
935   Handle<Object> ToBoolean(Handle<Object> object);
936 };
937
938
939 // Helper for BinaryOpIC and CompareIC.
940 enum InlinedSmiCheck { ENABLE_INLINED_SMI_CHECK, DISABLE_INLINED_SMI_CHECK };
941 void PatchInlinedSmiCode(Address address, InlinedSmiCheck check);
942
943 DECLARE_RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure);
944 DECLARE_RUNTIME_FUNCTION(KeyedStoreIC_MissFromStubFailure);
945 DECLARE_RUNTIME_FUNCTION(UnaryOpIC_Miss);
946 DECLARE_RUNTIME_FUNCTION(StoreIC_MissFromStubFailure);
947 DECLARE_RUNTIME_FUNCTION(ElementsTransitionAndStoreIC_Miss);
948 DECLARE_RUNTIME_FUNCTION(BinaryOpIC_Miss);
949 DECLARE_RUNTIME_FUNCTION(BinaryOpIC_MissWithAllocationSite);
950 DECLARE_RUNTIME_FUNCTION(CompareNilIC_Miss);
951 DECLARE_RUNTIME_FUNCTION(ToBooleanIC_Miss);
952
953 // Support functions for callbacks handlers.
954 DECLARE_RUNTIME_FUNCTION(StoreCallbackProperty);
955
956 // Support functions for interceptor handlers.
957 DECLARE_RUNTIME_FUNCTION(LoadPropertyWithInterceptorOnly);
958 DECLARE_RUNTIME_FUNCTION(LoadPropertyWithInterceptor);
959 DECLARE_RUNTIME_FUNCTION(LoadElementWithInterceptor);
960 DECLARE_RUNTIME_FUNCTION(StorePropertyWithInterceptor);
961 }
962 }  // namespace v8::internal
963
964 #endif  // V8_IC_H_