blob: a3265d70b9aedd7f548006cc8e0de3bbc3646c9c [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_IC_H_
6#define V8_IC_H_
7
8#include "src/ic/ic-state.h"
9#include "src/macro-assembler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/messages.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011
12namespace v8 {
13namespace internal {
14
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015//
16// IC is the base class for LoadIC, StoreIC, KeyedLoadIC, and KeyedStoreIC.
17//
18class IC {
19 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020 // Alias the inline cache state type to make the IC code more readable.
21 typedef InlineCacheState State;
22
23 // The IC code is either invoked with no extra frames on the stack
24 // or with a single extra frame for supporting calls.
25 enum FrameDepth { NO_EXTRA_FRAME = 0, EXTRA_CALL_FRAME = 1 };
26
27 // Construct the IC structure with the given number of extra
28 // JavaScript frames on the stack.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029 IC(FrameDepth depth, Isolate* isolate, FeedbackNexus* nexus = NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 virtual ~IC() {}
31
32 State state() const { return state_; }
33 inline Address address() const;
34
35 // Compute the current IC state based on the target stub, receiver and name.
36 void UpdateState(Handle<Object> receiver, Handle<Object> name);
37
38 bool IsNameCompatibleWithPrototypeFailure(Handle<Object> name);
39 void MarkPrototypeFailure(Handle<Object> name) {
40 DCHECK(IsNameCompatibleWithPrototypeFailure(name));
Emily Bernierd0a1eb72015-03-24 16:35:39 -040041 old_state_ = state_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 state_ = PROTOTYPE_FAILURE;
43 }
44
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 // Clear the inline cache to initial state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 static void Clear(Isolate* isolate, Address address, Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047
48#ifdef DEBUG
49 bool IsLoadStub() const {
50 return target()->is_load_stub() || target()->is_keyed_load_stub();
51 }
52
53 bool IsStoreStub() const {
54 return target()->is_store_stub() || target()->is_keyed_store_stub();
55 }
56
57 bool IsCallStub() const { return target()->is_call_stub(); }
58#endif
59
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 static inline Handle<Map> GetHandlerCacheHolder(Handle<Map> receiver_map,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 bool receiver_is_holder,
62 Isolate* isolate,
63 CacheHolderFlag* flag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 static inline Handle<Map> GetICCacheHolder(Handle<Map> receiver_map,
65 Isolate* isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 CacheHolderFlag* flag);
67
68 static bool IsCleared(Code* code) {
69 InlineCacheState state = code->ic_state();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070 return !FLAG_use_ic || state == UNINITIALIZED || state == PREMONOMORPHIC;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 }
72
Emily Bernierd0a1eb72015-03-24 16:35:39 -040073 static bool IsCleared(FeedbackNexus* nexus) {
74 InlineCacheState state = nexus->StateFromFeedback();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 return !FLAG_use_ic || state == UNINITIALIZED || state == PREMONOMORPHIC;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040076 }
77
Emily Bernierd0a1eb72015-03-24 16:35:39 -040078 static bool ICUseVector(Code::Kind kind) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 return kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC ||
80 kind == Code::CALL_IC || kind == Code::STORE_IC ||
81 kind == Code::KEYED_STORE_IC;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040082 }
83
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 protected:
85 // Get the call-site target; used for determining the state.
86 Handle<Code> target() const { return target_; }
87
88 Address fp() const { return fp_; }
89 Address pc() const { return *pc_address_; }
90 Isolate* isolate() const { return isolate_; }
91
92 // Get the shared function info of the caller.
93 SharedFunctionInfo* GetSharedFunctionInfo() const;
94 // Get the code object of the caller.
95 Code* GetCode() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096
97 bool AddressIsOptimizedCode() const;
98 inline bool AddressIsDeoptimizedCode() const;
99 inline static bool AddressIsDeoptimizedCode(Isolate* isolate,
100 Address address);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101
102 // Set the call-site target.
103 inline void set_target(Code* code);
104 bool is_target_set() { return target_set_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000105 bool is_vector_set() { return vector_set_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107 bool UseVector() const {
108 bool use = ICUseVector(kind());
109 // If we are supposed to use the nexus, verify the nexus is non-null.
110 DCHECK(!use || nexus_ != NULL);
111 return use;
112 }
113
114 // Configure for most states.
115 void ConfigureVectorState(IC::State new_state);
116 // Configure the vector for MONOMORPHIC.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 void ConfigureVectorState(Handle<Name> name, Handle<Map> map,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400118 Handle<Code> handler);
119 // Configure the vector for POLYMORPHIC.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 void ConfigureVectorState(Handle<Name> name, MapHandleList* maps,
121 CodeHandleList* handlers);
122 // Configure the vector for POLYMORPHIC with transitions (only for element
123 // keyed stores).
124 void ConfigureVectorState(MapHandleList* maps,
125 MapHandleList* transitioned_maps,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400126 CodeHandleList* handlers);
127
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 char TransitionMarkFromState(IC::State state);
129 void TraceIC(const char* type, Handle<Object> name);
130 void TraceIC(const char* type, Handle<Object> name, State old_state,
131 State new_state);
132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133 MaybeHandle<Object> TypeError(MessageTemplate::Template,
134 Handle<Object> object, Handle<Object> key);
135 MaybeHandle<Object> ReferenceError(Handle<Name> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136
137 // Access the target code for the given IC address.
138 static inline Code* GetTargetAtAddress(Address address,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 static inline void SetTargetAtAddress(Address address, Code* target,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 static void OnTypeFeedbackChanged(Isolate* isolate, Address address,
143 State old_state, State new_state,
144 bool target_remains_ic_stub);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400145 // As a vector-based IC, type feedback must be updated differently.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146 static void OnTypeFeedbackChanged(Isolate* isolate, Code* host);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 static void PostPatching(Address address, Code* target, Code* old_target);
148
149 // Compute the handler either by compiling or by retrieving a cached version.
150 Handle<Code> ComputeHandler(LookupIterator* lookup,
151 Handle<Object> value = Handle<Code>::null());
152 virtual Handle<Code> CompileHandler(LookupIterator* lookup,
153 Handle<Object> value,
154 CacheHolderFlag cache_holder) {
155 UNREACHABLE();
156 return Handle<Code>::null();
157 }
158
159 void UpdateMonomorphicIC(Handle<Code> handler, Handle<Name> name);
160 bool UpdatePolymorphicIC(Handle<Name> name, Handle<Code> code);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 void UpdateMegamorphicCache(Map* map, Name* name, Code* code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162
163 void CopyICToMegamorphicCache(Handle<Name> name);
164 bool IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map);
165 void PatchCache(Handle<Name> name, Handle<Code> code);
166 Code::Kind kind() const { return kind_; }
167 Code::Kind handler_kind() const {
168 if (kind_ == Code::KEYED_LOAD_IC) return Code::LOAD_IC;
169 DCHECK(kind_ == Code::LOAD_IC || kind_ == Code::STORE_IC ||
170 kind_ == Code::KEYED_STORE_IC);
171 return kind_;
172 }
173 virtual Handle<Code> megamorphic_stub() {
174 UNREACHABLE();
175 return Handle<Code>::null();
176 }
177
178 bool TryRemoveInvalidPrototypeDependentStub(Handle<Object> receiver,
179 Handle<String> name);
180
181 ExtraICState extra_ic_state() const { return extra_ic_state_; }
182 void set_extra_ic_state(ExtraICState state) { extra_ic_state_ = state; }
183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 Handle<Map> receiver_map() { return receiver_map_; }
185 void update_receiver_map(Handle<Object> receiver) {
186 if (receiver->IsSmi()) {
187 receiver_map_ = isolate_->factory()->heap_number_map();
188 } else {
189 receiver_map_ = handle(HeapObject::cast(*receiver)->map());
190 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 }
192
193 void TargetMaps(MapHandleList* list) {
194 FindTargetMaps();
195 for (int i = 0; i < target_maps_.length(); i++) {
196 list->Add(target_maps_.at(i));
197 }
198 }
199
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 Map* FirstTargetMap() {
201 FindTargetMaps();
202 return target_maps_.length() > 0 ? *target_maps_.at(0) : NULL;
203 }
204
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 inline void UpdateTarget();
206
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400207 Handle<TypeFeedbackVector> vector() const { return nexus()->vector_handle(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 FeedbackVectorSlot slot() const { return nexus()->slot(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400209 State saved_state() const {
210 return state() == PROTOTYPE_FAILURE ? old_state_ : state();
211 }
212
213 template <class NexusClass>
214 NexusClass* casted_nexus() {
215 return static_cast<NexusClass*>(nexus_);
216 }
217 FeedbackNexus* nexus() const { return nexus_; }
218
219 inline Code* get_host();
220
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 private:
222 inline Code* raw_target() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223 inline Address constant_pool() const;
224 inline Address raw_constant_pool() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225
226 void FindTargetMaps() {
227 if (target_maps_set_) return;
228 target_maps_set_ = true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 if (UseVector()) {
230 nexus()->ExtractMaps(&target_maps_);
231 } else {
232 if (state_ == MONOMORPHIC) {
233 Map* map = target_->FindFirstMap();
234 if (map != NULL) target_maps_.Add(handle(map));
235 } else if (state_ != UNINITIALIZED && state_ != PREMONOMORPHIC) {
236 target_->FindAllMaps(&target_maps_);
237 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 }
239 }
240
241 // Frame pointer for the frame that uses (calls) the IC.
242 Address fp_;
243
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 // All access to the program counter and constant pool of an IC structure is
245 // indirect to make the code GC safe. This feature is crucial since
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 // GetProperty and SetProperty are called and they in turn might
247 // invoke the garbage collector.
248 Address* pc_address_;
249
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 // The constant pool of the code which originally called the IC (which might
251 // be for the breakpointed copy of the original code).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 Address* constant_pool_address_;
253
254 Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255
256 // The original code target that missed.
257 Handle<Code> target_;
258 bool target_set_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 bool vector_set_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400260 State old_state_; // For saving if we marked as prototype failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 State state_;
262 Code::Kind kind_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 Handle<Map> receiver_map_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 MaybeHandle<Code> maybe_handler_;
265
266 ExtraICState extra_ic_state_;
267 MapHandleList target_maps_;
268 bool target_maps_set_;
269
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400270 FeedbackNexus* nexus_;
271
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
273};
274
275
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276class CallIC : public IC {
277 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400278 CallIC(Isolate* isolate, CallICNexus* nexus)
279 : IC(EXTRA_CALL_FRAME, isolate, nexus) {
280 DCHECK(nexus != NULL);
281 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000283 void HandleMiss(Handle<Object> function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284
285 // Code generator routines.
286 static Handle<Code> initialize_stub(Isolate* isolate, int argc,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287 ConvertReceiverMode mode);
288 static Handle<Code> initialize_stub_in_optimized_code(
289 Isolate* isolate, int argc, ConvertReceiverMode mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400291 static void Clear(Isolate* isolate, Code* host, CallICNexus* nexus);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292};
293
294
295class LoadIC : public IC {
296 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 static ExtraICState ComputeExtraICState(TypeofMode typeof_mode,
298 LanguageMode language_mode) {
299 return LoadICState(typeof_mode, language_mode).GetExtraICState();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 }
301
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 TypeofMode typeof_mode() const {
303 return LoadICState::GetTypeofMode(extra_ic_state());
304 }
305
306 LanguageMode language_mode() const {
307 return LoadICState::GetLanguageMode(extra_ic_state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 }
309
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400310 LoadIC(FrameDepth depth, Isolate* isolate, FeedbackNexus* nexus = NULL)
311 : IC(depth, isolate, nexus) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000312 DCHECK(nexus != NULL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400313 DCHECK(IsLoadStub());
314 }
315
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000316 bool ShouldThrowReferenceError(Handle<Object> receiver) {
317 return receiver->IsJSGlobalObject() && typeof_mode() == NOT_INSIDE_TYPEOF;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 }
319
320 // Code generator routines.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323 static void GenerateMiss(MacroAssembler* masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 static void GenerateRuntimeGetProperty(MacroAssembler* masm,
325 LanguageMode language_mode);
326 static void GenerateNormal(MacroAssembler* masm, LanguageMode language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327
328 static Handle<Code> initialize_stub(Isolate* isolate,
329 ExtraICState extra_state);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400330 static Handle<Code> initialize_stub_in_optimized_code(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000331 Isolate* isolate, ExtraICState extra_state, State initialization_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332
333 MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
334 Handle<Name> name);
335
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400336 static void Clear(Isolate* isolate, Code* host, LoadICNexus* nexus);
337
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338 protected:
339 inline void set_target(Code* code);
340
341 Handle<Code> slow_stub() const {
342 if (kind() == Code::LOAD_IC) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 return is_strong(language_mode())
344 ? isolate()->builtins()->LoadIC_Slow_Strong()
345 : isolate()->builtins()->LoadIC_Slow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 } else {
347 DCHECK_EQ(Code::KEYED_LOAD_IC, kind());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 return is_strong(language_mode())
349 ? isolate()->builtins()->KeyedLoadIC_Slow_Strong()
350 : isolate()->builtins()->KeyedLoadIC_Slow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351 }
352 }
353
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000354 Handle<Code> megamorphic_stub() override;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355
356 // Update the inline cache and the global stub cache based on the
357 // lookup result.
358 void UpdateCaches(LookupIterator* lookup);
359
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360 Handle<Code> CompileHandler(LookupIterator* lookup, Handle<Object> unused,
361 CacheHolderFlag cache_holder) override;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362
363 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000364 Handle<Code> SimpleFieldLoad(FieldIndex index);
365
366 static void Clear(Isolate* isolate, Address address, Code* target,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000367 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368
369 friend class IC;
370};
371
372
373class KeyedLoadIC : public LoadIC {
374 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400375 // ExtraICState bits (building on IC)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000376 class IcCheckTypeField
377 : public BitField<IcCheckType, LoadICState::kNextBitFieldOffset, 1> {};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400378
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 static ExtraICState ComputeExtraICState(TypeofMode typeof_mode,
380 LanguageMode language_mode,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400381 IcCheckType key_type) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000382 return LoadICState(typeof_mode, language_mode).GetExtraICState() |
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400383 IcCheckTypeField::encode(key_type);
384 }
385
386 static IcCheckType GetKeyType(ExtraICState extra_state) {
387 return IcCheckTypeField::decode(extra_state);
388 }
389
390 KeyedLoadIC(FrameDepth depth, Isolate* isolate,
391 KeyedLoadICNexus* nexus = NULL)
392 : LoadIC(depth, isolate, nexus) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393 DCHECK(nexus != NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 DCHECK(target()->is_keyed_load_stub());
395 }
396
397 MUST_USE_RESULT MaybeHandle<Object> Load(Handle<Object> object,
398 Handle<Object> key);
399
400 // Code generator routines.
401 static void GenerateMiss(MacroAssembler* masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000402 static void GenerateRuntimeGetProperty(MacroAssembler* masm,
403 LanguageMode language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405 static void GenerateMegamorphic(MacroAssembler* masm,
406 LanguageMode language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407
408 // Bit mask to be tested against bit field for the cases when
409 // generic stub should go into slow case.
410 // Access check is necessary explicitly since generic stub does not perform
411 // map checks.
412 static const int kSlowCaseBitFieldMask =
413 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
414
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415 static Handle<Code> initialize_stub(Isolate* isolate,
416 ExtraICState extra_state);
417 static Handle<Code> initialize_stub_in_optimized_code(
418 Isolate* isolate, State initialization_state, ExtraICState extra_state);
419 static Handle<Code> ChooseMegamorphicStub(Isolate* isolate,
420 ExtraICState extra_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400422 static void Clear(Isolate* isolate, Code* host, KeyedLoadICNexus* nexus);
423
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424 protected:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400425 // receiver is HeapObject because it could be a String or a JSObject
426 Handle<Code> LoadElementStub(Handle<HeapObject> receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427
428 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 static void Clear(Isolate* isolate, Address address, Code* target,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431
432 friend class IC;
433};
434
435
436class StoreIC : public IC {
437 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000438 static ExtraICState ComputeExtraICState(LanguageMode flag) {
439 return StoreICState(flag).GetExtraICState();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440 }
441
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000442 StoreIC(FrameDepth depth, Isolate* isolate, FeedbackNexus* nexus = NULL)
443 : IC(depth, isolate, nexus) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000444 DCHECK(IsStoreStub());
445 }
446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000447 LanguageMode language_mode() const {
448 return StoreICState::GetLanguageMode(extra_ic_state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 }
450
451 // Code generators for stub routines. Only called once at startup.
452 static void GenerateSlow(MacroAssembler* masm);
453 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
454 static void GeneratePreMonomorphic(MacroAssembler* masm) {
455 GenerateMiss(masm);
456 }
457 static void GenerateMiss(MacroAssembler* masm);
458 static void GenerateMegamorphic(MacroAssembler* masm);
459 static void GenerateNormal(MacroAssembler* masm);
460 static void GenerateRuntimeSetProperty(MacroAssembler* masm,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 LanguageMode language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 static Handle<Code> initialize_stub(Isolate* isolate,
464 LanguageMode language_mode,
465 State initialization_state);
466 static Handle<Code> initialize_stub_in_optimized_code(
467 Isolate* isolate, LanguageMode language_mode, State initialization_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468
469 MUST_USE_RESULT MaybeHandle<Object> Store(
470 Handle<Object> object, Handle<Name> name, Handle<Object> value,
471 JSReceiver::StoreFromKeyed store_mode =
472 JSReceiver::CERTAINLY_NOT_STORE_FROM_KEYED);
473
474 bool LookupForWrite(LookupIterator* it, Handle<Object> value,
475 JSReceiver::StoreFromKeyed store_mode);
476
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 static void Clear(Isolate* isolate, Code* host, StoreICNexus* nexus);
478
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 // Stub accessors.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481 Handle<Code> megamorphic_stub() override;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482 Handle<Code> slow_stub() const;
483
484 virtual Handle<Code> pre_monomorphic_stub() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000485 return pre_monomorphic_stub(isolate(), language_mode());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 }
487
488 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489 LanguageMode language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490
491 // Update the inline cache and the global stub cache based on the
492 // lookup result.
493 void UpdateCaches(LookupIterator* lookup, Handle<Object> value,
494 JSReceiver::StoreFromKeyed store_mode);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 Handle<Code> CompileHandler(LookupIterator* lookup, Handle<Object> value,
496 CacheHolderFlag cache_holder) override;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497
498 private:
499 inline void set_target(Code* code);
500
501 static void Clear(Isolate* isolate, Address address, Code* target,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503
504 friend class IC;
505};
506
507
508enum KeyedStoreCheckMap { kDontCheckMap, kCheckMap };
509
510
511enum KeyedStoreIncrementLength { kDontIncrementLength, kIncrementLength };
512
513
514class KeyedStoreIC : public StoreIC {
515 public:
516 // ExtraICState bits (building on IC)
517 // ExtraICState bits
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000518 // When more language modes are added, these BitFields need to move too.
519 STATIC_ASSERT(i::LANGUAGE_END == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000520 class ExtraICStateKeyedAccessStoreMode
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000521 : public BitField<KeyedAccessStoreMode, 3, 3> {}; // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400523 class IcCheckTypeField : public BitField<IcCheckType, 6, 1> {};
524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525 static ExtraICState ComputeExtraICState(LanguageMode flag,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 KeyedAccessStoreMode mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000527 return StoreICState(flag).GetExtraICState() |
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400528 ExtraICStateKeyedAccessStoreMode::encode(mode) |
529 IcCheckTypeField::encode(ELEMENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 }
531
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000532 KeyedAccessStoreMode GetKeyedAccessStoreMode() {
533 return casted_nexus<KeyedStoreICNexus>()->GetKeyedAccessStoreMode();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 }
535
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000536 KeyedStoreIC(FrameDepth depth, Isolate* isolate,
537 KeyedStoreICNexus* nexus = NULL)
538 : StoreIC(depth, isolate, nexus) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 DCHECK(target()->is_keyed_store_stub());
540 }
541
542 MUST_USE_RESULT MaybeHandle<Object> Store(Handle<Object> object,
543 Handle<Object> name,
544 Handle<Object> value);
545
546 // Code generators for stub routines. Only called once at startup.
547 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
548 static void GeneratePreMonomorphic(MacroAssembler* masm) {
549 GenerateMiss(masm);
550 }
551 static void GenerateMiss(MacroAssembler* masm);
552 static void GenerateSlow(MacroAssembler* masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000553 static void GenerateMegamorphic(MacroAssembler* masm,
554 LanguageMode language_mode);
555
556 static Handle<Code> initialize_stub(Isolate* isolate,
557 LanguageMode language_mode,
558 State initialization_state);
559
560 static Handle<Code> initialize_stub_in_optimized_code(
561 Isolate* isolate, LanguageMode language_mode, State initialization_state);
562 static Handle<Code> ChooseMegamorphicStub(Isolate* isolate,
563 ExtraICState extra_state);
564
565 static void Clear(Isolate* isolate, Code* host, KeyedStoreICNexus* nexus);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566
567 protected:
568 virtual Handle<Code> pre_monomorphic_stub() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000569 return pre_monomorphic_stub(isolate(), language_mode());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 }
571 static Handle<Code> pre_monomorphic_stub(Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000572 LanguageMode language_mode) {
573 if (is_strict(language_mode)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574 return isolate->builtins()->KeyedStoreIC_PreMonomorphic_Strict();
575 } else {
576 return isolate->builtins()->KeyedStoreIC_PreMonomorphic();
577 }
578 }
579
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000580 Handle<Code> StoreElementStub(Handle<Map> receiver_map,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 KeyedAccessStoreMode store_mode);
582
583 private:
584 inline void set_target(Code* code);
585
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 static void Clear(Isolate* isolate, Address address, Code* target,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000587 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000588
589 Handle<Map> ComputeTransitionedMap(Handle<Map> map,
590 KeyedAccessStoreMode store_mode);
591
592 friend class IC;
593};
594
595
596// Type Recording BinaryOpIC, that records the types of the inputs and outputs.
597class BinaryOpIC : public IC {
598 public:
599 explicit BinaryOpIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
600
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 MaybeHandle<Object> Transition(Handle<AllocationSite> allocation_site,
602 Handle<Object> left,
603 Handle<Object> right) WARN_UNUSED_RESULT;
604};
605
606
607class CompareIC : public IC {
608 public:
609 CompareIC(Isolate* isolate, Token::Value op)
610 : IC(EXTRA_CALL_FRAME, isolate), op_(op) {}
611
612 // Update the inline cache for the given operands.
613 Code* UpdateCaches(Handle<Object> x, Handle<Object> y);
614
615 // Helper function for computing the condition for a compare operation.
616 static Condition ComputeCondition(Token::Value op);
617
618 // Factory method for getting an uninitialized compare stub.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000619 static Handle<Code> GetUninitialized(Isolate* isolate, Token::Value op,
620 Strength strength);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000621
622 private:
623 static bool HasInlinedSmiCode(Address address);
624
625 bool strict() const { return op_ == Token::EQ_STRICT; }
626 Condition GetCondition() const { return ComputeCondition(op_); }
627
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000628 static Code* GetRawUninitialized(Isolate* isolate, Token::Value op,
629 Strength strength);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630
631 static void Clear(Isolate* isolate, Address address, Code* target,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000632 Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000633
634 Token::Value op_;
635
636 friend class IC;
637};
638
639
640class CompareNilIC : public IC {
641 public:
642 explicit CompareNilIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
643
644 Handle<Object> CompareNil(Handle<Object> object);
645
646 static Handle<Code> GetUninitialized();
647
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000648 static void Clear(Address address, Code* target, Address constant_pool);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000649
650 static Handle<Object> DoCompareNilSlow(Isolate* isolate, NilValue nil,
651 Handle<Object> object);
652};
653
654
655class ToBooleanIC : public IC {
656 public:
657 explicit ToBooleanIC(Isolate* isolate) : IC(EXTRA_CALL_FRAME, isolate) {}
658
659 Handle<Object> ToBoolean(Handle<Object> object);
660};
661
662
663// Helper for BinaryOpIC and CompareIC.
664enum InlinedSmiCheck { ENABLE_INLINED_SMI_CHECK, DISABLE_INLINED_SMI_CHECK };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000665void PatchInlinedSmiCode(Isolate* isolate, Address address,
666 InlinedSmiCheck check);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000668} // namespace internal
669} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000670
671#endif // V8_IC_H_