2ae4ba7085e37e1a10b3c0a73acd623034324ac5
[platform/upstream/nodejs.git] / deps / v8 / src / code-stubs.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_CODE_STUBS_H_
6 #define V8_CODE_STUBS_H_
7
8 #include "src/allocation.h"
9 #include "src/assembler.h"
10 #include "src/codegen.h"
11 #include "src/globals.h"
12 #include "src/ic/ic-state.h"
13 #include "src/interface-descriptors.h"
14 #include "src/macro-assembler.h"
15 #include "src/ostreams.h"
16
17 namespace v8 {
18 namespace internal {
19
20 // List of code stubs used on all platforms.
21 #define CODE_STUB_LIST_ALL_PLATFORMS(V)     \
22   /* PlatformCodeStubs */                   \
23   V(ArgumentsAccess)                        \
24   V(ArrayConstructor)                       \
25   V(BinaryOpICWithAllocationSite)           \
26   V(CallApiFunction)                        \
27   V(CallApiAccessor)                        \
28   V(CallApiGetter)                          \
29   V(CallConstruct)                          \
30   V(CallFunction)                           \
31   V(CallIC)                                 \
32   V(CallIC_Array)                           \
33   V(CEntry)                                 \
34   V(CompareIC)                              \
35   V(DoubleToI)                              \
36   V(FunctionPrototype)                      \
37   V(Instanceof)                             \
38   V(InternalArrayConstructor)               \
39   V(JSEntry)                                \
40   V(KeyedLoadICTrampoline)                  \
41   V(LoadICTrampoline)                       \
42   V(CallICTrampoline)                       \
43   V(CallIC_ArrayTrampoline)                 \
44   V(LoadIndexedInterceptor)                 \
45   V(LoadIndexedString)                      \
46   V(MathPow)                                \
47   V(ProfileEntryHook)                       \
48   V(RecordWrite)                            \
49   V(RegExpExec)                             \
50   V(StoreArrayLiteralElement)               \
51   V(StoreBufferOverflow)                    \
52   V(StoreElement)                           \
53   V(StringCompare)                          \
54   V(StubFailureTrampoline)                  \
55   V(SubString)                              \
56   V(ToNumber)                               \
57   /* HydrogenCodeStubs */                   \
58   V(AllocateHeapNumber)                     \
59   V(ArrayNArgumentsConstructor)             \
60   V(ArrayNoArgumentConstructor)             \
61   V(ArraySingleArgumentConstructor)         \
62   V(BinaryOpIC)                             \
63   V(BinaryOpWithAllocationSite)             \
64   V(CompareNilIC)                           \
65   V(CreateAllocationSite)                   \
66   V(CreateWeakCell)                         \
67   V(ElementsTransitionAndStore)             \
68   V(FastCloneShallowArray)                  \
69   V(FastCloneShallowObject)                 \
70   V(FastNewClosure)                         \
71   V(FastNewContext)                         \
72   V(InternalArrayNArgumentsConstructor)     \
73   V(InternalArrayNoArgumentConstructor)     \
74   V(InternalArraySingleArgumentConstructor) \
75   V(KeyedLoadGeneric)                       \
76   V(LoadScriptContextField)                 \
77   V(LoadDictionaryElement)                  \
78   V(LoadFastElement)                        \
79   V(MegamorphicLoad)                        \
80   V(NameDictionaryLookup)                   \
81   V(NumberToString)                         \
82   V(RegExpConstructResult)                  \
83   V(StoreFastElement)                       \
84   V(StoreScriptContextField)                \
85   V(StringAdd)                              \
86   V(ToBoolean)                              \
87   V(TransitionElementsKind)                 \
88   V(VectorKeyedLoad)                        \
89   V(VectorLoad)                             \
90   /* IC Handler stubs */                    \
91   V(LoadConstant)                           \
92   V(LoadField)                              \
93   V(KeyedLoadSloppyArguments)               \
94   V(StoreField)                             \
95   V(StoreGlobal)                            \
96   V(StoreTransition)                        \
97   V(StringLength)                           \
98   V(RestParamAccess)
99
100 // List of code stubs only used on ARM 32 bits platforms.
101 #if V8_TARGET_ARCH_ARM
102 #define CODE_STUB_LIST_ARM(V) V(DirectCEntry)
103
104 #else
105 #define CODE_STUB_LIST_ARM(V)
106 #endif
107
108 // List of code stubs only used on ARM 64 bits platforms.
109 #if V8_TARGET_ARCH_ARM64
110 #define CODE_STUB_LIST_ARM64(V) \
111   V(DirectCEntry)               \
112   V(RestoreRegistersState)      \
113   V(StoreRegistersState)
114
115 #else
116 #define CODE_STUB_LIST_ARM64(V)
117 #endif
118
119 // List of code stubs only used on PPC platforms.
120 #ifdef V8_TARGET_ARCH_PPC
121 #define CODE_STUB_LIST_PPC(V) \
122   V(DirectCEntry)             \
123   V(StoreRegistersState)      \
124   V(RestoreRegistersState)
125 #else
126 #define CODE_STUB_LIST_PPC(V)
127 #endif
128
129 // List of code stubs only used on MIPS platforms.
130 #if V8_TARGET_ARCH_MIPS
131 #define CODE_STUB_LIST_MIPS(V) \
132   V(DirectCEntry)              \
133   V(RestoreRegistersState)     \
134   V(StoreRegistersState)
135 #elif V8_TARGET_ARCH_MIPS64
136 #define CODE_STUB_LIST_MIPS(V) \
137   V(DirectCEntry)              \
138   V(RestoreRegistersState)     \
139   V(StoreRegistersState)
140 #else
141 #define CODE_STUB_LIST_MIPS(V)
142 #endif
143
144 // Combined list of code stubs.
145 #define CODE_STUB_LIST(V)         \
146   CODE_STUB_LIST_ALL_PLATFORMS(V) \
147   CODE_STUB_LIST_ARM(V)           \
148   CODE_STUB_LIST_ARM64(V)         \
149   CODE_STUB_LIST_PPC(V)           \
150   CODE_STUB_LIST_MIPS(V)
151
152 // Stub is base classes of all stubs.
153 class CodeStub BASE_EMBEDDED {
154  public:
155   enum Major {
156     // TODO(mvstanton): eliminate the NoCache key by getting rid
157     //                  of the non-monomorphic-cache.
158     NoCache = 0,  // marker for stubs that do custom caching]
159 #define DEF_ENUM(name) name,
160     CODE_STUB_LIST(DEF_ENUM)
161 #undef DEF_ENUM
162     NUMBER_OF_IDS
163   };
164
165   // Retrieve the code for the stub. Generate the code if needed.
166   Handle<Code> GetCode();
167
168   // Retrieve the code for the stub, make and return a copy of the code.
169   Handle<Code> GetCodeCopy(const Code::FindAndReplacePattern& pattern);
170
171   static Major MajorKeyFromKey(uint32_t key) {
172     return static_cast<Major>(MajorKeyBits::decode(key));
173   }
174   static uint32_t MinorKeyFromKey(uint32_t key) {
175     return MinorKeyBits::decode(key);
176   }
177
178   // Gets the major key from a code object that is a code stub or binary op IC.
179   static Major GetMajorKey(Code* code_stub) {
180     return MajorKeyFromKey(code_stub->stub_key());
181   }
182
183   static uint32_t NoCacheKey() { return MajorKeyBits::encode(NoCache); }
184
185   static const char* MajorName(Major major_key, bool allow_unknown_keys);
186
187   explicit CodeStub(Isolate* isolate) : minor_key_(0), isolate_(isolate) {}
188   virtual ~CodeStub() {}
189
190   static void GenerateStubsAheadOfTime(Isolate* isolate);
191   static void GenerateFPStubs(Isolate* isolate);
192
193   // Some stubs put untagged junk on the stack that cannot be scanned by the
194   // GC.  This means that we must be statically sure that no GC can occur while
195   // they are running.  If that is the case they should override this to return
196   // true, which will cause an assertion if we try to call something that can
197   // GC or if we try to put a stack frame on top of the junk, which would not
198   // result in a traversable stack.
199   virtual bool SometimesSetsUpAFrame() { return true; }
200
201   // Lookup the code in the (possibly custom) cache.
202   bool FindCodeInCache(Code** code_out);
203
204   virtual CallInterfaceDescriptor GetCallInterfaceDescriptor() = 0;
205
206   virtual void InitializeDescriptor(CodeStubDescriptor* descriptor) {}
207
208   static void InitializeDescriptor(Isolate* isolate, uint32_t key,
209                                    CodeStubDescriptor* desc);
210
211   static MaybeHandle<Code> GetCode(Isolate* isolate, uint32_t key);
212
213   // Returns information for computing the number key.
214   virtual Major MajorKey() const = 0;
215   uint32_t MinorKey() const { return minor_key_; }
216
217   // BinaryOpStub needs to override this.
218   virtual Code::Kind GetCodeKind() const;
219
220   virtual InlineCacheState GetICState() const { return UNINITIALIZED; }
221   virtual ExtraICState GetExtraICState() const { return kNoExtraICState; }
222   virtual Code::StubType GetStubType() const { return Code::NORMAL; }
223
224   friend std::ostream& operator<<(std::ostream& os, const CodeStub& s) {
225     s.PrintName(os);
226     return os;
227   }
228
229   Isolate* isolate() const { return isolate_; }
230
231  protected:
232   CodeStub(uint32_t key, Isolate* isolate)
233       : minor_key_(MinorKeyFromKey(key)), isolate_(isolate) {}
234
235   // Generates the assembler code for the stub.
236   virtual Handle<Code> GenerateCode() = 0;
237
238   // Returns whether the code generated for this stub needs to be allocated as
239   // a fixed (non-moveable) code object.
240   virtual bool NeedsImmovableCode() { return false; }
241
242   virtual void PrintName(std::ostream& os) const;        // NOLINT
243   virtual void PrintBaseName(std::ostream& os) const;    // NOLINT
244   virtual void PrintState(std::ostream& os) const { ; }  // NOLINT
245
246   // Computes the key based on major and minor.
247   uint32_t GetKey() {
248     DCHECK(static_cast<int>(MajorKey()) < NUMBER_OF_IDS);
249     return MinorKeyBits::encode(MinorKey()) | MajorKeyBits::encode(MajorKey());
250   }
251
252   uint32_t minor_key_;
253
254  private:
255   // Perform bookkeeping required after code generation when stub code is
256   // initially generated.
257   void RecordCodeGeneration(Handle<Code> code);
258
259   // Finish the code object after it has been generated.
260   virtual void FinishCode(Handle<Code> code) { }
261
262   // Activate newly generated stub. Is called after
263   // registering stub in the stub cache.
264   virtual void Activate(Code* code) { }
265
266   // Add the code to a specialized cache, specific to an individual
267   // stub type. Please note, this method must add the code object to a
268   // roots object, otherwise we will remove the code during GC.
269   virtual void AddToSpecialCache(Handle<Code> new_object) { }
270
271   // Find code in a specialized cache, work is delegated to the specific stub.
272   virtual bool FindCodeInSpecialCache(Code** code_out) {
273     return false;
274   }
275
276   // If a stub uses a special cache override this.
277   virtual bool UseSpecialCache() { return false; }
278
279   // We use this dispatch to statically instantiate the correct code stub for
280   // the given stub key and call the passed function with that code stub.
281   typedef void (*DispatchedCall)(CodeStub* stub, void** value_out);
282   static void Dispatch(Isolate* isolate, uint32_t key, void** value_out,
283                        DispatchedCall call);
284
285   static void GetCodeDispatchCall(CodeStub* stub, void** value_out);
286
287   STATIC_ASSERT(NUMBER_OF_IDS < (1 << kStubMajorKeyBits));
288   class MajorKeyBits: public BitField<uint32_t, 0, kStubMajorKeyBits> {};
289   class MinorKeyBits: public BitField<uint32_t,
290       kStubMajorKeyBits, kStubMinorKeyBits> {};  // NOLINT
291
292   friend class BreakPointIterator;
293
294   Isolate* isolate_;
295 };
296
297
298 // TODO(svenpanne) This class is only used to construct a more or less sensible
299 // CompilationInfo for testing purposes, basically pretending that we are
300 // currently compiling some kind of code stub. Remove this when the pipeline and
301 // testing machinery is restructured in such a way that we don't have to come up
302 // with a CompilationInfo out of thin air, although we only need a few parts of
303 // it.
304 struct FakeStubForTesting : public CodeStub {
305   explicit FakeStubForTesting(Isolate* isolate) : CodeStub(isolate) {}
306
307   // Only used by pipeline.cc's GetDebugName in DEBUG mode.
308   Major MajorKey() const OVERRIDE { return CodeStub::NoCache; }
309
310   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
311     UNREACHABLE();
312     return CallInterfaceDescriptor();
313   }
314
315   Handle<Code> GenerateCode() OVERRIDE {
316     UNREACHABLE();
317     return Handle<Code>();
318   }
319 };
320
321
322 #define DEFINE_CODE_STUB_BASE(NAME, SUPER)                      \
323  public:                                                        \
324   NAME(uint32_t key, Isolate* isolate) : SUPER(key, isolate) {} \
325                                                                 \
326  private:                                                       \
327   DISALLOW_COPY_AND_ASSIGN(NAME)
328
329
330 #define DEFINE_CODE_STUB(NAME, SUPER)                      \
331  protected:                                                \
332   inline Major MajorKey() const OVERRIDE { return NAME; }; \
333   DEFINE_CODE_STUB_BASE(NAME##Stub, SUPER)
334
335
336 #define DEFINE_PLATFORM_CODE_STUB(NAME, SUPER)  \
337  private:                                       \
338   void Generate(MacroAssembler* masm) OVERRIDE; \
339   DEFINE_CODE_STUB(NAME, SUPER)
340
341
342 #define DEFINE_HYDROGEN_CODE_STUB(NAME, SUPER)                        \
343  public:                                                              \
344   void InitializeDescriptor(CodeStubDescriptor* descriptor) OVERRIDE; \
345   Handle<Code> GenerateCode() OVERRIDE;                               \
346   DEFINE_CODE_STUB(NAME, SUPER)
347
348 #define DEFINE_HANDLER_CODE_STUB(NAME, SUPER) \
349  public:                                      \
350   Handle<Code> GenerateCode() OVERRIDE;       \
351   DEFINE_CODE_STUB(NAME, SUPER)
352
353 #define DEFINE_CALL_INTERFACE_DESCRIPTOR(NAME)                    \
354  public:                                                          \
355   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE { \
356     return NAME##Descriptor(isolate());                           \
357   }
358
359 // There are some code stubs we just can't describe right now with a
360 // CallInterfaceDescriptor. Isolate behavior for those cases with this macro.
361 // An attempt to retrieve a descriptor will fail.
362 #define DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR()                   \
363  public:                                                          \
364   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE { \
365     UNREACHABLE();                                                \
366     return CallInterfaceDescriptor();                             \
367   }
368
369
370 class PlatformCodeStub : public CodeStub {
371  public:
372   // Retrieve the code for the stub. Generate the code if needed.
373   Handle<Code> GenerateCode() OVERRIDE;
374
375   Code::Kind GetCodeKind() const OVERRIDE { return Code::STUB; }
376
377  protected:
378   explicit PlatformCodeStub(Isolate* isolate) : CodeStub(isolate) {}
379
380   // Generates the assembler code for the stub.
381   virtual void Generate(MacroAssembler* masm) = 0;
382
383   DEFINE_CODE_STUB_BASE(PlatformCodeStub, CodeStub);
384 };
385
386
387 enum StubFunctionMode { NOT_JS_FUNCTION_STUB_MODE, JS_FUNCTION_STUB_MODE };
388 enum HandlerArgumentsMode { DONT_PASS_ARGUMENTS, PASS_ARGUMENTS };
389
390
391 class CodeStubDescriptor {
392  public:
393   explicit CodeStubDescriptor(CodeStub* stub);
394
395   CodeStubDescriptor(Isolate* isolate, uint32_t stub_key);
396
397   void Initialize(Address deoptimization_handler = NULL,
398                   int hint_stack_parameter_count = -1,
399                   StubFunctionMode function_mode = NOT_JS_FUNCTION_STUB_MODE);
400   void Initialize(Register stack_parameter_count,
401                   Address deoptimization_handler = NULL,
402                   int hint_stack_parameter_count = -1,
403                   StubFunctionMode function_mode = NOT_JS_FUNCTION_STUB_MODE,
404                   HandlerArgumentsMode handler_mode = DONT_PASS_ARGUMENTS);
405
406   void SetMissHandler(ExternalReference handler) {
407     miss_handler_ = handler;
408     has_miss_handler_ = true;
409     // Our miss handler infrastructure doesn't currently support
410     // variable stack parameter counts.
411     DCHECK(!stack_parameter_count_.is_valid());
412   }
413
414   void set_call_descriptor(CallInterfaceDescriptor d) { call_descriptor_ = d; }
415   CallInterfaceDescriptor call_descriptor() const { return call_descriptor_; }
416
417   int GetEnvironmentParameterCount() const {
418     return call_descriptor().GetEnvironmentParameterCount();
419   }
420
421   Representation GetEnvironmentParameterRepresentation(int index) const {
422     return call_descriptor().GetEnvironmentParameterRepresentation(index);
423   }
424
425   ExternalReference miss_handler() const {
426     DCHECK(has_miss_handler_);
427     return miss_handler_;
428   }
429
430   bool has_miss_handler() const {
431     return has_miss_handler_;
432   }
433
434   bool IsEnvironmentParameterCountRegister(int index) const {
435     return call_descriptor().GetEnvironmentParameterRegister(index).is(
436         stack_parameter_count_);
437   }
438
439   int GetHandlerParameterCount() const {
440     int params = call_descriptor().GetEnvironmentParameterCount();
441     if (handler_arguments_mode_ == PASS_ARGUMENTS) {
442       params += 1;
443     }
444     return params;
445   }
446
447   int hint_stack_parameter_count() const { return hint_stack_parameter_count_; }
448   Register stack_parameter_count() const { return stack_parameter_count_; }
449   StubFunctionMode function_mode() const { return function_mode_; }
450   Address deoptimization_handler() const { return deoptimization_handler_; }
451
452  private:
453   CallInterfaceDescriptor call_descriptor_;
454   Register stack_parameter_count_;
455   // If hint_stack_parameter_count_ > 0, the code stub can optimize the
456   // return sequence. Default value is -1, which means it is ignored.
457   int hint_stack_parameter_count_;
458   StubFunctionMode function_mode_;
459
460   Address deoptimization_handler_;
461   HandlerArgumentsMode handler_arguments_mode_;
462
463   ExternalReference miss_handler_;
464   bool has_miss_handler_;
465 };
466
467
468 class HydrogenCodeStub : public CodeStub {
469  public:
470   enum InitializationState {
471     UNINITIALIZED,
472     INITIALIZED
473   };
474
475   Code::Kind GetCodeKind() const OVERRIDE { return Code::STUB; }
476
477   template<class SubClass>
478   static Handle<Code> GetUninitialized(Isolate* isolate) {
479     SubClass::GenerateAheadOfTime(isolate);
480     return SubClass().GetCode(isolate);
481   }
482
483   // Retrieve the code for the stub. Generate the code if needed.
484   Handle<Code> GenerateCode() OVERRIDE = 0;
485
486   bool IsUninitialized() const { return IsMissBits::decode(minor_key_); }
487
488   Handle<Code> GenerateLightweightMissCode(ExternalReference miss);
489
490   template<class StateType>
491   void TraceTransition(StateType from, StateType to);
492
493  protected:
494   explicit HydrogenCodeStub(Isolate* isolate,
495                             InitializationState state = INITIALIZED)
496       : CodeStub(isolate) {
497     minor_key_ = IsMissBits::encode(state == UNINITIALIZED);
498   }
499
500   void set_sub_minor_key(uint32_t key) {
501     minor_key_ = SubMinorKeyBits::update(minor_key_, key);
502   }
503
504   uint32_t sub_minor_key() const { return SubMinorKeyBits::decode(minor_key_); }
505
506   static const int kSubMinorKeyBits = kStubMinorKeyBits - 1;
507
508  private:
509   class IsMissBits : public BitField<bool, kSubMinorKeyBits, 1> {};
510   class SubMinorKeyBits : public BitField<int, 0, kSubMinorKeyBits> {};
511
512   void GenerateLightweightMiss(MacroAssembler* masm, ExternalReference miss);
513
514   DEFINE_CODE_STUB_BASE(HydrogenCodeStub, CodeStub);
515 };
516
517
518 // Helper interface to prepare to/restore after making runtime calls.
519 class RuntimeCallHelper {
520  public:
521   virtual ~RuntimeCallHelper() {}
522
523   virtual void BeforeCall(MacroAssembler* masm) const = 0;
524
525   virtual void AfterCall(MacroAssembler* masm) const = 0;
526
527  protected:
528   RuntimeCallHelper() {}
529
530  private:
531   DISALLOW_COPY_AND_ASSIGN(RuntimeCallHelper);
532 };
533
534
535 } }  // namespace v8::internal
536
537 #if V8_TARGET_ARCH_IA32
538 #include "src/ia32/code-stubs-ia32.h"
539 #elif V8_TARGET_ARCH_X64
540 #include "src/x64/code-stubs-x64.h"
541 #elif V8_TARGET_ARCH_ARM64
542 #include "src/arm64/code-stubs-arm64.h"
543 #elif V8_TARGET_ARCH_ARM
544 #include "src/arm/code-stubs-arm.h"
545 #elif V8_TARGET_ARCH_PPC
546 #include "src/ppc/code-stubs-ppc.h"
547 #elif V8_TARGET_ARCH_MIPS
548 #include "src/mips/code-stubs-mips.h"
549 #elif V8_TARGET_ARCH_MIPS64
550 #include "src/mips64/code-stubs-mips64.h"
551 #elif V8_TARGET_ARCH_X87
552 #include "src/x87/code-stubs-x87.h"
553 #else
554 #error Unsupported target architecture.
555 #endif
556
557 namespace v8 {
558 namespace internal {
559
560
561 // RuntimeCallHelper implementation used in stubs: enters/leaves a
562 // newly created internal frame before/after the runtime call.
563 class StubRuntimeCallHelper : public RuntimeCallHelper {
564  public:
565   StubRuntimeCallHelper() {}
566
567   virtual void BeforeCall(MacroAssembler* masm) const;
568
569   virtual void AfterCall(MacroAssembler* masm) const;
570 };
571
572
573 // Trivial RuntimeCallHelper implementation.
574 class NopRuntimeCallHelper : public RuntimeCallHelper {
575  public:
576   NopRuntimeCallHelper() {}
577
578   virtual void BeforeCall(MacroAssembler* masm) const {}
579
580   virtual void AfterCall(MacroAssembler* masm) const {}
581 };
582
583
584 class NumberToStringStub FINAL : public HydrogenCodeStub {
585  public:
586   explicit NumberToStringStub(Isolate* isolate) : HydrogenCodeStub(isolate) {}
587
588   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
589   static const int kNumber = 0;
590
591   DEFINE_CALL_INTERFACE_DESCRIPTOR(NumberToString);
592   DEFINE_HYDROGEN_CODE_STUB(NumberToString, HydrogenCodeStub);
593 };
594
595
596 class FastNewClosureStub : public HydrogenCodeStub {
597  public:
598   FastNewClosureStub(Isolate* isolate, LanguageMode language_mode,
599                      FunctionKind kind)
600       : HydrogenCodeStub(isolate) {
601     DCHECK(IsValidFunctionKind(kind));
602     set_sub_minor_key(LanguageModeBits::encode(language_mode) |
603                       FunctionKindBits::encode(kind));
604   }
605
606   LanguageMode language_mode() const {
607     return LanguageModeBits::decode(sub_minor_key());
608   }
609
610   FunctionKind kind() const {
611     return FunctionKindBits::decode(sub_minor_key());
612   }
613
614  private:
615   STATIC_ASSERT(LANGUAGE_END == 3);
616   class LanguageModeBits : public BitField<LanguageMode, 0, 2> {};
617   class FunctionKindBits : public BitField<FunctionKind, 2, 7> {};
618
619   DEFINE_CALL_INTERFACE_DESCRIPTOR(FastNewClosure);
620   DEFINE_HYDROGEN_CODE_STUB(FastNewClosure, HydrogenCodeStub);
621 };
622
623
624 class FastNewContextStub FINAL : public HydrogenCodeStub {
625  public:
626   static const int kMaximumSlots = 64;
627
628   FastNewContextStub(Isolate* isolate, int slots) : HydrogenCodeStub(isolate) {
629     DCHECK(slots > 0 && slots <= kMaximumSlots);
630     set_sub_minor_key(SlotsBits::encode(slots));
631   }
632
633   int slots() const { return SlotsBits::decode(sub_minor_key()); }
634
635   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
636   static const int kFunction = 0;
637
638  private:
639   class SlotsBits : public BitField<int, 0, 8> {};
640
641   DEFINE_CALL_INTERFACE_DESCRIPTOR(FastNewContext);
642   DEFINE_HYDROGEN_CODE_STUB(FastNewContext, HydrogenCodeStub);
643 };
644
645
646 class FastCloneShallowArrayStub : public HydrogenCodeStub {
647  public:
648   FastCloneShallowArrayStub(Isolate* isolate,
649                             AllocationSiteMode allocation_site_mode)
650       : HydrogenCodeStub(isolate) {
651     set_sub_minor_key(AllocationSiteModeBits::encode(allocation_site_mode));
652   }
653
654   AllocationSiteMode allocation_site_mode() const {
655     return AllocationSiteModeBits::decode(sub_minor_key());
656   }
657
658  private:
659   class AllocationSiteModeBits: public BitField<AllocationSiteMode, 0, 1> {};
660
661   DEFINE_CALL_INTERFACE_DESCRIPTOR(FastCloneShallowArray);
662   DEFINE_HYDROGEN_CODE_STUB(FastCloneShallowArray, HydrogenCodeStub);
663 };
664
665
666 class FastCloneShallowObjectStub : public HydrogenCodeStub {
667  public:
668   // Maximum number of properties in copied object.
669   static const int kMaximumClonedProperties = 6;
670
671   FastCloneShallowObjectStub(Isolate* isolate, int length)
672       : HydrogenCodeStub(isolate) {
673     DCHECK_GE(length, 0);
674     DCHECK_LE(length, kMaximumClonedProperties);
675     set_sub_minor_key(LengthBits::encode(length));
676   }
677
678   int length() const { return LengthBits::decode(sub_minor_key()); }
679
680  private:
681   class LengthBits : public BitField<int, 0, 4> {};
682
683   DEFINE_CALL_INTERFACE_DESCRIPTOR(FastCloneShallowObject);
684   DEFINE_HYDROGEN_CODE_STUB(FastCloneShallowObject, HydrogenCodeStub);
685 };
686
687
688 class CreateAllocationSiteStub : public HydrogenCodeStub {
689  public:
690   explicit CreateAllocationSiteStub(Isolate* isolate)
691       : HydrogenCodeStub(isolate) { }
692
693   static void GenerateAheadOfTime(Isolate* isolate);
694
695   DEFINE_CALL_INTERFACE_DESCRIPTOR(CreateAllocationSite);
696   DEFINE_HYDROGEN_CODE_STUB(CreateAllocationSite, HydrogenCodeStub);
697 };
698
699
700 class CreateWeakCellStub : public HydrogenCodeStub {
701  public:
702   explicit CreateWeakCellStub(Isolate* isolate) : HydrogenCodeStub(isolate) {}
703
704   static void GenerateAheadOfTime(Isolate* isolate);
705
706   DEFINE_CALL_INTERFACE_DESCRIPTOR(CreateWeakCell);
707   DEFINE_HYDROGEN_CODE_STUB(CreateWeakCell, HydrogenCodeStub);
708 };
709
710
711 class InstanceofStub: public PlatformCodeStub {
712  public:
713   enum Flags {
714     kNoFlags = 0,
715     kArgsInRegisters = 1 << 0,
716     kCallSiteInlineCheck = 1 << 1,
717     kReturnTrueFalseObject = 1 << 2
718   };
719
720   InstanceofStub(Isolate* isolate, Flags flags) : PlatformCodeStub(isolate) {
721     minor_key_ = FlagBits::encode(flags);
722   }
723
724   static Register left() { return InstanceofDescriptor::left(); }
725   static Register right() { return InstanceofDescriptor::right(); }
726
727   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
728     if (HasArgsInRegisters()) {
729       return InstanceofDescriptor(isolate());
730     }
731     return ContextOnlyDescriptor(isolate());
732   }
733
734  private:
735   Flags flags() const { return FlagBits::decode(minor_key_); }
736
737   bool HasArgsInRegisters() const { return (flags() & kArgsInRegisters) != 0; }
738
739   bool HasCallSiteInlineCheck() const {
740     return (flags() & kCallSiteInlineCheck) != 0;
741   }
742
743   bool ReturnTrueFalseObject() const {
744     return (flags() & kReturnTrueFalseObject) != 0;
745   }
746
747   void PrintName(std::ostream& os) const OVERRIDE;  // NOLINT
748
749   class FlagBits : public BitField<Flags, 0, 3> {};
750
751   DEFINE_PLATFORM_CODE_STUB(Instanceof, PlatformCodeStub);
752 };
753
754
755 enum AllocationSiteOverrideMode {
756   DONT_OVERRIDE,
757   DISABLE_ALLOCATION_SITES,
758   LAST_ALLOCATION_SITE_OVERRIDE_MODE = DISABLE_ALLOCATION_SITES
759 };
760
761
762 class ArrayConstructorStub: public PlatformCodeStub {
763  public:
764   enum ArgumentCountKey { ANY, NONE, ONE, MORE_THAN_ONE };
765
766   ArrayConstructorStub(Isolate* isolate, int argument_count);
767
768   explicit ArrayConstructorStub(Isolate* isolate);
769
770  private:
771   ArgumentCountKey argument_count() const {
772     return ArgumentCountBits::decode(minor_key_);
773   }
774
775   void GenerateDispatchToArrayStub(MacroAssembler* masm,
776                                    AllocationSiteOverrideMode mode);
777
778   void PrintName(std::ostream& os) const OVERRIDE;  // NOLINT
779
780   class ArgumentCountBits : public BitField<ArgumentCountKey, 0, 2> {};
781
782   DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayConstructor);
783   DEFINE_PLATFORM_CODE_STUB(ArrayConstructor, PlatformCodeStub);
784 };
785
786
787 class InternalArrayConstructorStub: public PlatformCodeStub {
788  public:
789   explicit InternalArrayConstructorStub(Isolate* isolate);
790
791  private:
792   void GenerateCase(MacroAssembler* masm, ElementsKind kind);
793
794   DEFINE_CALL_INTERFACE_DESCRIPTOR(InternalArrayConstructor);
795   DEFINE_PLATFORM_CODE_STUB(InternalArrayConstructor, PlatformCodeStub);
796 };
797
798
799 class MathPowStub: public PlatformCodeStub {
800  public:
801   enum ExponentType { INTEGER, DOUBLE, TAGGED, ON_STACK };
802
803   MathPowStub(Isolate* isolate, ExponentType exponent_type)
804       : PlatformCodeStub(isolate) {
805     minor_key_ = ExponentTypeBits::encode(exponent_type);
806   }
807
808   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
809     if (exponent_type() == TAGGED) {
810       return MathPowTaggedDescriptor(isolate());
811     } else if (exponent_type() == INTEGER) {
812       return MathPowIntegerDescriptor(isolate());
813     }
814     // A CallInterfaceDescriptor doesn't specify double registers (yet).
815     return ContextOnlyDescriptor(isolate());
816   }
817
818  private:
819   ExponentType exponent_type() const {
820     return ExponentTypeBits::decode(minor_key_);
821   }
822
823   class ExponentTypeBits : public BitField<ExponentType, 0, 2> {};
824
825   DEFINE_PLATFORM_CODE_STUB(MathPow, PlatformCodeStub);
826 };
827
828
829 class CallICStub: public PlatformCodeStub {
830  public:
831   CallICStub(Isolate* isolate, const CallICState& state)
832       : PlatformCodeStub(isolate) {
833     minor_key_ = state.GetExtraICState();
834   }
835
836   static int ExtractArgcFromMinorKey(int minor_key) {
837     CallICState state(static_cast<ExtraICState>(minor_key));
838     return state.arg_count();
839   }
840
841   Code::Kind GetCodeKind() const OVERRIDE { return Code::CALL_IC; }
842
843   InlineCacheState GetICState() const OVERRIDE { return DEFAULT; }
844
845   ExtraICState GetExtraICState() const FINAL {
846     return static_cast<ExtraICState>(minor_key_);
847   }
848
849  protected:
850   bool CallAsMethod() const {
851     return state().call_type() == CallICState::METHOD;
852   }
853
854   int arg_count() const { return state().arg_count(); }
855
856   CallICState state() const {
857     return CallICState(static_cast<ExtraICState>(minor_key_));
858   }
859
860   // Code generation helpers.
861   void GenerateMiss(MacroAssembler* masm);
862
863  private:
864   void PrintState(std::ostream& os) const OVERRIDE;  // NOLINT
865
866   DEFINE_CALL_INTERFACE_DESCRIPTOR(CallFunctionWithFeedbackAndVector);
867   DEFINE_PLATFORM_CODE_STUB(CallIC, PlatformCodeStub);
868 };
869
870
871 class CallIC_ArrayStub: public CallICStub {
872  public:
873   CallIC_ArrayStub(Isolate* isolate, const CallICState& state_in)
874       : CallICStub(isolate, state_in) {}
875
876   InlineCacheState GetICState() const FINAL { return MONOMORPHIC; }
877
878  private:
879   void PrintState(std::ostream& os) const OVERRIDE;  // NOLINT
880
881   DEFINE_PLATFORM_CODE_STUB(CallIC_Array, CallICStub);
882 };
883
884
885 // TODO(verwaest): Translate to hydrogen code stub.
886 class FunctionPrototypeStub : public PlatformCodeStub {
887  public:
888   explicit FunctionPrototypeStub(Isolate* isolate)
889       : PlatformCodeStub(isolate) {}
890
891   Code::Kind GetCodeKind() const OVERRIDE { return Code::HANDLER; }
892
893   // TODO(mvstanton): only the receiver register is accessed. When this is
894   // translated to a hydrogen code stub, a new CallInterfaceDescriptor
895   // should be created that just uses that register for more efficient code.
896   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
897     if (FLAG_vector_ics) {
898       return VectorLoadICDescriptor(isolate());
899     }
900     return LoadDescriptor(isolate());
901   }
902
903   DEFINE_PLATFORM_CODE_STUB(FunctionPrototype, PlatformCodeStub);
904 };
905
906
907 // TODO(mvstanton): Translate to hydrogen code stub.
908 class LoadIndexedInterceptorStub : public PlatformCodeStub {
909  public:
910   explicit LoadIndexedInterceptorStub(Isolate* isolate)
911       : PlatformCodeStub(isolate) {}
912
913   Code::Kind GetCodeKind() const OVERRIDE { return Code::HANDLER; }
914   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
915
916   DEFINE_CALL_INTERFACE_DESCRIPTOR(Load);
917   DEFINE_PLATFORM_CODE_STUB(LoadIndexedInterceptor, PlatformCodeStub);
918 };
919
920
921 class LoadIndexedStringStub : public PlatformCodeStub {
922  public:
923   explicit LoadIndexedStringStub(Isolate* isolate)
924       : PlatformCodeStub(isolate) {}
925
926   Code::Kind GetCodeKind() const OVERRIDE { return Code::HANDLER; }
927   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
928
929   DEFINE_CALL_INTERFACE_DESCRIPTOR(Load);
930   DEFINE_PLATFORM_CODE_STUB(LoadIndexedString, PlatformCodeStub);
931 };
932
933
934 class HandlerStub : public HydrogenCodeStub {
935  public:
936   Code::Kind GetCodeKind() const OVERRIDE { return Code::HANDLER; }
937   ExtraICState GetExtraICState() const OVERRIDE { return kind(); }
938   InlineCacheState GetICState() const OVERRIDE { return MONOMORPHIC; }
939
940   void InitializeDescriptor(CodeStubDescriptor* descriptor) OVERRIDE;
941
942   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE;
943
944  protected:
945   explicit HandlerStub(Isolate* isolate) : HydrogenCodeStub(isolate) {}
946
947   virtual Code::Kind kind() const = 0;
948
949   DEFINE_CODE_STUB_BASE(HandlerStub, HydrogenCodeStub);
950 };
951
952
953 class LoadFieldStub: public HandlerStub {
954  public:
955   LoadFieldStub(Isolate* isolate, FieldIndex index) : HandlerStub(isolate) {
956     int property_index_key = index.GetFieldAccessStubKey();
957     set_sub_minor_key(LoadFieldByIndexBits::encode(property_index_key));
958   }
959
960   FieldIndex index() const {
961     int property_index_key = LoadFieldByIndexBits::decode(sub_minor_key());
962     return FieldIndex::FromFieldAccessStubKey(property_index_key);
963   }
964
965  protected:
966   Code::Kind kind() const OVERRIDE { return Code::LOAD_IC; }
967   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
968
969  private:
970   class LoadFieldByIndexBits : public BitField<int, 0, 13> {};
971
972   DEFINE_HANDLER_CODE_STUB(LoadField, HandlerStub);
973 };
974
975
976 class KeyedLoadSloppyArgumentsStub : public HandlerStub {
977  public:
978   explicit KeyedLoadSloppyArgumentsStub(Isolate* isolate)
979       : HandlerStub(isolate) {}
980
981  protected:
982   Code::Kind kind() const OVERRIDE { return Code::KEYED_LOAD_IC; }
983   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
984
985  private:
986   DEFINE_HANDLER_CODE_STUB(KeyedLoadSloppyArguments, HandlerStub);
987 };
988
989
990 class LoadConstantStub : public HandlerStub {
991  public:
992   LoadConstantStub(Isolate* isolate, int constant_index)
993       : HandlerStub(isolate) {
994     set_sub_minor_key(ConstantIndexBits::encode(constant_index));
995   }
996
997   int constant_index() const {
998     return ConstantIndexBits::decode(sub_minor_key());
999   }
1000
1001  protected:
1002   Code::Kind kind() const OVERRIDE { return Code::LOAD_IC; }
1003   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
1004
1005  private:
1006   class ConstantIndexBits : public BitField<int, 0, kSubMinorKeyBits> {};
1007
1008   DEFINE_HANDLER_CODE_STUB(LoadConstant, HandlerStub);
1009 };
1010
1011
1012 class StringLengthStub: public HandlerStub {
1013  public:
1014   explicit StringLengthStub(Isolate* isolate) : HandlerStub(isolate) {}
1015
1016  protected:
1017   Code::Kind kind() const OVERRIDE { return Code::LOAD_IC; }
1018   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
1019
1020   DEFINE_HANDLER_CODE_STUB(StringLength, HandlerStub);
1021 };
1022
1023
1024 class StoreFieldStub : public HandlerStub {
1025  public:
1026   StoreFieldStub(Isolate* isolate, FieldIndex index,
1027                  Representation representation)
1028       : HandlerStub(isolate) {
1029     int property_index_key = index.GetFieldAccessStubKey();
1030     uint8_t repr = PropertyDetails::EncodeRepresentation(representation);
1031     set_sub_minor_key(StoreFieldByIndexBits::encode(property_index_key) |
1032                       RepresentationBits::encode(repr));
1033   }
1034
1035   FieldIndex index() const {
1036     int property_index_key = StoreFieldByIndexBits::decode(sub_minor_key());
1037     return FieldIndex::FromFieldAccessStubKey(property_index_key);
1038   }
1039
1040   Representation representation() {
1041     uint8_t repr = RepresentationBits::decode(sub_minor_key());
1042     return PropertyDetails::DecodeRepresentation(repr);
1043   }
1044
1045  protected:
1046   Code::Kind kind() const OVERRIDE { return Code::STORE_IC; }
1047   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
1048
1049  private:
1050   class StoreFieldByIndexBits : public BitField<int, 0, 13> {};
1051   class RepresentationBits : public BitField<uint8_t, 13, 4> {};
1052
1053   DEFINE_HANDLER_CODE_STUB(StoreField, HandlerStub);
1054 };
1055
1056
1057 class StoreTransitionStub : public HandlerStub {
1058  public:
1059   enum StoreMode {
1060     StoreMapOnly,
1061     StoreMapAndValue,
1062     ExtendStorageAndStoreMapAndValue
1063   };
1064
1065   explicit StoreTransitionStub(Isolate* isolate) : HandlerStub(isolate) {
1066     set_sub_minor_key(StoreModeBits::encode(StoreMapOnly));
1067   }
1068
1069   StoreTransitionStub(Isolate* isolate, FieldIndex index,
1070                       Representation representation, StoreMode store_mode)
1071       : HandlerStub(isolate) {
1072     DCHECK(store_mode != StoreMapOnly);
1073     int property_index_key = index.GetFieldAccessStubKey();
1074     uint8_t repr = PropertyDetails::EncodeRepresentation(representation);
1075     set_sub_minor_key(StoreFieldByIndexBits::encode(property_index_key) |
1076                       RepresentationBits::encode(repr) |
1077                       StoreModeBits::encode(store_mode));
1078   }
1079
1080   FieldIndex index() const {
1081     DCHECK(store_mode() != StoreMapOnly);
1082     int property_index_key = StoreFieldByIndexBits::decode(sub_minor_key());
1083     return FieldIndex::FromFieldAccessStubKey(property_index_key);
1084   }
1085
1086   Representation representation() {
1087     DCHECK(store_mode() != StoreMapOnly);
1088     uint8_t repr = RepresentationBits::decode(sub_minor_key());
1089     return PropertyDetails::DecodeRepresentation(repr);
1090   }
1091
1092   StoreMode store_mode() const {
1093     return StoreModeBits::decode(sub_minor_key());
1094   }
1095
1096   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE;
1097
1098  protected:
1099   Code::Kind kind() const OVERRIDE { return Code::STORE_IC; }
1100   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
1101
1102  private:
1103   class StoreFieldByIndexBits : public BitField<int, 0, 13> {};
1104   class RepresentationBits : public BitField<uint8_t, 13, 4> {};
1105   class StoreModeBits : public BitField<StoreMode, 17, 2> {};
1106
1107   DEFINE_HANDLER_CODE_STUB(StoreTransition, HandlerStub);
1108 };
1109
1110
1111 class StoreGlobalStub : public HandlerStub {
1112  public:
1113   StoreGlobalStub(Isolate* isolate, bool is_constant, bool check_global)
1114       : HandlerStub(isolate) {
1115     set_sub_minor_key(IsConstantBits::encode(is_constant) |
1116                       CheckGlobalBits::encode(check_global));
1117   }
1118
1119   static Handle<HeapObject> property_cell_placeholder(Isolate* isolate) {
1120     return isolate->factory()->uninitialized_value();
1121   }
1122
1123   Handle<Code> GetCodeCopyFromTemplate(Handle<GlobalObject> global,
1124                                        Handle<PropertyCell> cell) {
1125     if (check_global()) {
1126       Code::FindAndReplacePattern pattern;
1127       pattern.Add(isolate()->factory()->meta_map(),
1128                   Map::WeakCellForMap(Handle<Map>(global->map())));
1129       pattern.Add(Handle<Map>(property_cell_placeholder(isolate())->map()),
1130                   isolate()->factory()->NewWeakCell(cell));
1131       return CodeStub::GetCodeCopy(pattern);
1132     } else {
1133       Code::FindAndReplacePattern pattern;
1134       pattern.Add(Handle<Map>(property_cell_placeholder(isolate())->map()),
1135                   isolate()->factory()->NewWeakCell(cell));
1136       return CodeStub::GetCodeCopy(pattern);
1137     }
1138   }
1139
1140   Code::Kind kind() const OVERRIDE { return Code::STORE_IC; }
1141
1142   bool is_constant() const { return IsConstantBits::decode(sub_minor_key()); }
1143
1144   bool check_global() const { return CheckGlobalBits::decode(sub_minor_key()); }
1145
1146   void set_is_constant(bool value) {
1147     set_sub_minor_key(IsConstantBits::update(sub_minor_key(), value));
1148   }
1149
1150   Representation representation() {
1151     return Representation::FromKind(
1152         RepresentationBits::decode(sub_minor_key()));
1153   }
1154
1155   void set_representation(Representation r) {
1156     set_sub_minor_key(RepresentationBits::update(sub_minor_key(), r.kind()));
1157   }
1158
1159  private:
1160   class IsConstantBits: public BitField<bool, 0, 1> {};
1161   class RepresentationBits: public BitField<Representation::Kind, 1, 8> {};
1162   class CheckGlobalBits: public BitField<bool, 9, 1> {};
1163
1164   DEFINE_HANDLER_CODE_STUB(StoreGlobal, HandlerStub);
1165 };
1166
1167
1168 class CallApiFunctionStub : public PlatformCodeStub {
1169  public:
1170   explicit CallApiFunctionStub(Isolate* isolate, bool call_data_undefined)
1171       : PlatformCodeStub(isolate) {
1172     minor_key_ = CallDataUndefinedBits::encode(call_data_undefined);
1173   }
1174
1175  private:
1176   bool call_data_undefined() const {
1177     return CallDataUndefinedBits::decode(minor_key_);
1178   }
1179
1180   class CallDataUndefinedBits : public BitField<bool, 0, 1> {};
1181
1182   DEFINE_CALL_INTERFACE_DESCRIPTOR(ApiFunction);
1183   DEFINE_PLATFORM_CODE_STUB(CallApiFunction, PlatformCodeStub);
1184 };
1185
1186
1187 class CallApiAccessorStub : public PlatformCodeStub {
1188  public:
1189   CallApiAccessorStub(Isolate* isolate, bool is_store, bool call_data_undefined)
1190       : PlatformCodeStub(isolate) {
1191     minor_key_ = IsStoreBits::encode(is_store) |
1192                  CallDataUndefinedBits::encode(call_data_undefined) |
1193                  ArgumentBits::encode(is_store ? 1 : 0);
1194   }
1195
1196  protected:
1197   // For CallApiFunctionWithFixedArgsStub, see below.
1198   static const int kArgBits = 3;
1199   CallApiAccessorStub(Isolate* isolate, int argc, bool call_data_undefined)
1200       : PlatformCodeStub(isolate) {
1201     minor_key_ = IsStoreBits::encode(false) |
1202                  CallDataUndefinedBits::encode(call_data_undefined) |
1203                  ArgumentBits::encode(argc);
1204   }
1205
1206  private:
1207   bool is_store() const { return IsStoreBits::decode(minor_key_); }
1208   bool call_data_undefined() const {
1209     return CallDataUndefinedBits::decode(minor_key_);
1210   }
1211   int argc() const { return ArgumentBits::decode(minor_key_); }
1212
1213   class IsStoreBits: public BitField<bool, 0, 1> {};
1214   class CallDataUndefinedBits: public BitField<bool, 1, 1> {};
1215   class ArgumentBits : public BitField<int, 2, kArgBits> {};
1216
1217   DEFINE_CALL_INTERFACE_DESCRIPTOR(ApiAccessor);
1218   DEFINE_PLATFORM_CODE_STUB(CallApiAccessor, PlatformCodeStub);
1219 };
1220
1221
1222 // TODO(dcarney): see if it's possible to remove this later without performance
1223 // degradation.
1224 // This is not a real stub, but a way of generating the CallApiAccessorStub
1225 // (which has the same abi) which makes it clear that it is not an accessor.
1226 class CallApiFunctionWithFixedArgsStub : public CallApiAccessorStub {
1227  public:
1228   static const int kMaxFixedArgs = (1 << kArgBits) - 1;
1229   CallApiFunctionWithFixedArgsStub(Isolate* isolate, int argc,
1230                                    bool call_data_undefined)
1231       : CallApiAccessorStub(isolate, argc, call_data_undefined) {
1232     DCHECK(0 <= argc && argc <= kMaxFixedArgs);
1233   }
1234 };
1235
1236
1237 typedef ApiAccessorDescriptor ApiFunctionWithFixedArgsDescriptor;
1238
1239
1240 class CallApiGetterStub : public PlatformCodeStub {
1241  public:
1242   explicit CallApiGetterStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
1243
1244   DEFINE_CALL_INTERFACE_DESCRIPTOR(ApiGetter);
1245   DEFINE_PLATFORM_CODE_STUB(CallApiGetter, PlatformCodeStub);
1246 };
1247
1248
1249 class BinaryOpICStub : public HydrogenCodeStub {
1250  public:
1251   BinaryOpICStub(Isolate* isolate, Token::Value op)
1252       : HydrogenCodeStub(isolate, UNINITIALIZED) {
1253     BinaryOpICState state(isolate, op);
1254     set_sub_minor_key(state.GetExtraICState());
1255   }
1256
1257   BinaryOpICStub(Isolate* isolate, const BinaryOpICState& state)
1258       : HydrogenCodeStub(isolate) {
1259     set_sub_minor_key(state.GetExtraICState());
1260   }
1261
1262   static void GenerateAheadOfTime(Isolate* isolate);
1263
1264   Code::Kind GetCodeKind() const OVERRIDE { return Code::BINARY_OP_IC; }
1265
1266   InlineCacheState GetICState() const FINAL { return state().GetICState(); }
1267
1268   ExtraICState GetExtraICState() const FINAL {
1269     return static_cast<ExtraICState>(sub_minor_key());
1270   }
1271
1272   BinaryOpICState state() const {
1273     return BinaryOpICState(isolate(), GetExtraICState());
1274   }
1275
1276   void PrintState(std::ostream& os) const FINAL;  // NOLINT
1277
1278   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
1279   static const int kLeft = 0;
1280   static const int kRight = 1;
1281
1282  private:
1283   static void GenerateAheadOfTime(Isolate* isolate,
1284                                   const BinaryOpICState& state);
1285
1286   DEFINE_CALL_INTERFACE_DESCRIPTOR(BinaryOp);
1287   DEFINE_HYDROGEN_CODE_STUB(BinaryOpIC, HydrogenCodeStub);
1288 };
1289
1290
1291 // TODO(bmeurer): Merge this into the BinaryOpICStub once we have proper tail
1292 // call support for stubs in Hydrogen.
1293 class BinaryOpICWithAllocationSiteStub FINAL : public PlatformCodeStub {
1294  public:
1295   BinaryOpICWithAllocationSiteStub(Isolate* isolate,
1296                                    const BinaryOpICState& state)
1297       : PlatformCodeStub(isolate) {
1298     minor_key_ = state.GetExtraICState();
1299   }
1300
1301   static void GenerateAheadOfTime(Isolate* isolate);
1302
1303   Handle<Code> GetCodeCopyFromTemplate(Handle<AllocationSite> allocation_site) {
1304     Code::FindAndReplacePattern pattern;
1305     pattern.Add(isolate()->factory()->undefined_map(), allocation_site);
1306     return CodeStub::GetCodeCopy(pattern);
1307   }
1308
1309   Code::Kind GetCodeKind() const OVERRIDE { return Code::BINARY_OP_IC; }
1310
1311   InlineCacheState GetICState() const OVERRIDE { return state().GetICState(); }
1312
1313   ExtraICState GetExtraICState() const OVERRIDE {
1314     return static_cast<ExtraICState>(minor_key_);
1315   }
1316
1317   void PrintState(std::ostream& os) const OVERRIDE;  // NOLINT
1318
1319  private:
1320   BinaryOpICState state() const {
1321     return BinaryOpICState(isolate(), static_cast<ExtraICState>(minor_key_));
1322   }
1323
1324   static void GenerateAheadOfTime(Isolate* isolate,
1325                                   const BinaryOpICState& state);
1326
1327   DEFINE_CALL_INTERFACE_DESCRIPTOR(BinaryOpWithAllocationSite);
1328   DEFINE_PLATFORM_CODE_STUB(BinaryOpICWithAllocationSite, PlatformCodeStub);
1329 };
1330
1331
1332 class BinaryOpWithAllocationSiteStub FINAL : public BinaryOpICStub {
1333  public:
1334   BinaryOpWithAllocationSiteStub(Isolate* isolate, Token::Value op)
1335       : BinaryOpICStub(isolate, op) {}
1336
1337   BinaryOpWithAllocationSiteStub(Isolate* isolate, const BinaryOpICState& state)
1338       : BinaryOpICStub(isolate, state) {}
1339
1340   Code::Kind GetCodeKind() const FINAL { return Code::STUB; }
1341
1342   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
1343   static const int kAllocationSite = 0;
1344   static const int kLeft = 1;
1345   static const int kRight = 2;
1346
1347   DEFINE_CALL_INTERFACE_DESCRIPTOR(BinaryOpWithAllocationSite);
1348   DEFINE_HYDROGEN_CODE_STUB(BinaryOpWithAllocationSite, BinaryOpICStub);
1349 };
1350
1351
1352 enum StringAddFlags {
1353   // Omit both parameter checks.
1354   STRING_ADD_CHECK_NONE = 0,
1355   // Check left parameter.
1356   STRING_ADD_CHECK_LEFT = 1 << 0,
1357   // Check right parameter.
1358   STRING_ADD_CHECK_RIGHT = 1 << 1,
1359   // Check both parameters.
1360   STRING_ADD_CHECK_BOTH = STRING_ADD_CHECK_LEFT | STRING_ADD_CHECK_RIGHT
1361 };
1362
1363
1364 class StringAddStub FINAL : public HydrogenCodeStub {
1365  public:
1366   StringAddStub(Isolate* isolate, StringAddFlags flags,
1367                 PretenureFlag pretenure_flag)
1368       : HydrogenCodeStub(isolate) {
1369     set_sub_minor_key(StringAddFlagsBits::encode(flags) |
1370                       PretenureFlagBits::encode(pretenure_flag));
1371   }
1372
1373   StringAddFlags flags() const {
1374     return StringAddFlagsBits::decode(sub_minor_key());
1375   }
1376
1377   PretenureFlag pretenure_flag() const {
1378     return PretenureFlagBits::decode(sub_minor_key());
1379   }
1380
1381   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
1382   static const int kLeft = 0;
1383   static const int kRight = 1;
1384
1385  private:
1386   class StringAddFlagsBits: public BitField<StringAddFlags, 0, 2> {};
1387   class PretenureFlagBits: public BitField<PretenureFlag, 2, 1> {};
1388
1389   void PrintBaseName(std::ostream& os) const OVERRIDE;  // NOLINT
1390
1391   DEFINE_CALL_INTERFACE_DESCRIPTOR(StringAdd);
1392   DEFINE_HYDROGEN_CODE_STUB(StringAdd, HydrogenCodeStub);
1393 };
1394
1395
1396 class CompareICStub : public PlatformCodeStub {
1397  public:
1398   CompareICStub(Isolate* isolate, Token::Value op, CompareICState::State left,
1399                 CompareICState::State right, CompareICState::State state)
1400       : PlatformCodeStub(isolate) {
1401     DCHECK(Token::IsCompareOp(op));
1402     minor_key_ = OpBits::encode(op - Token::EQ) | LeftStateBits::encode(left) |
1403                  RightStateBits::encode(right) | StateBits::encode(state);
1404   }
1405
1406   void set_known_map(Handle<Map> map) { known_map_ = map; }
1407
1408   InlineCacheState GetICState() const OVERRIDE;
1409
1410   Token::Value op() const {
1411     return static_cast<Token::Value>(Token::EQ + OpBits::decode(minor_key_));
1412   }
1413
1414   CompareICState::State left() const {
1415     return LeftStateBits::decode(minor_key_);
1416   }
1417   CompareICState::State right() const {
1418     return RightStateBits::decode(minor_key_);
1419   }
1420   CompareICState::State state() const { return StateBits::decode(minor_key_); }
1421
1422  private:
1423   Code::Kind GetCodeKind() const OVERRIDE { return Code::COMPARE_IC; }
1424
1425   void GenerateSmis(MacroAssembler* masm);
1426   void GenerateNumbers(MacroAssembler* masm);
1427   void GenerateInternalizedStrings(MacroAssembler* masm);
1428   void GenerateStrings(MacroAssembler* masm);
1429   void GenerateUniqueNames(MacroAssembler* masm);
1430   void GenerateObjects(MacroAssembler* masm);
1431   void GenerateMiss(MacroAssembler* masm);
1432   void GenerateKnownObjects(MacroAssembler* masm);
1433   void GenerateGeneric(MacroAssembler* masm);
1434
1435   bool strict() const { return op() == Token::EQ_STRICT; }
1436   Condition GetCondition() const;
1437
1438   void AddToSpecialCache(Handle<Code> new_object) OVERRIDE;
1439   bool FindCodeInSpecialCache(Code** code_out) OVERRIDE;
1440   bool UseSpecialCache() OVERRIDE {
1441     return state() == CompareICState::KNOWN_OBJECT;
1442   }
1443
1444   class OpBits : public BitField<int, 0, 3> {};
1445   class LeftStateBits : public BitField<CompareICState::State, 3, 4> {};
1446   class RightStateBits : public BitField<CompareICState::State, 7, 4> {};
1447   class StateBits : public BitField<CompareICState::State, 11, 4> {};
1448
1449   Handle<Map> known_map_;
1450
1451   DEFINE_CALL_INTERFACE_DESCRIPTOR(BinaryOp);
1452   DEFINE_PLATFORM_CODE_STUB(CompareIC, PlatformCodeStub);
1453 };
1454
1455
1456 class CompareNilICStub : public HydrogenCodeStub  {
1457  public:
1458   Type* GetType(Zone* zone, Handle<Map> map = Handle<Map>());
1459   Type* GetInputType(Zone* zone, Handle<Map> map);
1460
1461   CompareNilICStub(Isolate* isolate, NilValue nil) : HydrogenCodeStub(isolate) {
1462     set_sub_minor_key(NilValueBits::encode(nil));
1463   }
1464
1465   CompareNilICStub(Isolate* isolate, ExtraICState ic_state,
1466                    InitializationState init_state = INITIALIZED)
1467       : HydrogenCodeStub(isolate, init_state) {
1468     set_sub_minor_key(ic_state);
1469   }
1470
1471   static Handle<Code> GetUninitialized(Isolate* isolate,
1472                                        NilValue nil) {
1473     return CompareNilICStub(isolate, nil, UNINITIALIZED).GetCode();
1474   }
1475
1476   InlineCacheState GetICState() const OVERRIDE {
1477     State state = this->state();
1478     if (state.Contains(GENERIC)) {
1479       return MEGAMORPHIC;
1480     } else if (state.Contains(MONOMORPHIC_MAP)) {
1481       return MONOMORPHIC;
1482     } else {
1483       return PREMONOMORPHIC;
1484     }
1485   }
1486
1487   Code::Kind GetCodeKind() const OVERRIDE { return Code::COMPARE_NIL_IC; }
1488
1489   ExtraICState GetExtraICState() const OVERRIDE { return sub_minor_key(); }
1490
1491   void UpdateStatus(Handle<Object> object);
1492
1493   bool IsMonomorphic() const { return state().Contains(MONOMORPHIC_MAP); }
1494
1495   NilValue nil_value() const { return NilValueBits::decode(sub_minor_key()); }
1496
1497   void ClearState() {
1498     set_sub_minor_key(TypesBits::update(sub_minor_key(), 0));
1499   }
1500
1501   void PrintState(std::ostream& os) const OVERRIDE;     // NOLINT
1502   void PrintBaseName(std::ostream& os) const OVERRIDE;  // NOLINT
1503
1504  private:
1505   CompareNilICStub(Isolate* isolate, NilValue nil,
1506                    InitializationState init_state)
1507       : HydrogenCodeStub(isolate, init_state) {
1508     set_sub_minor_key(NilValueBits::encode(nil));
1509   }
1510
1511   enum CompareNilType {
1512     UNDEFINED,
1513     NULL_TYPE,
1514     MONOMORPHIC_MAP,
1515     GENERIC,
1516     NUMBER_OF_TYPES
1517   };
1518
1519   // At most 6 different types can be distinguished, because the Code object
1520   // only has room for a single byte to hold a set and there are two more
1521   // boolean flags we need to store. :-P
1522   STATIC_ASSERT(NUMBER_OF_TYPES <= 6);
1523
1524   class State : public EnumSet<CompareNilType, byte> {
1525    public:
1526     State() : EnumSet<CompareNilType, byte>(0) { }
1527     explicit State(byte bits) : EnumSet<CompareNilType, byte>(bits) { }
1528   };
1529   friend std::ostream& operator<<(std::ostream& os, const State& s);
1530
1531   State state() const { return State(TypesBits::decode(sub_minor_key())); }
1532
1533   class NilValueBits : public BitField<NilValue, 0, 1> {};
1534   class TypesBits : public BitField<byte, 1, NUMBER_OF_TYPES> {};
1535
1536   friend class CompareNilIC;
1537
1538   DEFINE_CALL_INTERFACE_DESCRIPTOR(CompareNil);
1539   DEFINE_HYDROGEN_CODE_STUB(CompareNilIC, HydrogenCodeStub);
1540 };
1541
1542
1543 std::ostream& operator<<(std::ostream& os, const CompareNilICStub::State& s);
1544
1545
1546 class CEntryStub : public PlatformCodeStub {
1547  public:
1548   CEntryStub(Isolate* isolate, int result_size,
1549              SaveFPRegsMode save_doubles = kDontSaveFPRegs)
1550       : PlatformCodeStub(isolate) {
1551     minor_key_ = SaveDoublesBits::encode(save_doubles == kSaveFPRegs);
1552     DCHECK(result_size == 1 || result_size == 2);
1553 #if _WIN64 || V8_TARGET_ARCH_PPC
1554     minor_key_ = ResultSizeBits::update(minor_key_, result_size);
1555 #endif  // _WIN64
1556   }
1557
1558   // The version of this stub that doesn't save doubles is generated ahead of
1559   // time, so it's OK to call it from other stubs that can't cope with GC during
1560   // their code generation.  On machines that always have gp registers (x64) we
1561   // can generate both variants ahead of time.
1562   static void GenerateAheadOfTime(Isolate* isolate);
1563
1564  private:
1565   bool save_doubles() const { return SaveDoublesBits::decode(minor_key_); }
1566 #if _WIN64 || V8_TARGET_ARCH_PPC
1567   int result_size() const { return ResultSizeBits::decode(minor_key_); }
1568 #endif  // _WIN64
1569
1570   bool NeedsImmovableCode() OVERRIDE;
1571
1572   class SaveDoublesBits : public BitField<bool, 0, 1> {};
1573   class ResultSizeBits : public BitField<int, 1, 3> {};
1574
1575   DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
1576   DEFINE_PLATFORM_CODE_STUB(CEntry, PlatformCodeStub);
1577 };
1578
1579
1580 class JSEntryStub : public PlatformCodeStub {
1581  public:
1582   JSEntryStub(Isolate* isolate, StackFrame::Type type)
1583       : PlatformCodeStub(isolate) {
1584     DCHECK(type == StackFrame::ENTRY || type == StackFrame::ENTRY_CONSTRUCT);
1585     minor_key_ = StackFrameTypeBits::encode(type);
1586   }
1587
1588  private:
1589   void FinishCode(Handle<Code> code) OVERRIDE;
1590
1591   void PrintName(std::ostream& os) const OVERRIDE {  // NOLINT
1592     os << (type() == StackFrame::ENTRY ? "JSEntryStub"
1593                                        : "JSConstructEntryStub");
1594   }
1595
1596   StackFrame::Type type() const {
1597     return StackFrameTypeBits::decode(minor_key_);
1598   }
1599
1600   class StackFrameTypeBits : public BitField<StackFrame::Type, 0, 5> {};
1601
1602   int handler_offset_;
1603
1604   DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
1605   DEFINE_PLATFORM_CODE_STUB(JSEntry, PlatformCodeStub);
1606 };
1607
1608
1609 class ArgumentsAccessStub: public PlatformCodeStub {
1610  public:
1611   enum Type {
1612     READ_ELEMENT,
1613     NEW_SLOPPY_FAST,
1614     NEW_SLOPPY_SLOW,
1615     NEW_STRICT
1616   };
1617
1618   enum HasNewTarget { NO_NEW_TARGET, HAS_NEW_TARGET };
1619
1620   ArgumentsAccessStub(Isolate* isolate, Type type,
1621                       HasNewTarget has_new_target = NO_NEW_TARGET)
1622       : PlatformCodeStub(isolate) {
1623     minor_key_ =
1624         TypeBits::encode(type) | HasNewTargetBits::encode(has_new_target);
1625   }
1626
1627   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
1628     if (type() == READ_ELEMENT) {
1629       return ArgumentsAccessReadDescriptor(isolate());
1630     }
1631     return ContextOnlyDescriptor(isolate());
1632   }
1633
1634  private:
1635   Type type() const { return TypeBits::decode(minor_key_); }
1636   bool has_new_target() const {
1637     return HasNewTargetBits::decode(minor_key_) == HAS_NEW_TARGET;
1638   }
1639
1640   void GenerateReadElement(MacroAssembler* masm);
1641   void GenerateNewStrict(MacroAssembler* masm);
1642   void GenerateNewSloppyFast(MacroAssembler* masm);
1643   void GenerateNewSloppySlow(MacroAssembler* masm);
1644
1645   void PrintName(std::ostream& os) const OVERRIDE;  // NOLINT
1646
1647   class TypeBits : public BitField<Type, 0, 2> {};
1648   class HasNewTargetBits : public BitField<HasNewTarget, 2, 1> {};
1649
1650   DEFINE_PLATFORM_CODE_STUB(ArgumentsAccess, PlatformCodeStub);
1651 };
1652
1653
1654 class RestParamAccessStub: public PlatformCodeStub {
1655  public:
1656   explicit RestParamAccessStub(Isolate* isolate) : PlatformCodeStub(isolate) { }
1657
1658   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
1659     return ContextOnlyDescriptor(isolate());
1660   }
1661
1662  private:
1663   void GenerateNew(MacroAssembler* masm);
1664
1665   virtual void PrintName(std::ostream& os) const OVERRIDE;  // NOLINT
1666
1667   DEFINE_PLATFORM_CODE_STUB(RestParamAccess, PlatformCodeStub);
1668 };
1669
1670
1671 class RegExpExecStub: public PlatformCodeStub {
1672  public:
1673   explicit RegExpExecStub(Isolate* isolate) : PlatformCodeStub(isolate) { }
1674
1675   DEFINE_CALL_INTERFACE_DESCRIPTOR(ContextOnly);
1676   DEFINE_PLATFORM_CODE_STUB(RegExpExec, PlatformCodeStub);
1677 };
1678
1679
1680 class RegExpConstructResultStub FINAL : public HydrogenCodeStub {
1681  public:
1682   explicit RegExpConstructResultStub(Isolate* isolate)
1683       : HydrogenCodeStub(isolate) { }
1684
1685   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
1686   static const int kLength = 0;
1687   static const int kIndex = 1;
1688   static const int kInput = 2;
1689
1690   DEFINE_CALL_INTERFACE_DESCRIPTOR(RegExpConstructResult);
1691   DEFINE_HYDROGEN_CODE_STUB(RegExpConstructResult, HydrogenCodeStub);
1692 };
1693
1694
1695 class CallFunctionStub: public PlatformCodeStub {
1696  public:
1697   CallFunctionStub(Isolate* isolate, int argc, CallFunctionFlags flags)
1698       : PlatformCodeStub(isolate) {
1699     DCHECK(argc >= 0 && argc <= Code::kMaxArguments);
1700     minor_key_ = ArgcBits::encode(argc) | FlagBits::encode(flags);
1701   }
1702
1703   static int ExtractArgcFromMinorKey(int minor_key) {
1704     return ArgcBits::decode(minor_key);
1705   }
1706
1707  private:
1708   int argc() const { return ArgcBits::decode(minor_key_); }
1709   int flags() const { return FlagBits::decode(minor_key_); }
1710
1711   bool CallAsMethod() const {
1712     return flags() == CALL_AS_METHOD || flags() == WRAP_AND_CALL;
1713   }
1714
1715   bool NeedsChecks() const { return flags() != WRAP_AND_CALL; }
1716
1717   void PrintName(std::ostream& os) const OVERRIDE;  // NOLINT
1718
1719   // Minor key encoding in 32 bits with Bitfield <Type, shift, size>.
1720   class FlagBits : public BitField<CallFunctionFlags, 0, 2> {};
1721   class ArgcBits : public BitField<unsigned, 2, Code::kArgumentsBits> {};
1722   STATIC_ASSERT(Code::kArgumentsBits + 2 <= kStubMinorKeyBits);
1723
1724   DEFINE_CALL_INTERFACE_DESCRIPTOR(CallFunction);
1725   DEFINE_PLATFORM_CODE_STUB(CallFunction, PlatformCodeStub);
1726 };
1727
1728
1729 class CallConstructStub: public PlatformCodeStub {
1730  public:
1731   CallConstructStub(Isolate* isolate, CallConstructorFlags flags)
1732       : PlatformCodeStub(isolate) {
1733     minor_key_ = FlagBits::encode(flags);
1734   }
1735
1736   void FinishCode(Handle<Code> code) OVERRIDE {
1737     code->set_has_function_cache(RecordCallTarget());
1738   }
1739
1740  private:
1741   CallConstructorFlags flags() const { return FlagBits::decode(minor_key_); }
1742
1743   bool RecordCallTarget() const {
1744     return (flags() & RECORD_CONSTRUCTOR_TARGET) != 0;
1745   }
1746
1747   bool IsSuperConstructorCall() const {
1748     return (flags() & SUPER_CONSTRUCTOR_CALL) != 0;
1749   }
1750
1751   void PrintName(std::ostream& os) const OVERRIDE;  // NOLINT
1752
1753   class FlagBits : public BitField<CallConstructorFlags, 0, 2> {};
1754
1755   DEFINE_CALL_INTERFACE_DESCRIPTOR(CallConstruct);
1756   DEFINE_PLATFORM_CODE_STUB(CallConstruct, PlatformCodeStub);
1757 };
1758
1759
1760 enum StringIndexFlags {
1761   // Accepts smis or heap numbers.
1762   STRING_INDEX_IS_NUMBER,
1763
1764   // Accepts smis or heap numbers that are valid array indices
1765   // (ECMA-262 15.4). Invalid indices are reported as being out of
1766   // range.
1767   STRING_INDEX_IS_ARRAY_INDEX
1768 };
1769
1770
1771 enum ReceiverCheckMode {
1772   // We don't know anything about the receiver.
1773   RECEIVER_IS_UNKNOWN,
1774
1775   // We know the receiver is a string.
1776   RECEIVER_IS_STRING
1777 };
1778
1779
1780 // Generates code implementing String.prototype.charCodeAt.
1781 //
1782 // Only supports the case when the receiver is a string and the index
1783 // is a number (smi or heap number) that is a valid index into the
1784 // string. Additional index constraints are specified by the
1785 // flags. Otherwise, bails out to the provided labels.
1786 //
1787 // Register usage: |object| may be changed to another string in a way
1788 // that doesn't affect charCodeAt/charAt semantics, |index| is
1789 // preserved, |scratch| and |result| are clobbered.
1790 class StringCharCodeAtGenerator {
1791  public:
1792   StringCharCodeAtGenerator(Register object, Register index, Register result,
1793                             Label* receiver_not_string, Label* index_not_number,
1794                             Label* index_out_of_range,
1795                             StringIndexFlags index_flags,
1796                             ReceiverCheckMode check_mode = RECEIVER_IS_UNKNOWN)
1797       : object_(object),
1798         index_(index),
1799         result_(result),
1800         receiver_not_string_(receiver_not_string),
1801         index_not_number_(index_not_number),
1802         index_out_of_range_(index_out_of_range),
1803         index_flags_(index_flags),
1804         check_mode_(check_mode) {
1805     DCHECK(!result_.is(object_));
1806     DCHECK(!result_.is(index_));
1807   }
1808
1809   // Generates the fast case code. On the fallthrough path |result|
1810   // register contains the result.
1811   void GenerateFast(MacroAssembler* masm);
1812
1813   // Generates the slow case code. Must not be naturally
1814   // reachable. Expected to be put after a ret instruction (e.g., in
1815   // deferred code). Always jumps back to the fast case.
1816   void GenerateSlow(MacroAssembler* masm,
1817                     const RuntimeCallHelper& call_helper);
1818
1819   // Skip handling slow case and directly jump to bailout.
1820   void SkipSlow(MacroAssembler* masm, Label* bailout) {
1821     masm->bind(&index_not_smi_);
1822     masm->bind(&call_runtime_);
1823     masm->jmp(bailout);
1824   }
1825
1826  private:
1827   Register object_;
1828   Register index_;
1829   Register result_;
1830
1831   Label* receiver_not_string_;
1832   Label* index_not_number_;
1833   Label* index_out_of_range_;
1834
1835   StringIndexFlags index_flags_;
1836   ReceiverCheckMode check_mode_;
1837
1838   Label call_runtime_;
1839   Label index_not_smi_;
1840   Label got_smi_index_;
1841   Label exit_;
1842
1843   DISALLOW_COPY_AND_ASSIGN(StringCharCodeAtGenerator);
1844 };
1845
1846
1847 // Generates code for creating a one-char string from a char code.
1848 class StringCharFromCodeGenerator {
1849  public:
1850   StringCharFromCodeGenerator(Register code,
1851                               Register result)
1852       : code_(code),
1853         result_(result) {
1854     DCHECK(!code_.is(result_));
1855   }
1856
1857   // Generates the fast case code. On the fallthrough path |result|
1858   // register contains the result.
1859   void GenerateFast(MacroAssembler* masm);
1860
1861   // Generates the slow case code. Must not be naturally
1862   // reachable. Expected to be put after a ret instruction (e.g., in
1863   // deferred code). Always jumps back to the fast case.
1864   void GenerateSlow(MacroAssembler* masm,
1865                     const RuntimeCallHelper& call_helper);
1866
1867   // Skip handling slow case and directly jump to bailout.
1868   void SkipSlow(MacroAssembler* masm, Label* bailout) {
1869     masm->bind(&slow_case_);
1870     masm->jmp(bailout);
1871   }
1872
1873  private:
1874   Register code_;
1875   Register result_;
1876
1877   Label slow_case_;
1878   Label exit_;
1879
1880   DISALLOW_COPY_AND_ASSIGN(StringCharFromCodeGenerator);
1881 };
1882
1883
1884 // Generates code implementing String.prototype.charAt.
1885 //
1886 // Only supports the case when the receiver is a string and the index
1887 // is a number (smi or heap number) that is a valid index into the
1888 // string. Additional index constraints are specified by the
1889 // flags. Otherwise, bails out to the provided labels.
1890 //
1891 // Register usage: |object| may be changed to another string in a way
1892 // that doesn't affect charCodeAt/charAt semantics, |index| is
1893 // preserved, |scratch1|, |scratch2|, and |result| are clobbered.
1894 class StringCharAtGenerator {
1895  public:
1896   StringCharAtGenerator(Register object, Register index, Register scratch,
1897                         Register result, Label* receiver_not_string,
1898                         Label* index_not_number, Label* index_out_of_range,
1899                         StringIndexFlags index_flags,
1900                         ReceiverCheckMode check_mode = RECEIVER_IS_UNKNOWN)
1901       : char_code_at_generator_(object, index, scratch, receiver_not_string,
1902                                 index_not_number, index_out_of_range,
1903                                 index_flags, check_mode),
1904         char_from_code_generator_(scratch, result) {}
1905
1906   // Generates the fast case code. On the fallthrough path |result|
1907   // register contains the result.
1908   void GenerateFast(MacroAssembler* masm) {
1909     char_code_at_generator_.GenerateFast(masm);
1910     char_from_code_generator_.GenerateFast(masm);
1911   }
1912
1913   // Generates the slow case code. Must not be naturally
1914   // reachable. Expected to be put after a ret instruction (e.g., in
1915   // deferred code). Always jumps back to the fast case.
1916   void GenerateSlow(MacroAssembler* masm,
1917                     const RuntimeCallHelper& call_helper) {
1918     char_code_at_generator_.GenerateSlow(masm, call_helper);
1919     char_from_code_generator_.GenerateSlow(masm, call_helper);
1920   }
1921
1922   // Skip handling slow case and directly jump to bailout.
1923   void SkipSlow(MacroAssembler* masm, Label* bailout) {
1924     char_code_at_generator_.SkipSlow(masm, bailout);
1925     char_from_code_generator_.SkipSlow(masm, bailout);
1926   }
1927
1928  private:
1929   StringCharCodeAtGenerator char_code_at_generator_;
1930   StringCharFromCodeGenerator char_from_code_generator_;
1931
1932   DISALLOW_COPY_AND_ASSIGN(StringCharAtGenerator);
1933 };
1934
1935
1936 class LoadDictionaryElementStub : public HydrogenCodeStub {
1937  public:
1938   explicit LoadDictionaryElementStub(Isolate* isolate)
1939       : HydrogenCodeStub(isolate) {}
1940
1941   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
1942     if (FLAG_vector_ics) {
1943       return VectorLoadICDescriptor(isolate());
1944     }
1945     return LoadDescriptor(isolate());
1946   }
1947
1948   DEFINE_HYDROGEN_CODE_STUB(LoadDictionaryElement, HydrogenCodeStub);
1949 };
1950
1951
1952 class KeyedLoadGenericStub : public HydrogenCodeStub {
1953  public:
1954   explicit KeyedLoadGenericStub(Isolate* isolate) : HydrogenCodeStub(isolate) {}
1955
1956   Code::Kind GetCodeKind() const OVERRIDE { return Code::KEYED_LOAD_IC; }
1957   InlineCacheState GetICState() const OVERRIDE { return GENERIC; }
1958
1959   // Since KeyedLoadGeneric stub doesn't miss (simply calls runtime), it
1960   // doesn't need to use the VectorLoadICDescriptor for the case when
1961   // flag --vector-ics is true.
1962   DEFINE_CALL_INTERFACE_DESCRIPTOR(Load);
1963
1964   DEFINE_HYDROGEN_CODE_STUB(KeyedLoadGeneric, HydrogenCodeStub);
1965 };
1966
1967
1968 class LoadICTrampolineStub : public PlatformCodeStub {
1969  public:
1970   LoadICTrampolineStub(Isolate* isolate, const LoadICState& state)
1971       : PlatformCodeStub(isolate) {
1972     minor_key_ = state.GetExtraICState();
1973   }
1974
1975   Code::Kind GetCodeKind() const OVERRIDE { return Code::LOAD_IC; }
1976
1977   InlineCacheState GetICState() const FINAL { return DEFAULT; }
1978
1979   ExtraICState GetExtraICState() const FINAL {
1980     return static_cast<ExtraICState>(minor_key_);
1981   }
1982
1983  private:
1984   LoadICState state() const {
1985     return LoadICState(static_cast<ExtraICState>(minor_key_));
1986   }
1987
1988   DEFINE_CALL_INTERFACE_DESCRIPTOR(VectorLoadICTrampoline);
1989   DEFINE_PLATFORM_CODE_STUB(LoadICTrampoline, PlatformCodeStub);
1990 };
1991
1992
1993 class KeyedLoadICTrampolineStub : public LoadICTrampolineStub {
1994  public:
1995   explicit KeyedLoadICTrampolineStub(Isolate* isolate)
1996       : LoadICTrampolineStub(isolate, LoadICState(0)) {}
1997
1998   Code::Kind GetCodeKind() const OVERRIDE { return Code::KEYED_LOAD_IC; }
1999
2000   DEFINE_PLATFORM_CODE_STUB(KeyedLoadICTrampoline, LoadICTrampolineStub);
2001 };
2002
2003
2004 class CallICTrampolineStub : public PlatformCodeStub {
2005  public:
2006   CallICTrampolineStub(Isolate* isolate, const CallICState& state)
2007       : PlatformCodeStub(isolate) {
2008     minor_key_ = state.GetExtraICState();
2009   }
2010
2011   Code::Kind GetCodeKind() const OVERRIDE { return Code::CALL_IC; }
2012
2013   InlineCacheState GetICState() const FINAL { return DEFAULT; }
2014
2015   ExtraICState GetExtraICState() const FINAL {
2016     return static_cast<ExtraICState>(minor_key_);
2017   }
2018
2019  protected:
2020   CallICState state() const {
2021     return CallICState(static_cast<ExtraICState>(minor_key_));
2022   }
2023
2024   DEFINE_CALL_INTERFACE_DESCRIPTOR(CallFunctionWithFeedback);
2025   DEFINE_PLATFORM_CODE_STUB(CallICTrampoline, PlatformCodeStub);
2026 };
2027
2028
2029 class CallIC_ArrayTrampolineStub : public CallICTrampolineStub {
2030  public:
2031   CallIC_ArrayTrampolineStub(Isolate* isolate, const CallICState& state)
2032       : CallICTrampolineStub(isolate, state) {}
2033
2034  private:
2035   DEFINE_PLATFORM_CODE_STUB(CallIC_ArrayTrampoline, CallICTrampolineStub);
2036 };
2037
2038
2039 class MegamorphicLoadStub : public HydrogenCodeStub {
2040  public:
2041   MegamorphicLoadStub(Isolate* isolate, const LoadICState& state)
2042       : HydrogenCodeStub(isolate) {
2043     set_sub_minor_key(state.GetExtraICState());
2044   }
2045
2046   Code::Kind GetCodeKind() const OVERRIDE { return Code::LOAD_IC; }
2047
2048   InlineCacheState GetICState() const FINAL { return MEGAMORPHIC; }
2049
2050   ExtraICState GetExtraICState() const FINAL {
2051     return static_cast<ExtraICState>(sub_minor_key());
2052   }
2053
2054   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
2055     if (FLAG_vector_ics) {
2056       return VectorLoadICDescriptor(isolate());
2057     }
2058     return LoadDescriptor(isolate());
2059   }
2060
2061   DEFINE_HYDROGEN_CODE_STUB(MegamorphicLoad, HydrogenCodeStub);
2062 };
2063
2064
2065 class VectorLoadStub : public HydrogenCodeStub {
2066  public:
2067   explicit VectorLoadStub(Isolate* isolate, const LoadICState& state)
2068       : HydrogenCodeStub(isolate) {
2069     set_sub_minor_key(state.GetExtraICState());
2070   }
2071
2072   Code::Kind GetCodeKind() const OVERRIDE { return Code::LOAD_IC; }
2073
2074   InlineCacheState GetICState() const FINAL { return DEFAULT; }
2075
2076   ExtraICState GetExtraICState() const FINAL {
2077     return static_cast<ExtraICState>(sub_minor_key());
2078   }
2079
2080  private:
2081   LoadICState state() const { return LoadICState(GetExtraICState()); }
2082
2083   DEFINE_CALL_INTERFACE_DESCRIPTOR(VectorLoadIC);
2084   DEFINE_HYDROGEN_CODE_STUB(VectorLoad, HydrogenCodeStub);
2085 };
2086
2087
2088 class VectorKeyedLoadStub : public VectorLoadStub {
2089  public:
2090   explicit VectorKeyedLoadStub(Isolate* isolate)
2091       : VectorLoadStub(isolate, LoadICState(0)) {}
2092
2093   Code::Kind GetCodeKind() const OVERRIDE { return Code::KEYED_LOAD_IC; }
2094
2095   DEFINE_CALL_INTERFACE_DESCRIPTOR(VectorLoadIC);
2096   DEFINE_HYDROGEN_CODE_STUB(VectorKeyedLoad, VectorLoadStub);
2097 };
2098
2099
2100 class DoubleToIStub : public PlatformCodeStub {
2101  public:
2102   DoubleToIStub(Isolate* isolate, Register source, Register destination,
2103                 int offset, bool is_truncating, bool skip_fastpath = false)
2104       : PlatformCodeStub(isolate) {
2105     minor_key_ = SourceRegisterBits::encode(source.code()) |
2106                  DestinationRegisterBits::encode(destination.code()) |
2107                  OffsetBits::encode(offset) |
2108                  IsTruncatingBits::encode(is_truncating) |
2109                  SkipFastPathBits::encode(skip_fastpath) |
2110                  SSE3Bits::encode(CpuFeatures::IsSupported(SSE3) ? 1 : 0);
2111   }
2112
2113   bool SometimesSetsUpAFrame() OVERRIDE { return false; }
2114
2115  private:
2116   Register source() const {
2117     return Register::from_code(SourceRegisterBits::decode(minor_key_));
2118   }
2119   Register destination() const {
2120     return Register::from_code(DestinationRegisterBits::decode(minor_key_));
2121   }
2122   bool is_truncating() const { return IsTruncatingBits::decode(minor_key_); }
2123   bool skip_fastpath() const { return SkipFastPathBits::decode(minor_key_); }
2124   int offset() const { return OffsetBits::decode(minor_key_); }
2125
2126   static const int kBitsPerRegisterNumber = 6;
2127   STATIC_ASSERT((1L << kBitsPerRegisterNumber) >= Register::kNumRegisters);
2128   class SourceRegisterBits:
2129       public BitField<int, 0, kBitsPerRegisterNumber> {};  // NOLINT
2130   class DestinationRegisterBits:
2131       public BitField<int, kBitsPerRegisterNumber,
2132         kBitsPerRegisterNumber> {};  // NOLINT
2133   class IsTruncatingBits:
2134       public BitField<bool, 2 * kBitsPerRegisterNumber, 1> {};  // NOLINT
2135   class OffsetBits:
2136       public BitField<int, 2 * kBitsPerRegisterNumber + 1, 3> {};  // NOLINT
2137   class SkipFastPathBits:
2138       public BitField<int, 2 * kBitsPerRegisterNumber + 4, 1> {};  // NOLINT
2139   class SSE3Bits:
2140       public BitField<int, 2 * kBitsPerRegisterNumber + 5, 1> {};  // NOLINT
2141
2142   DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
2143   DEFINE_PLATFORM_CODE_STUB(DoubleToI, PlatformCodeStub);
2144 };
2145
2146
2147 class ScriptContextFieldStub : public HandlerStub {
2148  public:
2149   ScriptContextFieldStub(Isolate* isolate,
2150                          const ScriptContextTable::LookupResult* lookup_result)
2151       : HandlerStub(isolate) {
2152     DCHECK(Accepted(lookup_result));
2153     set_sub_minor_key(ContextIndexBits::encode(lookup_result->context_index) |
2154                       SlotIndexBits::encode(lookup_result->slot_index));
2155   }
2156
2157   int context_index() const {
2158     return ContextIndexBits::decode(sub_minor_key());
2159   }
2160
2161   int slot_index() const { return SlotIndexBits::decode(sub_minor_key()); }
2162
2163   static bool Accepted(const ScriptContextTable::LookupResult* lookup_result) {
2164     return ContextIndexBits::is_valid(lookup_result->context_index) &&
2165            SlotIndexBits::is_valid(lookup_result->slot_index);
2166   }
2167
2168  private:
2169   static const int kContextIndexBits = 13;
2170   static const int kSlotIndexBits = 13;
2171   class ContextIndexBits : public BitField<int, 0, kContextIndexBits> {};
2172   class SlotIndexBits
2173       : public BitField<int, kContextIndexBits, kSlotIndexBits> {};
2174
2175   Code::StubType GetStubType() const OVERRIDE { return Code::FAST; }
2176
2177   DEFINE_CODE_STUB_BASE(ScriptContextFieldStub, HandlerStub);
2178 };
2179
2180
2181 class LoadScriptContextFieldStub : public ScriptContextFieldStub {
2182  public:
2183   LoadScriptContextFieldStub(
2184       Isolate* isolate, const ScriptContextTable::LookupResult* lookup_result)
2185       : ScriptContextFieldStub(isolate, lookup_result) {}
2186
2187  private:
2188   Code::Kind kind() const OVERRIDE { return Code::LOAD_IC; }
2189
2190   DEFINE_HANDLER_CODE_STUB(LoadScriptContextField, ScriptContextFieldStub);
2191 };
2192
2193
2194 class StoreScriptContextFieldStub : public ScriptContextFieldStub {
2195  public:
2196   StoreScriptContextFieldStub(
2197       Isolate* isolate, const ScriptContextTable::LookupResult* lookup_result)
2198       : ScriptContextFieldStub(isolate, lookup_result) {}
2199
2200  private:
2201   Code::Kind kind() const OVERRIDE { return Code::STORE_IC; }
2202
2203   DEFINE_HANDLER_CODE_STUB(StoreScriptContextField, ScriptContextFieldStub);
2204 };
2205
2206
2207 class LoadFastElementStub : public HydrogenCodeStub {
2208  public:
2209   LoadFastElementStub(Isolate* isolate, bool is_js_array,
2210                       ElementsKind elements_kind)
2211       : HydrogenCodeStub(isolate) {
2212     set_sub_minor_key(ElementsKindBits::encode(elements_kind) |
2213                       IsJSArrayBits::encode(is_js_array));
2214   }
2215
2216   bool is_js_array() const { return IsJSArrayBits::decode(sub_minor_key()); }
2217
2218   ElementsKind elements_kind() const {
2219     return ElementsKindBits::decode(sub_minor_key());
2220   }
2221
2222  private:
2223   class ElementsKindBits: public BitField<ElementsKind, 0, 8> {};
2224   class IsJSArrayBits: public BitField<bool, 8, 1> {};
2225
2226   CallInterfaceDescriptor GetCallInterfaceDescriptor() OVERRIDE {
2227     if (FLAG_vector_ics) {
2228       return VectorLoadICDescriptor(isolate());
2229     }
2230     return LoadDescriptor(isolate());
2231   }
2232
2233   DEFINE_HYDROGEN_CODE_STUB(LoadFastElement, HydrogenCodeStub);
2234 };
2235
2236
2237 class StoreFastElementStub : public HydrogenCodeStub {
2238  public:
2239   StoreFastElementStub(Isolate* isolate, bool is_js_array,
2240                        ElementsKind elements_kind, KeyedAccessStoreMode mode)
2241       : HydrogenCodeStub(isolate) {
2242     set_sub_minor_key(ElementsKindBits::encode(elements_kind) |
2243                       IsJSArrayBits::encode(is_js_array) |
2244                       StoreModeBits::encode(mode));
2245   }
2246
2247   bool is_js_array() const { return IsJSArrayBits::decode(sub_minor_key()); }
2248
2249   ElementsKind elements_kind() const {
2250     return ElementsKindBits::decode(sub_minor_key());
2251   }
2252
2253   KeyedAccessStoreMode store_mode() const {
2254     return StoreModeBits::decode(sub_minor_key());
2255   }
2256
2257  private:
2258   class ElementsKindBits: public BitField<ElementsKind,      0, 8> {};
2259   class StoreModeBits: public BitField<KeyedAccessStoreMode, 8, 4> {};
2260   class IsJSArrayBits: public BitField<bool,                12, 1> {};
2261
2262   DEFINE_CALL_INTERFACE_DESCRIPTOR(Store);
2263   DEFINE_HYDROGEN_CODE_STUB(StoreFastElement, HydrogenCodeStub);
2264 };
2265
2266
2267 class TransitionElementsKindStub : public HydrogenCodeStub {
2268  public:
2269   TransitionElementsKindStub(Isolate* isolate,
2270                              ElementsKind from_kind,
2271                              ElementsKind to_kind,
2272                              bool is_js_array) : HydrogenCodeStub(isolate) {
2273     set_sub_minor_key(FromKindBits::encode(from_kind) |
2274                       ToKindBits::encode(to_kind) |
2275                       IsJSArrayBits::encode(is_js_array));
2276   }
2277
2278   ElementsKind from_kind() const {
2279     return FromKindBits::decode(sub_minor_key());
2280   }
2281
2282   ElementsKind to_kind() const { return ToKindBits::decode(sub_minor_key()); }
2283
2284   bool is_js_array() const { return IsJSArrayBits::decode(sub_minor_key()); }
2285
2286  private:
2287   class FromKindBits: public BitField<ElementsKind, 8, 8> {};
2288   class ToKindBits: public BitField<ElementsKind, 0, 8> {};
2289   class IsJSArrayBits: public BitField<bool, 16, 1> {};
2290
2291   DEFINE_CALL_INTERFACE_DESCRIPTOR(TransitionElementsKind);
2292   DEFINE_HYDROGEN_CODE_STUB(TransitionElementsKind, HydrogenCodeStub);
2293 };
2294
2295
2296 class AllocateHeapNumberStub FINAL : public HydrogenCodeStub {
2297  public:
2298   explicit AllocateHeapNumberStub(Isolate* isolate)
2299       : HydrogenCodeStub(isolate) {}
2300
2301  private:
2302   DEFINE_CALL_INTERFACE_DESCRIPTOR(AllocateHeapNumber);
2303   DEFINE_HYDROGEN_CODE_STUB(AllocateHeapNumber, HydrogenCodeStub);
2304 };
2305
2306
2307 class ArrayConstructorStubBase : public HydrogenCodeStub {
2308  public:
2309   ArrayConstructorStubBase(Isolate* isolate,
2310                            ElementsKind kind,
2311                            AllocationSiteOverrideMode override_mode)
2312       : HydrogenCodeStub(isolate) {
2313     // It only makes sense to override local allocation site behavior
2314     // if there is a difference between the global allocation site policy
2315     // for an ElementsKind and the desired usage of the stub.
2316     DCHECK(override_mode != DISABLE_ALLOCATION_SITES ||
2317            AllocationSite::GetMode(kind) == TRACK_ALLOCATION_SITE);
2318     set_sub_minor_key(ElementsKindBits::encode(kind) |
2319                       AllocationSiteOverrideModeBits::encode(override_mode));
2320   }
2321
2322   ElementsKind elements_kind() const {
2323     return ElementsKindBits::decode(sub_minor_key());
2324   }
2325
2326   AllocationSiteOverrideMode override_mode() const {
2327     return AllocationSiteOverrideModeBits::decode(sub_minor_key());
2328   }
2329
2330   static void GenerateStubsAheadOfTime(Isolate* isolate);
2331
2332   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
2333   static const int kConstructor = 0;
2334   static const int kAllocationSite = 1;
2335
2336  protected:
2337   std::ostream& BasePrintName(std::ostream& os,
2338                               const char* name) const;  // NOLINT
2339
2340  private:
2341   // Ensure data fits within available bits.
2342   STATIC_ASSERT(LAST_ALLOCATION_SITE_OVERRIDE_MODE == 1);
2343
2344   class ElementsKindBits: public BitField<ElementsKind, 0, 8> {};
2345   class AllocationSiteOverrideModeBits: public
2346       BitField<AllocationSiteOverrideMode, 8, 1> {};  // NOLINT
2347
2348   DEFINE_CODE_STUB_BASE(ArrayConstructorStubBase, HydrogenCodeStub);
2349 };
2350
2351
2352 class ArrayNoArgumentConstructorStub : public ArrayConstructorStubBase {
2353  public:
2354   ArrayNoArgumentConstructorStub(
2355       Isolate* isolate,
2356       ElementsKind kind,
2357       AllocationSiteOverrideMode override_mode = DONT_OVERRIDE)
2358       : ArrayConstructorStubBase(isolate, kind, override_mode) {
2359   }
2360
2361  private:
2362   void PrintName(std::ostream& os) const OVERRIDE {  // NOLINT
2363     BasePrintName(os, "ArrayNoArgumentConstructorStub");
2364   }
2365
2366   DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayConstructorConstantArgCount);
2367   DEFINE_HYDROGEN_CODE_STUB(ArrayNoArgumentConstructor,
2368                             ArrayConstructorStubBase);
2369 };
2370
2371
2372 class ArraySingleArgumentConstructorStub : public ArrayConstructorStubBase {
2373  public:
2374   ArraySingleArgumentConstructorStub(
2375       Isolate* isolate,
2376       ElementsKind kind,
2377       AllocationSiteOverrideMode override_mode = DONT_OVERRIDE)
2378       : ArrayConstructorStubBase(isolate, kind, override_mode) {
2379   }
2380
2381  private:
2382   void PrintName(std::ostream& os) const OVERRIDE {  // NOLINT
2383     BasePrintName(os, "ArraySingleArgumentConstructorStub");
2384   }
2385
2386   DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayConstructor);
2387   DEFINE_HYDROGEN_CODE_STUB(ArraySingleArgumentConstructor,
2388                             ArrayConstructorStubBase);
2389 };
2390
2391
2392 class ArrayNArgumentsConstructorStub : public ArrayConstructorStubBase {
2393  public:
2394   ArrayNArgumentsConstructorStub(
2395       Isolate* isolate,
2396       ElementsKind kind,
2397       AllocationSiteOverrideMode override_mode = DONT_OVERRIDE)
2398       : ArrayConstructorStubBase(isolate, kind, override_mode) {
2399   }
2400
2401  private:
2402   void PrintName(std::ostream& os) const OVERRIDE {  // NOLINT
2403     BasePrintName(os, "ArrayNArgumentsConstructorStub");
2404   }
2405
2406   DEFINE_CALL_INTERFACE_DESCRIPTOR(ArrayConstructor);
2407   DEFINE_HYDROGEN_CODE_STUB(ArrayNArgumentsConstructor,
2408                             ArrayConstructorStubBase);
2409 };
2410
2411
2412 class InternalArrayConstructorStubBase : public HydrogenCodeStub {
2413  public:
2414   InternalArrayConstructorStubBase(Isolate* isolate, ElementsKind kind)
2415       : HydrogenCodeStub(isolate) {
2416     set_sub_minor_key(ElementsKindBits::encode(kind));
2417   }
2418
2419   static void GenerateStubsAheadOfTime(Isolate* isolate);
2420
2421   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
2422   static const int kConstructor = 0;
2423
2424   ElementsKind elements_kind() const {
2425     return ElementsKindBits::decode(sub_minor_key());
2426   }
2427
2428  private:
2429   class ElementsKindBits : public BitField<ElementsKind, 0, 8> {};
2430
2431   DEFINE_CODE_STUB_BASE(InternalArrayConstructorStubBase, HydrogenCodeStub);
2432 };
2433
2434
2435 class InternalArrayNoArgumentConstructorStub : public
2436     InternalArrayConstructorStubBase {
2437  public:
2438   InternalArrayNoArgumentConstructorStub(Isolate* isolate,
2439                                          ElementsKind kind)
2440       : InternalArrayConstructorStubBase(isolate, kind) { }
2441
2442   DEFINE_CALL_INTERFACE_DESCRIPTOR(InternalArrayConstructorConstantArgCount);
2443   DEFINE_HYDROGEN_CODE_STUB(InternalArrayNoArgumentConstructor,
2444                             InternalArrayConstructorStubBase);
2445 };
2446
2447
2448 class InternalArraySingleArgumentConstructorStub : public
2449     InternalArrayConstructorStubBase {
2450  public:
2451   InternalArraySingleArgumentConstructorStub(Isolate* isolate,
2452                                              ElementsKind kind)
2453       : InternalArrayConstructorStubBase(isolate, kind) { }
2454
2455   DEFINE_CALL_INTERFACE_DESCRIPTOR(InternalArrayConstructor);
2456   DEFINE_HYDROGEN_CODE_STUB(InternalArraySingleArgumentConstructor,
2457                             InternalArrayConstructorStubBase);
2458 };
2459
2460
2461 class InternalArrayNArgumentsConstructorStub : public
2462     InternalArrayConstructorStubBase {
2463  public:
2464   InternalArrayNArgumentsConstructorStub(Isolate* isolate, ElementsKind kind)
2465       : InternalArrayConstructorStubBase(isolate, kind) { }
2466
2467   DEFINE_CALL_INTERFACE_DESCRIPTOR(InternalArrayConstructor);
2468   DEFINE_HYDROGEN_CODE_STUB(InternalArrayNArgumentsConstructor,
2469                             InternalArrayConstructorStubBase);
2470 };
2471
2472
2473 class StoreElementStub : public PlatformCodeStub {
2474  public:
2475   StoreElementStub(Isolate* isolate, ElementsKind elements_kind)
2476       : PlatformCodeStub(isolate) {
2477     minor_key_ = ElementsKindBits::encode(elements_kind);
2478   }
2479
2480  private:
2481   ElementsKind elements_kind() const {
2482     return ElementsKindBits::decode(minor_key_);
2483   }
2484
2485   class ElementsKindBits : public BitField<ElementsKind, 0, 8> {};
2486
2487   DEFINE_CALL_INTERFACE_DESCRIPTOR(Store);
2488   DEFINE_PLATFORM_CODE_STUB(StoreElement, PlatformCodeStub);
2489 };
2490
2491
2492 class ToBooleanStub: public HydrogenCodeStub {
2493  public:
2494   enum Type {
2495     UNDEFINED,
2496     BOOLEAN,
2497     NULL_TYPE,
2498     SMI,
2499     SPEC_OBJECT,
2500     STRING,
2501     SYMBOL,
2502     HEAP_NUMBER,
2503     NUMBER_OF_TYPES
2504   };
2505
2506   enum ResultMode {
2507     RESULT_AS_SMI,             // For Smi(1) on truthy value, Smi(0) otherwise.
2508     RESULT_AS_ODDBALL,         // For {true} on truthy value, {false} otherwise.
2509     RESULT_AS_INVERSE_ODDBALL  // For {false} on truthy value, {true} otherwise.
2510   };
2511
2512   // At most 8 different types can be distinguished, because the Code object
2513   // only has room for a single byte to hold a set of these types. :-P
2514   STATIC_ASSERT(NUMBER_OF_TYPES <= 8);
2515
2516   class Types : public EnumSet<Type, byte> {
2517    public:
2518     Types() : EnumSet<Type, byte>(0) {}
2519     explicit Types(byte bits) : EnumSet<Type, byte>(bits) {}
2520
2521     byte ToByte() const { return ToIntegral(); }
2522     bool UpdateStatus(Handle<Object> object);
2523     bool NeedsMap() const;
2524     bool CanBeUndetectable() const;
2525     bool IsGeneric() const { return ToIntegral() == Generic().ToIntegral(); }
2526
2527     static Types Generic() { return Types((1 << NUMBER_OF_TYPES) - 1); }
2528   };
2529
2530   ToBooleanStub(Isolate* isolate, ResultMode mode, Types types = Types())
2531       : HydrogenCodeStub(isolate) {
2532     set_sub_minor_key(TypesBits::encode(types.ToByte()) |
2533                       ResultModeBits::encode(mode));
2534   }
2535
2536   ToBooleanStub(Isolate* isolate, ExtraICState state)
2537       : HydrogenCodeStub(isolate) {
2538     set_sub_minor_key(TypesBits::encode(static_cast<byte>(state)) |
2539                       ResultModeBits::encode(RESULT_AS_SMI));
2540   }
2541
2542   bool UpdateStatus(Handle<Object> object);
2543   Types types() const { return Types(TypesBits::decode(sub_minor_key())); }
2544   ResultMode mode() const { return ResultModeBits::decode(sub_minor_key()); }
2545
2546   Code::Kind GetCodeKind() const OVERRIDE { return Code::TO_BOOLEAN_IC; }
2547   void PrintState(std::ostream& os) const OVERRIDE;  // NOLINT
2548
2549   bool SometimesSetsUpAFrame() OVERRIDE { return false; }
2550
2551   static Handle<Code> GetUninitialized(Isolate* isolate) {
2552     return ToBooleanStub(isolate, UNINITIALIZED).GetCode();
2553   }
2554
2555   ExtraICState GetExtraICState() const OVERRIDE { return types().ToIntegral(); }
2556
2557   InlineCacheState GetICState() const OVERRIDE {
2558     if (types().IsEmpty()) {
2559       return ::v8::internal::UNINITIALIZED;
2560     } else {
2561       return MONOMORPHIC;
2562     }
2563   }
2564
2565  private:
2566   ToBooleanStub(Isolate* isolate, InitializationState init_state)
2567       : HydrogenCodeStub(isolate, init_state) {
2568     set_sub_minor_key(ResultModeBits::encode(RESULT_AS_SMI));
2569   }
2570
2571   class TypesBits : public BitField<byte, 0, NUMBER_OF_TYPES> {};
2572   class ResultModeBits : public BitField<ResultMode, NUMBER_OF_TYPES, 2> {};
2573
2574   DEFINE_CALL_INTERFACE_DESCRIPTOR(ToBoolean);
2575   DEFINE_HYDROGEN_CODE_STUB(ToBoolean, HydrogenCodeStub);
2576 };
2577
2578
2579 std::ostream& operator<<(std::ostream& os, const ToBooleanStub::Types& t);
2580
2581
2582 class ElementsTransitionAndStoreStub : public HydrogenCodeStub {
2583  public:
2584   ElementsTransitionAndStoreStub(Isolate* isolate, ElementsKind from_kind,
2585                                  ElementsKind to_kind, bool is_jsarray,
2586                                  KeyedAccessStoreMode store_mode)
2587       : HydrogenCodeStub(isolate) {
2588     set_sub_minor_key(FromBits::encode(from_kind) | ToBits::encode(to_kind) |
2589                       IsJSArrayBits::encode(is_jsarray) |
2590                       StoreModeBits::encode(store_mode));
2591   }
2592
2593   ElementsKind from_kind() const { return FromBits::decode(sub_minor_key()); }
2594   ElementsKind to_kind() const { return ToBits::decode(sub_minor_key()); }
2595   bool is_jsarray() const { return IsJSArrayBits::decode(sub_minor_key()); }
2596   KeyedAccessStoreMode store_mode() const {
2597     return StoreModeBits::decode(sub_minor_key());
2598   }
2599
2600   // Parameters accessed via CodeStubGraphBuilder::GetParameter()
2601   enum ParameterIndices {
2602     kValueIndex,
2603     kMapIndex,
2604     kKeyIndex,
2605     kObjectIndex,
2606     kParameterCount
2607   };
2608
2609   static const Register ValueRegister() {
2610     return ElementTransitionAndStoreDescriptor::ValueRegister();
2611   }
2612   static const Register MapRegister() {
2613     return ElementTransitionAndStoreDescriptor::MapRegister();
2614   }
2615   static const Register KeyRegister() {
2616     return ElementTransitionAndStoreDescriptor::NameRegister();
2617   }
2618   static const Register ObjectRegister() {
2619     return ElementTransitionAndStoreDescriptor::ReceiverRegister();
2620   }
2621
2622  private:
2623   class FromBits : public BitField<ElementsKind, 0, 8> {};
2624   class ToBits : public BitField<ElementsKind, 8, 8> {};
2625   class IsJSArrayBits : public BitField<bool, 16, 1> {};
2626   class StoreModeBits : public BitField<KeyedAccessStoreMode, 17, 4> {};
2627
2628   DEFINE_CALL_INTERFACE_DESCRIPTOR(ElementTransitionAndStore);
2629   DEFINE_HYDROGEN_CODE_STUB(ElementsTransitionAndStore, HydrogenCodeStub);
2630 };
2631
2632
2633 class StoreArrayLiteralElementStub : public PlatformCodeStub {
2634  public:
2635   explicit StoreArrayLiteralElementStub(Isolate* isolate)
2636       : PlatformCodeStub(isolate) { }
2637
2638   DEFINE_CALL_INTERFACE_DESCRIPTOR(StoreArrayLiteralElement);
2639   DEFINE_PLATFORM_CODE_STUB(StoreArrayLiteralElement, PlatformCodeStub);
2640 };
2641
2642
2643 class StubFailureTrampolineStub : public PlatformCodeStub {
2644  public:
2645   StubFailureTrampolineStub(Isolate* isolate, StubFunctionMode function_mode)
2646       : PlatformCodeStub(isolate) {
2647     minor_key_ = FunctionModeField::encode(function_mode);
2648   }
2649
2650   static void GenerateAheadOfTime(Isolate* isolate);
2651
2652  private:
2653   StubFunctionMode function_mode() const {
2654     return FunctionModeField::decode(minor_key_);
2655   }
2656
2657   class FunctionModeField : public BitField<StubFunctionMode, 0, 1> {};
2658
2659   DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
2660   DEFINE_PLATFORM_CODE_STUB(StubFailureTrampoline, PlatformCodeStub);
2661 };
2662
2663
2664 class ProfileEntryHookStub : public PlatformCodeStub {
2665  public:
2666   explicit ProfileEntryHookStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
2667
2668   // The profile entry hook function is not allowed to cause a GC.
2669   bool SometimesSetsUpAFrame() OVERRIDE { return false; }
2670
2671   // Generates a call to the entry hook if it's enabled.
2672   static void MaybeCallEntryHook(MacroAssembler* masm);
2673
2674  private:
2675   static void EntryHookTrampoline(intptr_t function,
2676                                   intptr_t stack_pointer,
2677                                   Isolate* isolate);
2678
2679   // ProfileEntryHookStub is called at the start of a function, so it has the
2680   // same register set.
2681   DEFINE_CALL_INTERFACE_DESCRIPTOR(CallFunction)
2682   DEFINE_PLATFORM_CODE_STUB(ProfileEntryHook, PlatformCodeStub);
2683 };
2684
2685
2686 class StoreBufferOverflowStub : public PlatformCodeStub {
2687  public:
2688   StoreBufferOverflowStub(Isolate* isolate, SaveFPRegsMode save_fp)
2689       : PlatformCodeStub(isolate) {
2690     minor_key_ = SaveDoublesBits::encode(save_fp == kSaveFPRegs);
2691   }
2692
2693   static void GenerateFixedRegStubsAheadOfTime(Isolate* isolate);
2694   bool SometimesSetsUpAFrame() OVERRIDE { return false; }
2695
2696  private:
2697   bool save_doubles() const { return SaveDoublesBits::decode(minor_key_); }
2698
2699   class SaveDoublesBits : public BitField<bool, 0, 1> {};
2700
2701   DEFINE_NULL_CALL_INTERFACE_DESCRIPTOR();
2702   DEFINE_PLATFORM_CODE_STUB(StoreBufferOverflow, PlatformCodeStub);
2703 };
2704
2705
2706 class SubStringStub : public PlatformCodeStub {
2707  public:
2708   explicit SubStringStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
2709
2710   DEFINE_CALL_INTERFACE_DESCRIPTOR(ContextOnly);
2711   DEFINE_PLATFORM_CODE_STUB(SubString, PlatformCodeStub);
2712 };
2713
2714
2715 class ToNumberStub FINAL : public PlatformCodeStub {
2716  public:
2717   explicit ToNumberStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
2718
2719   DEFINE_CALL_INTERFACE_DESCRIPTOR(ToNumber);
2720   DEFINE_PLATFORM_CODE_STUB(ToNumber, PlatformCodeStub);
2721 };
2722
2723
2724 class StringCompareStub : public PlatformCodeStub {
2725  public:
2726   explicit StringCompareStub(Isolate* isolate) : PlatformCodeStub(isolate) {}
2727
2728   DEFINE_CALL_INTERFACE_DESCRIPTOR(ContextOnly);
2729   DEFINE_PLATFORM_CODE_STUB(StringCompare, PlatformCodeStub);
2730 };
2731
2732
2733 #undef DEFINE_CALL_INTERFACE_DESCRIPTOR
2734 #undef DEFINE_PLATFORM_CODE_STUB
2735 #undef DEFINE_HANDLER_CODE_STUB
2736 #undef DEFINE_HYDROGEN_CODE_STUB
2737 #undef DEFINE_CODE_STUB
2738 #undef DEFINE_CODE_STUB_BASE
2739 } }  // namespace v8::internal
2740
2741 #endif  // V8_CODE_STUBS_H_