blob: 5fd5078fedffd8d78260ec6734c211068ab532b5 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_IC_H_
29#define V8_IC_H_
30
31#include "assembler.h"
32
33namespace v8 {
34namespace internal {
35
Leon Clarkee46be812010-01-19 14:06:41 +000036// Flag indicating whether an IC stub needs to check that a backing
37// store is in dictionary case.
38enum DictionaryCheck { CHECK_DICTIONARY, DICTIONARY_CHECK_DONE };
39
40
Steve Blocka7e24c12009-10-30 11:49:00 +000041// IC_UTIL_LIST defines all utility functions called from generated
42// inline caching code. The argument for the macro, ICU, is the function name.
43#define IC_UTIL_LIST(ICU) \
44 ICU(LoadIC_Miss) \
45 ICU(KeyedLoadIC_Miss) \
46 ICU(CallIC_Miss) \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010047 ICU(KeyedCallIC_Miss) \
Steve Blocka7e24c12009-10-30 11:49:00 +000048 ICU(StoreIC_Miss) \
Steve Block6ded16b2010-05-10 14:33:55 +010049 ICU(StoreIC_ArrayLength) \
Steve Blocka7e24c12009-10-30 11:49:00 +000050 ICU(SharedStoreIC_ExtendStorage) \
51 ICU(KeyedStoreIC_Miss) \
52 /* Utilities for IC stubs. */ \
53 ICU(LoadCallbackProperty) \
54 ICU(StoreCallbackProperty) \
55 ICU(LoadPropertyWithInterceptorOnly) \
56 ICU(LoadPropertyWithInterceptorForLoad) \
57 ICU(LoadPropertyWithInterceptorForCall) \
Andrei Popescu402d9372010-02-26 13:31:12 +000058 ICU(KeyedLoadPropertyWithInterceptor) \
Steve Block6ded16b2010-05-10 14:33:55 +010059 ICU(StoreInterceptorProperty) \
60 ICU(BinaryOp_Patch)
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62//
63// IC is the base class for LoadIC, StoreIC, CallIC, KeyedLoadIC,
64// and KeyedStoreIC.
65//
66class IC {
67 public:
68
69 // The ids for utility called from the generated code.
70 enum UtilityId {
71 #define CONST_NAME(name) k##name,
72 IC_UTIL_LIST(CONST_NAME)
73 #undef CONST_NAME
74 kUtilityCount
75 };
76
77 // Looks up the address of the named utility.
78 static Address AddressFromUtilityId(UtilityId id);
79
80 // Alias the inline cache state type to make the IC code more readable.
81 typedef InlineCacheState State;
82
83 // The IC code is either invoked with no extra frames on the stack
84 // or with a single extra frame for supporting calls.
85 enum FrameDepth {
86 NO_EXTRA_FRAME = 0,
87 EXTRA_CALL_FRAME = 1
88 };
89
90 // Construct the IC structure with the given number of extra
91 // JavaScript frames on the stack.
92 explicit IC(FrameDepth depth);
93
94 // Get the call-site target; used for determining the state.
95 Code* target() { return GetTargetAtAddress(address()); }
96 inline Address address();
97
Steve Block6ded16b2010-05-10 14:33:55 +010098 // Compute the current IC state based on the target stub, receiver and name.
99 static State StateFrom(Code* target, Object* receiver, Object* name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100
101 // Clear the inline cache to initial state.
102 static void Clear(Address address);
103
104 // Computes the reloc info for this IC. This is a fairly expensive
105 // operation as it has to search through the heap to find the code
106 // object that contains this IC site.
107 RelocInfo::Mode ComputeMode();
108
109 // Returns if this IC is for contextual (no explicit receiver)
110 // access to properties.
Leon Clarkee46be812010-01-19 14:06:41 +0000111 bool IsContextual(Handle<Object> receiver) {
112 if (receiver->IsGlobalObject()) {
113 return SlowIsContextual();
114 } else {
115 ASSERT(!SlowIsContextual());
116 return false;
117 }
118 }
119
120 bool SlowIsContextual() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 return ComputeMode() == RelocInfo::CODE_TARGET_CONTEXT;
122 }
123
124 // Returns the map to use for caching stubs for a given object.
125 // This method should not be called with undefined or null.
126 static inline Map* GetCodeCacheMapForObject(Object* object);
127
128 protected:
129 Address fp() const { return fp_; }
130 Address pc() const { return *pc_address_; }
131
132#ifdef ENABLE_DEBUGGER_SUPPORT
133 // Computes the address in the original code when the code running is
134 // containing break points (calls to DebugBreakXXX builtins).
135 Address OriginalCodeAddress();
136#endif
137
138 // Set the call-site target.
139 void set_target(Code* code) { SetTargetAtAddress(address(), code); }
140
141#ifdef DEBUG
142 static void TraceIC(const char* type,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100143 Handle<Object> name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 State old_state,
145 Code* new_target,
146 const char* extra_info = "");
147#endif
148
149 static Failure* TypeError(const char* type,
150 Handle<Object> object,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100151 Handle<Object> key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 static Failure* ReferenceError(const char* type, Handle<String> name);
153
154 // Access the target code for the given IC address.
155 static inline Code* GetTargetAtAddress(Address address);
156 static inline void SetTargetAtAddress(Address address, Code* target);
157
158 private:
159 // Frame pointer for the frame that uses (calls) the IC.
160 Address fp_;
161
162 // All access to the program counter of an IC structure is indirect
163 // to make the code GC safe. This feature is crucial since
164 // GetProperty and SetProperty are called and they in turn might
165 // invoke the garbage collector.
166 Address* pc_address_;
167
168 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
169};
170
171
172// An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
173// cannot make forward declarations to an enum.
174class IC_Utility {
175 public:
176 explicit IC_Utility(IC::UtilityId id)
177 : address_(IC::AddressFromUtilityId(id)), id_(id) {}
178
179 Address address() const { return address_; }
180
181 IC::UtilityId id() const { return id_; }
182 private:
183 Address address_;
184 IC::UtilityId id_;
185};
186
187
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100188class CallICBase: public IC {
189 protected:
190 explicit CallICBase(Code::Kind kind) : IC(EXTRA_CALL_FRAME), kind_(kind) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000191
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100192 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 Object* LoadFunction(State state, Handle<Object> object, Handle<String> name);
194
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100195 protected:
196 Code::Kind kind_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 // Update the inline cache and the global stub cache based on the
199 // lookup result.
200 void UpdateCaches(LookupResult* lookup,
201 State state,
202 Handle<Object> object,
203 Handle<String> name);
204
205 // Returns a JSFunction if the object can be called as a function,
206 // and patches the stack to be ready for the call.
207 // Otherwise, it returns the undefined value.
208 Object* TryCallAsFunction(Object* object);
209
Leon Clarkee46be812010-01-19 14:06:41 +0000210 void ReceiverToObject(Handle<Object> object);
211
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 static void Clear(Address address, Code* target);
213 friend class IC;
214};
215
216
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100217class CallIC: public CallICBase {
218 public:
219 CallIC() : CallICBase(Code::CALL_IC) { ASSERT(target()->is_call_stub()); }
220
221 // Code generator routines.
222 static void GenerateInitialize(MacroAssembler* masm, int argc) {
223 GenerateMiss(masm, argc);
224 }
225 static void GenerateMiss(MacroAssembler* masm, int argc);
226 static void GenerateMegamorphic(MacroAssembler* masm, int argc);
227 static void GenerateNormal(MacroAssembler* masm, int argc);
228};
229
230
231class KeyedCallIC: public CallICBase {
232 public:
233 KeyedCallIC() : CallICBase(Code::KEYED_CALL_IC) {
234 ASSERT(target()->is_keyed_call_stub());
235 }
236
237 Object* LoadFunction(State state, Handle<Object> object, Handle<Object> key);
238
239 // Code generator routines.
240 static void GenerateInitialize(MacroAssembler* masm, int argc) {
241 GenerateMiss(masm, argc);
242 }
243 static void GenerateMiss(MacroAssembler* masm, int argc);
244 static void GenerateMegamorphic(MacroAssembler* masm, int argc);
245 static void GenerateNormal(MacroAssembler* masm, int argc);
246};
247
248
Steve Blocka7e24c12009-10-30 11:49:00 +0000249class LoadIC: public IC {
250 public:
251 LoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_load_stub()); }
252
253 Object* Load(State state, Handle<Object> object, Handle<String> name);
254
255 // Code generator routines.
Andrei Popescu402d9372010-02-26 13:31:12 +0000256 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
257 static void GeneratePreMonomorphic(MacroAssembler* masm) {
258 GenerateMiss(masm);
259 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 static void GenerateMiss(MacroAssembler* masm);
261 static void GenerateMegamorphic(MacroAssembler* masm);
262 static void GenerateNormal(MacroAssembler* masm);
263
264 // Specialized code generator routines.
265 static void GenerateArrayLength(MacroAssembler* masm);
266 static void GenerateStringLength(MacroAssembler* masm);
267 static void GenerateFunctionPrototype(MacroAssembler* masm);
268
Kristian Monsen25f61362010-05-21 11:50:48 +0100269 // Clear the use of the inlined version.
270 static void ClearInlinedVersion(Address address);
271
Steve Blocka7e24c12009-10-30 11:49:00 +0000272 // The offset from the inlined patch site to the start of the
273 // inlined load instruction. It is architecture-dependent, and not
274 // used on ARM.
275 static const int kOffsetToLoadInstruction;
276
277 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 // Update the inline cache and the global stub cache based on the
279 // lookup result.
280 void UpdateCaches(LookupResult* lookup,
281 State state,
282 Handle<Object> object,
283 Handle<String> name);
284
285 // Stub accessors.
286 static Code* megamorphic_stub() {
287 return Builtins::builtin(Builtins::LoadIC_Megamorphic);
288 }
289 static Code* initialize_stub() {
290 return Builtins::builtin(Builtins::LoadIC_Initialize);
291 }
292 static Code* pre_monomorphic_stub() {
293 return Builtins::builtin(Builtins::LoadIC_PreMonomorphic);
294 }
295
296 static void Clear(Address address, Code* target);
297
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 static bool PatchInlinedLoad(Address address, Object* map, int index);
299
300 friend class IC;
301};
302
303
304class KeyedLoadIC: public IC {
305 public:
306 KeyedLoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_keyed_load_stub()); }
307
308 Object* Load(State state, Handle<Object> object, Handle<Object> key);
309
310 // Code generator routines.
311 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000312 static void GenerateRuntimeGetProperty(MacroAssembler* masm);
313 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
314 static void GeneratePreMonomorphic(MacroAssembler* masm) {
315 GenerateMiss(masm);
316 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 static void GenerateGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000318 static void GenerateString(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000319
Steve Block3ce2e202009-11-05 08:53:23 +0000320 // Generators for external array types. See objects.h.
321 // These are similar to the generic IC; they optimize the case of
322 // operating upon external array types but fall back to the runtime
323 // for all other types.
324 static void GenerateExternalArray(MacroAssembler* masm,
325 ExternalArrayType array_type);
Andrei Popescu402d9372010-02-26 13:31:12 +0000326 static void GenerateIndexedInterceptor(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000327
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 // Clear the use of the inlined version.
329 static void ClearInlinedVersion(Address address);
330
Leon Clarked91b9f72010-01-27 17:25:45 +0000331 // Bit mask to be tested against bit field for the cases when
332 // generic stub should go into slow case.
333 // Access check is necessary explicitly since generic stub does not perform
334 // map checks.
335 static const int kSlowCaseBitFieldMask =
336 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
337
Steve Block6ded16b2010-05-10 14:33:55 +0100338 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // Update the inline cache.
340 void UpdateCaches(LookupResult* lookup,
341 State state,
342 Handle<Object> object,
343 Handle<String> name);
344
345 // Stub accessors.
346 static Code* initialize_stub() {
347 return Builtins::builtin(Builtins::KeyedLoadIC_Initialize);
348 }
349 static Code* megamorphic_stub() {
350 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
351 }
352 static Code* generic_stub() {
353 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
354 }
355 static Code* pre_monomorphic_stub() {
356 return Builtins::builtin(Builtins::KeyedLoadIC_PreMonomorphic);
357 }
Leon Clarkee46be812010-01-19 14:06:41 +0000358 static Code* string_stub() {
359 return Builtins::builtin(Builtins::KeyedLoadIC_String);
360 }
Steve Block3ce2e202009-11-05 08:53:23 +0000361 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000362
Andrei Popescu402d9372010-02-26 13:31:12 +0000363 static Code* indexed_interceptor_stub() {
364 return Builtins::builtin(Builtins::KeyedLoadIC_IndexedInterceptor);
365 }
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 static void Clear(Address address, Code* target);
368
369 // Support for patching the map that is checked in an inlined
370 // version of keyed load.
371 static bool PatchInlinedLoad(Address address, Object* map);
372
373 friend class IC;
374};
375
376
377class StoreIC: public IC {
378 public:
379 StoreIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_store_stub()); }
380
381 Object* Store(State state,
382 Handle<Object> object,
383 Handle<String> name,
384 Handle<Object> value);
385
386 // Code generators for stub routines. Only called once at startup.
Leon Clarke4515c472010-02-03 11:58:03 +0000387 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 static void GenerateMiss(MacroAssembler* masm);
389 static void GenerateMegamorphic(MacroAssembler* masm);
Steve Block6ded16b2010-05-10 14:33:55 +0100390 static void GenerateArrayLength(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000391
392 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000393 // Update the inline cache and the global stub cache based on the
394 // lookup result.
395 void UpdateCaches(LookupResult* lookup,
396 State state, Handle<JSObject> receiver,
397 Handle<String> name,
398 Handle<Object> value);
399
400 // Stub accessors.
401 static Code* megamorphic_stub() {
402 return Builtins::builtin(Builtins::StoreIC_Megamorphic);
403 }
404 static Code* initialize_stub() {
405 return Builtins::builtin(Builtins::StoreIC_Initialize);
406 }
407
408 static void Clear(Address address, Code* target);
409 friend class IC;
410};
411
412
413class KeyedStoreIC: public IC {
414 public:
415 KeyedStoreIC() : IC(NO_EXTRA_FRAME) { }
416
417 Object* Store(State state,
418 Handle<Object> object,
419 Handle<Object> name,
420 Handle<Object> value);
421
422 // Code generators for stub routines. Only called once at startup.
Andrei Popescu402d9372010-02-26 13:31:12 +0000423 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000425 static void GenerateRuntimeSetProperty(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000426 static void GenerateGeneric(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000427
Steve Block3ce2e202009-11-05 08:53:23 +0000428 // Generators for external array types. See objects.h.
429 // These are similar to the generic IC; they optimize the case of
430 // operating upon external array types but fall back to the runtime
431 // for all other types.
432 static void GenerateExternalArray(MacroAssembler* masm,
433 ExternalArrayType array_type);
434
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 // Clear the inlined version so the IC is always hit.
436 static void ClearInlinedVersion(Address address);
437
438 // Restore the inlined version so the fast case can get hit.
439 static void RestoreInlinedVersion(Address address);
440
441 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 // Update the inline cache.
443 void UpdateCaches(LookupResult* lookup,
444 State state,
445 Handle<JSObject> receiver,
446 Handle<String> name,
447 Handle<Object> value);
448
449 // Stub accessors.
450 static Code* initialize_stub() {
451 return Builtins::builtin(Builtins::KeyedStoreIC_Initialize);
452 }
453 static Code* megamorphic_stub() {
454 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
455 }
456 static Code* generic_stub() {
457 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
458 }
Steve Block3ce2e202009-11-05 08:53:23 +0000459 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000460
461 static void Clear(Address address, Code* target);
462
463 // Support for patching the map that is checked in an inlined
464 // version of keyed store.
465 // The address is the patch point for the IC call
466 // (Assembler::kCallTargetAddressOffset before the end of
467 // the call/return address).
468 // The map is the new map that the inlined code should check against.
469 static bool PatchInlinedStore(Address address, Object* map);
470
471 friend class IC;
472};
473
474
Steve Block6ded16b2010-05-10 14:33:55 +0100475class BinaryOpIC: public IC {
476 public:
477
478 enum TypeInfo {
479 DEFAULT, // Initial state. When first executed, patches to one
480 // of the following states depending on the operands types.
481 HEAP_NUMBERS, // Both arguments are HeapNumbers.
482 STRINGS, // At least one of the arguments is String.
483 GENERIC // Non-specialized case (processes any type combination).
484 };
485
486 BinaryOpIC() : IC(NO_EXTRA_FRAME) { }
487
488 void patch(Code* code);
489
490 static void Clear(Address address, Code* target);
491
492 static const char* GetName(TypeInfo type_info);
493
494 static State ToState(TypeInfo type_info);
495
496 static TypeInfo GetTypeInfo(Object* left, Object* right);
497};
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499} } // namespace v8::internal
500
501#endif // V8_IC_H_