blob: a5fada09f40f63a6ab88368c1f00df95b066837b [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);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100126 static inline JSObject* GetCodeCacheHolder(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
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100301 static bool PatchInlinedContextualLoad(Address address,
302 Object* map,
303 Object* cell);
304
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 friend class IC;
306};
307
308
309class KeyedLoadIC: public IC {
310 public:
311 KeyedLoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_keyed_load_stub()); }
312
313 Object* Load(State state, Handle<Object> object, Handle<Object> key);
314
315 // Code generator routines.
316 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000317 static void GenerateRuntimeGetProperty(MacroAssembler* masm);
318 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
319 static void GeneratePreMonomorphic(MacroAssembler* masm) {
320 GenerateMiss(masm);
321 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 static void GenerateGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000323 static void GenerateString(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000324
Steve Block3ce2e202009-11-05 08:53:23 +0000325 // Generators for external array types. See objects.h.
326 // These are similar to the generic IC; they optimize the case of
327 // operating upon external array types but fall back to the runtime
328 // for all other types.
329 static void GenerateExternalArray(MacroAssembler* masm,
330 ExternalArrayType array_type);
Andrei Popescu402d9372010-02-26 13:31:12 +0000331 static void GenerateIndexedInterceptor(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000332
Steve Blocka7e24c12009-10-30 11:49:00 +0000333 // Clear the use of the inlined version.
334 static void ClearInlinedVersion(Address address);
335
Leon Clarked91b9f72010-01-27 17:25:45 +0000336 // Bit mask to be tested against bit field for the cases when
337 // generic stub should go into slow case.
338 // Access check is necessary explicitly since generic stub does not perform
339 // map checks.
340 static const int kSlowCaseBitFieldMask =
341 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
342
Steve Block6ded16b2010-05-10 14:33:55 +0100343 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 // Update the inline cache.
345 void UpdateCaches(LookupResult* lookup,
346 State state,
347 Handle<Object> object,
348 Handle<String> name);
349
350 // Stub accessors.
351 static Code* initialize_stub() {
352 return Builtins::builtin(Builtins::KeyedLoadIC_Initialize);
353 }
354 static Code* megamorphic_stub() {
355 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
356 }
357 static Code* generic_stub() {
358 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
359 }
360 static Code* pre_monomorphic_stub() {
361 return Builtins::builtin(Builtins::KeyedLoadIC_PreMonomorphic);
362 }
Leon Clarkee46be812010-01-19 14:06:41 +0000363 static Code* string_stub() {
364 return Builtins::builtin(Builtins::KeyedLoadIC_String);
365 }
Steve Block3ce2e202009-11-05 08:53:23 +0000366 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000367
Andrei Popescu402d9372010-02-26 13:31:12 +0000368 static Code* indexed_interceptor_stub() {
369 return Builtins::builtin(Builtins::KeyedLoadIC_IndexedInterceptor);
370 }
371
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 static void Clear(Address address, Code* target);
373
374 // Support for patching the map that is checked in an inlined
375 // version of keyed load.
376 static bool PatchInlinedLoad(Address address, Object* map);
377
378 friend class IC;
379};
380
381
382class StoreIC: public IC {
383 public:
384 StoreIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_store_stub()); }
385
386 Object* Store(State state,
387 Handle<Object> object,
388 Handle<String> name,
389 Handle<Object> value);
390
391 // Code generators for stub routines. Only called once at startup.
Leon Clarke4515c472010-02-03 11:58:03 +0000392 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000393 static void GenerateMiss(MacroAssembler* masm);
394 static void GenerateMegamorphic(MacroAssembler* masm);
Steve Block6ded16b2010-05-10 14:33:55 +0100395 static void GenerateArrayLength(MacroAssembler* masm);
Steve Block8defd9f2010-07-08 12:39:36 +0100396 static void GenerateNormal(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000397
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100398 // Clear the use of an inlined version.
399 static void ClearInlinedVersion(Address address);
400
401 // The offset from the inlined patch site to the start of the
402 // inlined store instruction.
403 static const int kOffsetToStoreInstruction;
404
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 // Update the inline cache and the global stub cache based on the
407 // lookup result.
408 void UpdateCaches(LookupResult* lookup,
409 State state, Handle<JSObject> receiver,
410 Handle<String> name,
411 Handle<Object> value);
412
413 // Stub accessors.
414 static Code* megamorphic_stub() {
415 return Builtins::builtin(Builtins::StoreIC_Megamorphic);
416 }
417 static Code* initialize_stub() {
418 return Builtins::builtin(Builtins::StoreIC_Initialize);
419 }
420
421 static void Clear(Address address, Code* target);
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100422
423 // Support for patching the index and the map that is checked in an
424 // inlined version of the named store.
425 static bool PatchInlinedStore(Address address, Object* map, int index);
426
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 friend class IC;
428};
429
430
431class KeyedStoreIC: public IC {
432 public:
433 KeyedStoreIC() : IC(NO_EXTRA_FRAME) { }
434
435 Object* Store(State state,
436 Handle<Object> object,
437 Handle<Object> name,
438 Handle<Object> value);
439
440 // Code generators for stub routines. Only called once at startup.
Andrei Popescu402d9372010-02-26 13:31:12 +0000441 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000443 static void GenerateRuntimeSetProperty(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 static void GenerateGeneric(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000445
Steve Block3ce2e202009-11-05 08:53:23 +0000446 // Generators for external array types. See objects.h.
447 // These are similar to the generic IC; they optimize the case of
448 // operating upon external array types but fall back to the runtime
449 // for all other types.
450 static void GenerateExternalArray(MacroAssembler* masm,
451 ExternalArrayType array_type);
452
Steve Blocka7e24c12009-10-30 11:49:00 +0000453 // Clear the inlined version so the IC is always hit.
454 static void ClearInlinedVersion(Address address);
455
456 // Restore the inlined version so the fast case can get hit.
457 static void RestoreInlinedVersion(Address address);
458
459 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 // Update the inline cache.
461 void UpdateCaches(LookupResult* lookup,
462 State state,
463 Handle<JSObject> receiver,
464 Handle<String> name,
465 Handle<Object> value);
466
467 // Stub accessors.
468 static Code* initialize_stub() {
469 return Builtins::builtin(Builtins::KeyedStoreIC_Initialize);
470 }
471 static Code* megamorphic_stub() {
472 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
473 }
474 static Code* generic_stub() {
475 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
476 }
Steve Block3ce2e202009-11-05 08:53:23 +0000477 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478
479 static void Clear(Address address, Code* target);
480
481 // Support for patching the map that is checked in an inlined
482 // version of keyed store.
483 // The address is the patch point for the IC call
484 // (Assembler::kCallTargetAddressOffset before the end of
485 // the call/return address).
486 // The map is the new map that the inlined code should check against.
487 static bool PatchInlinedStore(Address address, Object* map);
488
489 friend class IC;
490};
491
492
Steve Block6ded16b2010-05-10 14:33:55 +0100493class BinaryOpIC: public IC {
494 public:
495
496 enum TypeInfo {
497 DEFAULT, // Initial state. When first executed, patches to one
498 // of the following states depending on the operands types.
499 HEAP_NUMBERS, // Both arguments are HeapNumbers.
500 STRINGS, // At least one of the arguments is String.
501 GENERIC // Non-specialized case (processes any type combination).
502 };
503
504 BinaryOpIC() : IC(NO_EXTRA_FRAME) { }
505
506 void patch(Code* code);
507
508 static void Clear(Address address, Code* target);
509
510 static const char* GetName(TypeInfo type_info);
511
512 static State ToState(TypeInfo type_info);
513
514 static TypeInfo GetTypeInfo(Object* left, Object* right);
515};
516
Steve Blocka7e24c12009-10-30 11:49:00 +0000517} } // namespace v8::internal
518
519#endif // V8_IC_H_