blob: 0d5df963f7a4dbc88fb8149ca3df920128831466 [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
Steve Blocka7e24c12009-10-30 11:49:00 +000037// IC_UTIL_LIST defines all utility functions called from generated
38// inline caching code. The argument for the macro, ICU, is the function name.
39#define IC_UTIL_LIST(ICU) \
40 ICU(LoadIC_Miss) \
41 ICU(KeyedLoadIC_Miss) \
42 ICU(CallIC_Miss) \
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010043 ICU(KeyedCallIC_Miss) \
Steve Blocka7e24c12009-10-30 11:49:00 +000044 ICU(StoreIC_Miss) \
Steve Block6ded16b2010-05-10 14:33:55 +010045 ICU(StoreIC_ArrayLength) \
Steve Blocka7e24c12009-10-30 11:49:00 +000046 ICU(SharedStoreIC_ExtendStorage) \
47 ICU(KeyedStoreIC_Miss) \
48 /* Utilities for IC stubs. */ \
49 ICU(LoadCallbackProperty) \
50 ICU(StoreCallbackProperty) \
51 ICU(LoadPropertyWithInterceptorOnly) \
52 ICU(LoadPropertyWithInterceptorForLoad) \
53 ICU(LoadPropertyWithInterceptorForCall) \
Andrei Popescu402d9372010-02-26 13:31:12 +000054 ICU(KeyedLoadPropertyWithInterceptor) \
Steve Block6ded16b2010-05-10 14:33:55 +010055 ICU(StoreInterceptorProperty) \
56 ICU(BinaryOp_Patch)
Steve Blocka7e24c12009-10-30 11:49:00 +000057
58//
59// IC is the base class for LoadIC, StoreIC, CallIC, KeyedLoadIC,
60// and KeyedStoreIC.
61//
62class IC {
63 public:
64
65 // The ids for utility called from the generated code.
66 enum UtilityId {
67 #define CONST_NAME(name) k##name,
68 IC_UTIL_LIST(CONST_NAME)
69 #undef CONST_NAME
70 kUtilityCount
71 };
72
73 // Looks up the address of the named utility.
74 static Address AddressFromUtilityId(UtilityId id);
75
76 // Alias the inline cache state type to make the IC code more readable.
77 typedef InlineCacheState State;
78
79 // The IC code is either invoked with no extra frames on the stack
80 // or with a single extra frame for supporting calls.
81 enum FrameDepth {
82 NO_EXTRA_FRAME = 0,
83 EXTRA_CALL_FRAME = 1
84 };
85
86 // Construct the IC structure with the given number of extra
87 // JavaScript frames on the stack.
88 explicit IC(FrameDepth depth);
89
90 // Get the call-site target; used for determining the state.
91 Code* target() { return GetTargetAtAddress(address()); }
92 inline Address address();
93
Steve Block6ded16b2010-05-10 14:33:55 +010094 // Compute the current IC state based on the target stub, receiver and name.
95 static State StateFrom(Code* target, Object* receiver, Object* name);
Steve Blocka7e24c12009-10-30 11:49:00 +000096
97 // Clear the inline cache to initial state.
98 static void Clear(Address address);
99
100 // Computes the reloc info for this IC. This is a fairly expensive
101 // operation as it has to search through the heap to find the code
102 // object that contains this IC site.
103 RelocInfo::Mode ComputeMode();
104
105 // Returns if this IC is for contextual (no explicit receiver)
106 // access to properties.
Leon Clarkee46be812010-01-19 14:06:41 +0000107 bool IsContextual(Handle<Object> receiver) {
108 if (receiver->IsGlobalObject()) {
109 return SlowIsContextual();
110 } else {
111 ASSERT(!SlowIsContextual());
112 return false;
113 }
114 }
115
116 bool SlowIsContextual() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 return ComputeMode() == RelocInfo::CODE_TARGET_CONTEXT;
118 }
119
Steve Block8defd9f2010-07-08 12:39:36 +0100120 // Determines which map must be used for keeping the code stub.
121 // These methods should not be called with undefined or null.
122 static inline InlineCacheHolderFlag GetCodeCacheForObject(Object* object,
123 JSObject* holder);
124 static inline InlineCacheHolderFlag GetCodeCacheForObject(JSObject* object,
125 JSObject* holder);
126 static inline Map* GetCodeCacheMap(Object* object,
127 InlineCacheHolderFlag holder);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128
129 protected:
130 Address fp() const { return fp_; }
131 Address pc() const { return *pc_address_; }
132
133#ifdef ENABLE_DEBUGGER_SUPPORT
134 // Computes the address in the original code when the code running is
135 // containing break points (calls to DebugBreakXXX builtins).
136 Address OriginalCodeAddress();
137#endif
138
139 // Set the call-site target.
140 void set_target(Code* code) { SetTargetAtAddress(address(), code); }
141
142#ifdef DEBUG
143 static void TraceIC(const char* type,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100144 Handle<Object> name,
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 State old_state,
146 Code* new_target,
147 const char* extra_info = "");
148#endif
149
150 static Failure* TypeError(const char* type,
151 Handle<Object> object,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100152 Handle<Object> key);
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 static Failure* ReferenceError(const char* type, Handle<String> name);
154
155 // Access the target code for the given IC address.
156 static inline Code* GetTargetAtAddress(Address address);
157 static inline void SetTargetAtAddress(Address address, Code* target);
158
159 private:
160 // Frame pointer for the frame that uses (calls) the IC.
161 Address fp_;
162
163 // All access to the program counter of an IC structure is indirect
164 // to make the code GC safe. This feature is crucial since
165 // GetProperty and SetProperty are called and they in turn might
166 // invoke the garbage collector.
167 Address* pc_address_;
168
169 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
170};
171
172
173// An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
174// cannot make forward declarations to an enum.
175class IC_Utility {
176 public:
177 explicit IC_Utility(IC::UtilityId id)
178 : address_(IC::AddressFromUtilityId(id)), id_(id) {}
179
180 Address address() const { return address_; }
181
182 IC::UtilityId id() const { return id_; }
183 private:
184 Address address_;
185 IC::UtilityId id_;
186};
187
188
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100189class CallICBase: public IC {
190 protected:
191 explicit CallICBase(Code::Kind kind) : IC(EXTRA_CALL_FRAME), kind_(kind) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000192
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100193 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 Object* LoadFunction(State state, Handle<Object> object, Handle<String> name);
195
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100196 protected:
197 Code::Kind kind_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
Steve Blocka7e24c12009-10-30 11:49:00 +0000199 // Update the inline cache and the global stub cache based on the
200 // lookup result.
201 void UpdateCaches(LookupResult* lookup,
202 State state,
203 Handle<Object> object,
204 Handle<String> name);
205
206 // Returns a JSFunction if the object can be called as a function,
207 // and patches the stack to be ready for the call.
208 // Otherwise, it returns the undefined value.
209 Object* TryCallAsFunction(Object* object);
210
Leon Clarkee46be812010-01-19 14:06:41 +0000211 void ReceiverToObject(Handle<Object> object);
212
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 static void Clear(Address address, Code* target);
214 friend class IC;
215};
216
217
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100218class CallIC: public CallICBase {
219 public:
220 CallIC() : CallICBase(Code::CALL_IC) { ASSERT(target()->is_call_stub()); }
221
222 // Code generator routines.
223 static void GenerateInitialize(MacroAssembler* masm, int argc) {
224 GenerateMiss(masm, argc);
225 }
226 static void GenerateMiss(MacroAssembler* masm, int argc);
227 static void GenerateMegamorphic(MacroAssembler* masm, int argc);
228 static void GenerateNormal(MacroAssembler* masm, int argc);
229};
230
231
232class KeyedCallIC: public CallICBase {
233 public:
234 KeyedCallIC() : CallICBase(Code::KEYED_CALL_IC) {
235 ASSERT(target()->is_keyed_call_stub());
236 }
237
238 Object* LoadFunction(State state, Handle<Object> object, Handle<Object> key);
239
240 // Code generator routines.
241 static void GenerateInitialize(MacroAssembler* masm, int argc) {
242 GenerateMiss(masm, argc);
243 }
244 static void GenerateMiss(MacroAssembler* masm, int argc);
245 static void GenerateMegamorphic(MacroAssembler* masm, int argc);
246 static void GenerateNormal(MacroAssembler* masm, int argc);
247};
248
249
Steve Blocka7e24c12009-10-30 11:49:00 +0000250class LoadIC: public IC {
251 public:
252 LoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_load_stub()); }
253
254 Object* Load(State state, Handle<Object> object, Handle<String> name);
255
256 // Code generator routines.
Andrei Popescu402d9372010-02-26 13:31:12 +0000257 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
258 static void GeneratePreMonomorphic(MacroAssembler* masm) {
259 GenerateMiss(masm);
260 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 static void GenerateMiss(MacroAssembler* masm);
262 static void GenerateMegamorphic(MacroAssembler* masm);
263 static void GenerateNormal(MacroAssembler* masm);
264
265 // Specialized code generator routines.
266 static void GenerateArrayLength(MacroAssembler* masm);
267 static void GenerateStringLength(MacroAssembler* masm);
268 static void GenerateFunctionPrototype(MacroAssembler* masm);
269
Kristian Monsen25f61362010-05-21 11:50:48 +0100270 // Clear the use of the inlined version.
271 static void ClearInlinedVersion(Address address);
272
Steve Blocka7e24c12009-10-30 11:49:00 +0000273 // The offset from the inlined patch site to the start of the
274 // inlined load instruction. It is architecture-dependent, and not
275 // used on ARM.
276 static const int kOffsetToLoadInstruction;
277
278 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 // Update the inline cache and the global stub cache based on the
280 // lookup result.
281 void UpdateCaches(LookupResult* lookup,
282 State state,
283 Handle<Object> object,
284 Handle<String> name);
285
286 // Stub accessors.
287 static Code* megamorphic_stub() {
288 return Builtins::builtin(Builtins::LoadIC_Megamorphic);
289 }
290 static Code* initialize_stub() {
291 return Builtins::builtin(Builtins::LoadIC_Initialize);
292 }
293 static Code* pre_monomorphic_stub() {
294 return Builtins::builtin(Builtins::LoadIC_PreMonomorphic);
295 }
296
297 static void Clear(Address address, Code* target);
298
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 static bool PatchInlinedLoad(Address address, Object* map, int index);
300
301 friend class IC;
302};
303
304
305class KeyedLoadIC: public IC {
306 public:
307 KeyedLoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_keyed_load_stub()); }
308
309 Object* Load(State state, Handle<Object> object, Handle<Object> key);
310
311 // Code generator routines.
312 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000313 static void GenerateRuntimeGetProperty(MacroAssembler* masm);
314 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
315 static void GeneratePreMonomorphic(MacroAssembler* masm) {
316 GenerateMiss(masm);
317 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 static void GenerateGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000319 static void GenerateString(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000320
Steve Block3ce2e202009-11-05 08:53:23 +0000321 // Generators for external array types. See objects.h.
322 // These are similar to the generic IC; they optimize the case of
323 // operating upon external array types but fall back to the runtime
324 // for all other types.
325 static void GenerateExternalArray(MacroAssembler* masm,
326 ExternalArrayType array_type);
Andrei Popescu402d9372010-02-26 13:31:12 +0000327 static void GenerateIndexedInterceptor(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000328
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 // Clear the use of the inlined version.
330 static void ClearInlinedVersion(Address address);
331
Leon Clarked91b9f72010-01-27 17:25:45 +0000332 // Bit mask to be tested against bit field for the cases when
333 // generic stub should go into slow case.
334 // Access check is necessary explicitly since generic stub does not perform
335 // map checks.
336 static const int kSlowCaseBitFieldMask =
337 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
338
Steve Block6ded16b2010-05-10 14:33:55 +0100339 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 // Update the inline cache.
341 void UpdateCaches(LookupResult* lookup,
342 State state,
343 Handle<Object> object,
344 Handle<String> name);
345
346 // Stub accessors.
347 static Code* initialize_stub() {
348 return Builtins::builtin(Builtins::KeyedLoadIC_Initialize);
349 }
350 static Code* megamorphic_stub() {
351 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
352 }
353 static Code* generic_stub() {
354 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
355 }
356 static Code* pre_monomorphic_stub() {
357 return Builtins::builtin(Builtins::KeyedLoadIC_PreMonomorphic);
358 }
Leon Clarkee46be812010-01-19 14:06:41 +0000359 static Code* string_stub() {
360 return Builtins::builtin(Builtins::KeyedLoadIC_String);
361 }
Steve Block3ce2e202009-11-05 08:53:23 +0000362 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000363
Andrei Popescu402d9372010-02-26 13:31:12 +0000364 static Code* indexed_interceptor_stub() {
365 return Builtins::builtin(Builtins::KeyedLoadIC_IndexedInterceptor);
366 }
367
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 static void Clear(Address address, Code* target);
369
370 // Support for patching the map that is checked in an inlined
371 // version of keyed load.
372 static bool PatchInlinedLoad(Address address, Object* map);
373
374 friend class IC;
375};
376
377
378class StoreIC: public IC {
379 public:
380 StoreIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_store_stub()); }
381
382 Object* Store(State state,
383 Handle<Object> object,
384 Handle<String> name,
385 Handle<Object> value);
386
387 // Code generators for stub routines. Only called once at startup.
Leon Clarke4515c472010-02-03 11:58:03 +0000388 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000389 static void GenerateMiss(MacroAssembler* masm);
390 static void GenerateMegamorphic(MacroAssembler* masm);
Steve Block6ded16b2010-05-10 14:33:55 +0100391 static void GenerateArrayLength(MacroAssembler* masm);
Steve Block8defd9f2010-07-08 12:39:36 +0100392 static void GenerateNormal(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000393
394 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 // Update the inline cache and the global stub cache based on the
396 // lookup result.
397 void UpdateCaches(LookupResult* lookup,
398 State state, Handle<JSObject> receiver,
399 Handle<String> name,
400 Handle<Object> value);
401
402 // Stub accessors.
403 static Code* megamorphic_stub() {
404 return Builtins::builtin(Builtins::StoreIC_Megamorphic);
405 }
406 static Code* initialize_stub() {
407 return Builtins::builtin(Builtins::StoreIC_Initialize);
408 }
409
410 static void Clear(Address address, Code* target);
411 friend class IC;
412};
413
414
415class KeyedStoreIC: public IC {
416 public:
417 KeyedStoreIC() : IC(NO_EXTRA_FRAME) { }
418
419 Object* Store(State state,
420 Handle<Object> object,
421 Handle<Object> name,
422 Handle<Object> value);
423
424 // Code generators for stub routines. Only called once at startup.
Andrei Popescu402d9372010-02-26 13:31:12 +0000425 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000426 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000427 static void GenerateRuntimeSetProperty(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 static void GenerateGeneric(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000429
Steve Block3ce2e202009-11-05 08:53:23 +0000430 // Generators for external array types. See objects.h.
431 // These are similar to the generic IC; they optimize the case of
432 // operating upon external array types but fall back to the runtime
433 // for all other types.
434 static void GenerateExternalArray(MacroAssembler* masm,
435 ExternalArrayType array_type);
436
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 // Clear the inlined version so the IC is always hit.
438 static void ClearInlinedVersion(Address address);
439
440 // Restore the inlined version so the fast case can get hit.
441 static void RestoreInlinedVersion(Address address);
442
443 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 // Update the inline cache.
445 void UpdateCaches(LookupResult* lookup,
446 State state,
447 Handle<JSObject> receiver,
448 Handle<String> name,
449 Handle<Object> value);
450
451 // Stub accessors.
452 static Code* initialize_stub() {
453 return Builtins::builtin(Builtins::KeyedStoreIC_Initialize);
454 }
455 static Code* megamorphic_stub() {
456 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
457 }
458 static Code* generic_stub() {
459 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
460 }
Steve Block3ce2e202009-11-05 08:53:23 +0000461 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000462
463 static void Clear(Address address, Code* target);
464
465 // Support for patching the map that is checked in an inlined
466 // version of keyed store.
467 // The address is the patch point for the IC call
468 // (Assembler::kCallTargetAddressOffset before the end of
469 // the call/return address).
470 // The map is the new map that the inlined code should check against.
471 static bool PatchInlinedStore(Address address, Object* map);
472
473 friend class IC;
474};
475
476
Steve Block6ded16b2010-05-10 14:33:55 +0100477class BinaryOpIC: public IC {
478 public:
479
480 enum TypeInfo {
481 DEFAULT, // Initial state. When first executed, patches to one
482 // of the following states depending on the operands types.
483 HEAP_NUMBERS, // Both arguments are HeapNumbers.
484 STRINGS, // At least one of the arguments is String.
485 GENERIC // Non-specialized case (processes any type combination).
486 };
487
488 BinaryOpIC() : IC(NO_EXTRA_FRAME) { }
489
490 void patch(Code* code);
491
492 static void Clear(Address address, Code* target);
493
494 static const char* GetName(TypeInfo type_info);
495
496 static State ToState(TypeInfo type_info);
497
498 static TypeInfo GetTypeInfo(Object* left, Object* right);
499};
500
Steve Blocka7e24c12009-10-30 11:49:00 +0000501} } // namespace v8::internal
502
503#endif // V8_IC_H_