b5f58ed211a1800ede1b7c6022409cfa04669797
[platform/upstream/nodejs.git] / deps / v8 / src / ic / ic-state.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_IC_STATE_H_
6 #define V8_IC_STATE_H_
7
8 #include "src/macro-assembler.h"
9
10 namespace v8 {
11 namespace internal {
12
13
14 const int kMaxKeyedPolymorphism = 4;
15
16
17 class ICUtility : public AllStatic {
18  public:
19   // Clear the inline cache to initial state.
20   static void Clear(Isolate* isolate, Address address,
21                     ConstantPoolArray* constant_pool);
22 };
23
24
25 class CallICState FINAL BASE_EMBEDDED {
26  public:
27   explicit CallICState(ExtraICState extra_ic_state);
28
29   enum CallType { METHOD, FUNCTION };
30
31   CallICState(int argc, CallType call_type)
32       : argc_(argc), call_type_(call_type) {}
33
34   ExtraICState GetExtraICState() const;
35
36   static void GenerateAheadOfTime(Isolate*,
37                                   void (*Generate)(Isolate*,
38                                                    const CallICState&));
39
40   int arg_count() const { return argc_; }
41   CallType call_type() const { return call_type_; }
42
43   bool CallAsMethod() const { return call_type_ == METHOD; }
44
45  private:
46   class ArgcBits : public BitField<int, 0, Code::kArgumentsBits> {};
47   class CallTypeBits : public BitField<CallType, Code::kArgumentsBits, 1> {};
48
49   const int argc_;
50   const CallType call_type_;
51 };
52
53
54 std::ostream& operator<<(std::ostream& os, const CallICState& s);
55
56
57 class BinaryOpICState FINAL BASE_EMBEDDED {
58  public:
59   BinaryOpICState(Isolate* isolate, ExtraICState extra_ic_state);
60
61   BinaryOpICState(Isolate* isolate, Token::Value op)
62       : op_(op),
63         left_kind_(NONE),
64         right_kind_(NONE),
65         result_kind_(NONE),
66         isolate_(isolate) {
67     DCHECK_LE(FIRST_TOKEN, op);
68     DCHECK_LE(op, LAST_TOKEN);
69   }
70
71   InlineCacheState GetICState() const {
72     if (Max(left_kind_, right_kind_) == NONE) {
73       return ::v8::internal::UNINITIALIZED;
74     }
75     if (Max(left_kind_, right_kind_) == GENERIC) {
76       return ::v8::internal::MEGAMORPHIC;
77     }
78     if (Min(left_kind_, right_kind_) == GENERIC) {
79       return ::v8::internal::GENERIC;
80     }
81     return ::v8::internal::MONOMORPHIC;
82   }
83
84   ExtraICState GetExtraICState() const;
85
86   static void GenerateAheadOfTime(Isolate*,
87                                   void (*Generate)(Isolate*,
88                                                    const BinaryOpICState&));
89
90   // Returns true if the IC _could_ create allocation mementos.
91   bool CouldCreateAllocationMementos() const {
92     if (left_kind_ == STRING || right_kind_ == STRING) {
93       DCHECK_EQ(Token::ADD, op_);
94       return true;
95     }
96     return false;
97   }
98
99   // Returns true if the IC _should_ create allocation mementos.
100   bool ShouldCreateAllocationMementos() const {
101     return FLAG_allocation_site_pretenuring && CouldCreateAllocationMementos();
102   }
103
104   bool HasSideEffects() const {
105     return Max(left_kind_, right_kind_) == GENERIC;
106   }
107
108   // Returns true if the IC should enable the inline smi code (i.e. if either
109   // parameter may be a smi).
110   bool UseInlinedSmiCode() const {
111     return KindMaybeSmi(left_kind_) || KindMaybeSmi(right_kind_);
112   }
113
114   static const int FIRST_TOKEN = Token::BIT_OR;
115   static const int LAST_TOKEN = Token::MOD;
116
117   Token::Value op() const { return op_; }
118   Maybe<int> fixed_right_arg() const { return fixed_right_arg_; }
119
120   Type* GetLeftType(Zone* zone) const { return KindToType(left_kind_, zone); }
121   Type* GetRightType(Zone* zone) const { return KindToType(right_kind_, zone); }
122   Type* GetResultType(Zone* zone) const;
123
124   void Update(Handle<Object> left, Handle<Object> right, Handle<Object> result);
125
126   Isolate* isolate() const { return isolate_; }
127
128  private:
129   friend std::ostream& operator<<(std::ostream& os, const BinaryOpICState& s);
130
131   enum Kind { NONE, SMI, INT32, NUMBER, STRING, GENERIC };
132
133   Kind UpdateKind(Handle<Object> object, Kind kind) const;
134
135   static const char* KindToString(Kind kind);
136   static Type* KindToType(Kind kind, Zone* zone);
137   static bool KindMaybeSmi(Kind kind) {
138     return (kind >= SMI && kind <= NUMBER) || kind == GENERIC;
139   }
140
141   // We truncate the last bit of the token.
142   STATIC_ASSERT(LAST_TOKEN - FIRST_TOKEN < (1 << 4));
143   class OpField : public BitField<int, 0, 4> {};
144   class ResultKindField : public BitField<Kind, 4, 3> {};
145   class LeftKindField : public BitField<Kind, 7, 3> {};
146   // When fixed right arg is set, we don't need to store the right kind.
147   // Thus the two fields can overlap.
148   class HasFixedRightArgField : public BitField<bool, 10, 1> {};
149   class FixedRightArgValueField : public BitField<int, 11, 4> {};
150   class RightKindField : public BitField<Kind, 11, 3> {};
151
152   Token::Value op_;
153   Kind left_kind_;
154   Kind right_kind_;
155   Kind result_kind_;
156   Maybe<int> fixed_right_arg_;
157   Isolate* isolate_;
158 };
159
160
161 std::ostream& operator<<(std::ostream& os, const BinaryOpICState& s);
162
163
164 class CompareICState {
165  public:
166   // The type/state lattice is defined by the following inequations:
167   //   UNINITIALIZED < ...
168   //   ... < GENERIC
169   //   SMI < NUMBER
170   //   INTERNALIZED_STRING < STRING
171   //   KNOWN_OBJECT < OBJECT
172   enum State {
173     UNINITIALIZED,
174     SMI,
175     NUMBER,
176     STRING,
177     INTERNALIZED_STRING,
178     UNIQUE_NAME,   // Symbol or InternalizedString
179     OBJECT,        // JSObject
180     KNOWN_OBJECT,  // JSObject with specific map (faster check)
181     GENERIC
182   };
183
184   static Type* StateToType(Zone* zone, State state,
185                            Handle<Map> map = Handle<Map>());
186
187   static State NewInputState(State old_state, Handle<Object> value);
188
189   static const char* GetStateName(CompareICState::State state);
190
191   static State TargetState(State old_state, State old_left, State old_right,
192                            Token::Value op, bool has_inlined_smi_code,
193                            Handle<Object> x, Handle<Object> y);
194 };
195
196
197 class LoadICState FINAL BASE_EMBEDDED {
198  public:
199   explicit LoadICState(ExtraICState extra_ic_state) : state_(extra_ic_state) {}
200
201   explicit LoadICState(ContextualMode mode)
202       : state_(ContextualModeBits::encode(mode)) {}
203
204   ExtraICState GetExtraICState() const { return state_; }
205
206   ContextualMode contextual_mode() const {
207     return ContextualModeBits::decode(state_);
208   }
209
210   static ContextualMode GetContextualMode(ExtraICState state) {
211     return LoadICState(state).contextual_mode();
212   }
213
214  private:
215   class ContextualModeBits : public BitField<ContextualMode, 0, 1> {};
216   STATIC_ASSERT(static_cast<int>(NOT_CONTEXTUAL) == 0);
217
218   const ExtraICState state_;
219 };
220 }
221 }
222
223 #endif  // V8_IC_STATE_H_