blob: 6aae096fc25f6c44e54ab4c3f6c3632a62a35e1a [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) \
47 ICU(StoreIC_Miss) \
Steve Block6ded16b2010-05-10 14:33:55 +010048 ICU(StoreIC_ArrayLength) \
Steve Blocka7e24c12009-10-30 11:49:00 +000049 ICU(SharedStoreIC_ExtendStorage) \
50 ICU(KeyedStoreIC_Miss) \
51 /* Utilities for IC stubs. */ \
52 ICU(LoadCallbackProperty) \
53 ICU(StoreCallbackProperty) \
54 ICU(LoadPropertyWithInterceptorOnly) \
55 ICU(LoadPropertyWithInterceptorForLoad) \
56 ICU(LoadPropertyWithInterceptorForCall) \
Andrei Popescu402d9372010-02-26 13:31:12 +000057 ICU(KeyedLoadPropertyWithInterceptor) \
Steve Block6ded16b2010-05-10 14:33:55 +010058 ICU(StoreInterceptorProperty) \
59 ICU(BinaryOp_Patch)
Steve Blocka7e24c12009-10-30 11:49:00 +000060
61//
62// IC is the base class for LoadIC, StoreIC, CallIC, KeyedLoadIC,
63// and KeyedStoreIC.
64//
65class IC {
66 public:
67
68 // The ids for utility called from the generated code.
69 enum UtilityId {
70 #define CONST_NAME(name) k##name,
71 IC_UTIL_LIST(CONST_NAME)
72 #undef CONST_NAME
73 kUtilityCount
74 };
75
76 // Looks up the address of the named utility.
77 static Address AddressFromUtilityId(UtilityId id);
78
79 // Alias the inline cache state type to make the IC code more readable.
80 typedef InlineCacheState State;
81
82 // The IC code is either invoked with no extra frames on the stack
83 // or with a single extra frame for supporting calls.
84 enum FrameDepth {
85 NO_EXTRA_FRAME = 0,
86 EXTRA_CALL_FRAME = 1
87 };
88
89 // Construct the IC structure with the given number of extra
90 // JavaScript frames on the stack.
91 explicit IC(FrameDepth depth);
92
93 // Get the call-site target; used for determining the state.
94 Code* target() { return GetTargetAtAddress(address()); }
95 inline Address address();
96
Steve Block6ded16b2010-05-10 14:33:55 +010097 // Compute the current IC state based on the target stub, receiver and name.
98 static State StateFrom(Code* target, Object* receiver, Object* name);
Steve Blocka7e24c12009-10-30 11:49:00 +000099
100 // Clear the inline cache to initial state.
101 static void Clear(Address address);
102
103 // Computes the reloc info for this IC. This is a fairly expensive
104 // operation as it has to search through the heap to find the code
105 // object that contains this IC site.
106 RelocInfo::Mode ComputeMode();
107
108 // Returns if this IC is for contextual (no explicit receiver)
109 // access to properties.
Leon Clarkee46be812010-01-19 14:06:41 +0000110 bool IsContextual(Handle<Object> receiver) {
111 if (receiver->IsGlobalObject()) {
112 return SlowIsContextual();
113 } else {
114 ASSERT(!SlowIsContextual());
115 return false;
116 }
117 }
118
119 bool SlowIsContextual() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 return ComputeMode() == RelocInfo::CODE_TARGET_CONTEXT;
121 }
122
123 // Returns the map to use for caching stubs for a given object.
124 // This method should not be called with undefined or null.
125 static inline Map* GetCodeCacheMapForObject(Object* object);
126
127 protected:
128 Address fp() const { return fp_; }
129 Address pc() const { return *pc_address_; }
130
131#ifdef ENABLE_DEBUGGER_SUPPORT
132 // Computes the address in the original code when the code running is
133 // containing break points (calls to DebugBreakXXX builtins).
134 Address OriginalCodeAddress();
135#endif
136
137 // Set the call-site target.
138 void set_target(Code* code) { SetTargetAtAddress(address(), code); }
139
140#ifdef DEBUG
141 static void TraceIC(const char* type,
142 Handle<String> name,
143 State old_state,
144 Code* new_target,
145 const char* extra_info = "");
146#endif
147
148 static Failure* TypeError(const char* type,
149 Handle<Object> object,
150 Handle<String> name);
151 static Failure* ReferenceError(const char* type, Handle<String> name);
152
153 // Access the target code for the given IC address.
154 static inline Code* GetTargetAtAddress(Address address);
155 static inline void SetTargetAtAddress(Address address, Code* target);
156
157 private:
158 // Frame pointer for the frame that uses (calls) the IC.
159 Address fp_;
160
161 // All access to the program counter of an IC structure is indirect
162 // to make the code GC safe. This feature is crucial since
163 // GetProperty and SetProperty are called and they in turn might
164 // invoke the garbage collector.
165 Address* pc_address_;
166
167 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
168};
169
170
171// An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
172// cannot make forward declarations to an enum.
173class IC_Utility {
174 public:
175 explicit IC_Utility(IC::UtilityId id)
176 : address_(IC::AddressFromUtilityId(id)), id_(id) {}
177
178 Address address() const { return address_; }
179
180 IC::UtilityId id() const { return id_; }
181 private:
182 Address address_;
183 IC::UtilityId id_;
184};
185
186
187class CallIC: public IC {
188 public:
189 CallIC() : IC(EXTRA_CALL_FRAME) { ASSERT(target()->is_call_stub()); }
190
191 Object* LoadFunction(State state, Handle<Object> object, Handle<String> name);
192
193
194 // Code generator routines.
Leon Clarkee46be812010-01-19 14:06:41 +0000195 static void GenerateInitialize(MacroAssembler* masm, int argc) {
196 GenerateMiss(masm, argc);
197 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 static void GenerateMiss(MacroAssembler* masm, int argc);
199 static void GenerateMegamorphic(MacroAssembler* masm, int argc);
200 static void GenerateNormal(MacroAssembler* masm, int argc);
201
202 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 // Update the inline cache and the global stub cache based on the
204 // lookup result.
205 void UpdateCaches(LookupResult* lookup,
206 State state,
207 Handle<Object> object,
208 Handle<String> name);
209
210 // Returns a JSFunction if the object can be called as a function,
211 // and patches the stack to be ready for the call.
212 // Otherwise, it returns the undefined value.
213 Object* TryCallAsFunction(Object* object);
214
Leon Clarkee46be812010-01-19 14:06:41 +0000215 void ReceiverToObject(Handle<Object> object);
216
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 static void Clear(Address address, Code* target);
218 friend class IC;
219};
220
221
222class LoadIC: public IC {
223 public:
224 LoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_load_stub()); }
225
226 Object* Load(State state, Handle<Object> object, Handle<String> name);
227
228 // Code generator routines.
Andrei Popescu402d9372010-02-26 13:31:12 +0000229 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
230 static void GeneratePreMonomorphic(MacroAssembler* masm) {
231 GenerateMiss(masm);
232 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 static void GenerateMiss(MacroAssembler* masm);
234 static void GenerateMegamorphic(MacroAssembler* masm);
235 static void GenerateNormal(MacroAssembler* masm);
236
237 // Specialized code generator routines.
238 static void GenerateArrayLength(MacroAssembler* masm);
239 static void GenerateStringLength(MacroAssembler* masm);
240 static void GenerateFunctionPrototype(MacroAssembler* masm);
241
242 // The offset from the inlined patch site to the start of the
243 // inlined load instruction. It is architecture-dependent, and not
244 // used on ARM.
245 static const int kOffsetToLoadInstruction;
246
247 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 // Update the inline cache and the global stub cache based on the
249 // lookup result.
250 void UpdateCaches(LookupResult* lookup,
251 State state,
252 Handle<Object> object,
253 Handle<String> name);
254
255 // Stub accessors.
256 static Code* megamorphic_stub() {
257 return Builtins::builtin(Builtins::LoadIC_Megamorphic);
258 }
259 static Code* initialize_stub() {
260 return Builtins::builtin(Builtins::LoadIC_Initialize);
261 }
262 static Code* pre_monomorphic_stub() {
263 return Builtins::builtin(Builtins::LoadIC_PreMonomorphic);
264 }
265
266 static void Clear(Address address, Code* target);
267
268 // Clear the use of the inlined version.
269 static void ClearInlinedVersion(Address address);
270
271 static bool PatchInlinedLoad(Address address, Object* map, int index);
272
273 friend class IC;
274};
275
276
277class KeyedLoadIC: public IC {
278 public:
279 KeyedLoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_keyed_load_stub()); }
280
281 Object* Load(State state, Handle<Object> object, Handle<Object> key);
282
283 // Code generator routines.
284 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000285 static void GenerateRuntimeGetProperty(MacroAssembler* masm);
286 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
287 static void GeneratePreMonomorphic(MacroAssembler* masm) {
288 GenerateMiss(masm);
289 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000290 static void GenerateGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000291 static void GenerateString(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292
Steve Block3ce2e202009-11-05 08:53:23 +0000293 // Generators for external array types. See objects.h.
294 // These are similar to the generic IC; they optimize the case of
295 // operating upon external array types but fall back to the runtime
296 // for all other types.
297 static void GenerateExternalArray(MacroAssembler* masm,
298 ExternalArrayType array_type);
Andrei Popescu402d9372010-02-26 13:31:12 +0000299 static void GenerateIndexedInterceptor(MacroAssembler* masm);
Steve Block3ce2e202009-11-05 08:53:23 +0000300
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 // Clear the use of the inlined version.
302 static void ClearInlinedVersion(Address address);
303
Leon Clarked91b9f72010-01-27 17:25:45 +0000304 // Bit mask to be tested against bit field for the cases when
305 // generic stub should go into slow case.
306 // Access check is necessary explicitly since generic stub does not perform
307 // map checks.
308 static const int kSlowCaseBitFieldMask =
309 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
310
Steve Block6ded16b2010-05-10 14:33:55 +0100311 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 // Update the inline cache.
313 void UpdateCaches(LookupResult* lookup,
314 State state,
315 Handle<Object> object,
316 Handle<String> name);
317
318 // Stub accessors.
319 static Code* initialize_stub() {
320 return Builtins::builtin(Builtins::KeyedLoadIC_Initialize);
321 }
322 static Code* megamorphic_stub() {
323 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
324 }
325 static Code* generic_stub() {
326 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
327 }
328 static Code* pre_monomorphic_stub() {
329 return Builtins::builtin(Builtins::KeyedLoadIC_PreMonomorphic);
330 }
Leon Clarkee46be812010-01-19 14:06:41 +0000331 static Code* string_stub() {
332 return Builtins::builtin(Builtins::KeyedLoadIC_String);
333 }
Steve Block3ce2e202009-11-05 08:53:23 +0000334 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
Andrei Popescu402d9372010-02-26 13:31:12 +0000336 static Code* indexed_interceptor_stub() {
337 return Builtins::builtin(Builtins::KeyedLoadIC_IndexedInterceptor);
338 }
339
Steve Blocka7e24c12009-10-30 11:49:00 +0000340 static void Clear(Address address, Code* target);
341
342 // Support for patching the map that is checked in an inlined
343 // version of keyed load.
344 static bool PatchInlinedLoad(Address address, Object* map);
345
346 friend class IC;
347};
348
349
350class StoreIC: public IC {
351 public:
352 StoreIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_store_stub()); }
353
354 Object* Store(State state,
355 Handle<Object> object,
356 Handle<String> name,
357 Handle<Object> value);
358
359 // Code generators for stub routines. Only called once at startup.
Leon Clarke4515c472010-02-03 11:58:03 +0000360 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 static void GenerateMiss(MacroAssembler* masm);
362 static void GenerateMegamorphic(MacroAssembler* masm);
Steve Block6ded16b2010-05-10 14:33:55 +0100363 static void GenerateArrayLength(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364
365 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 // Update the inline cache and the global stub cache based on the
367 // lookup result.
368 void UpdateCaches(LookupResult* lookup,
369 State state, Handle<JSObject> receiver,
370 Handle<String> name,
371 Handle<Object> value);
372
373 // Stub accessors.
374 static Code* megamorphic_stub() {
375 return Builtins::builtin(Builtins::StoreIC_Megamorphic);
376 }
377 static Code* initialize_stub() {
378 return Builtins::builtin(Builtins::StoreIC_Initialize);
379 }
380
381 static void Clear(Address address, Code* target);
382 friend class IC;
383};
384
385
386class KeyedStoreIC: public IC {
387 public:
388 KeyedStoreIC() : IC(NO_EXTRA_FRAME) { }
389
390 Object* Store(State state,
391 Handle<Object> object,
392 Handle<Object> name,
393 Handle<Object> value);
394
395 // Code generators for stub routines. Only called once at startup.
Andrei Popescu402d9372010-02-26 13:31:12 +0000396 static void GenerateInitialize(MacroAssembler* masm) { GenerateMiss(masm); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 static void GenerateMiss(MacroAssembler* masm);
Andrei Popescu402d9372010-02-26 13:31:12 +0000398 static void GenerateRuntimeSetProperty(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 static void GenerateGeneric(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000400
Steve Block3ce2e202009-11-05 08:53:23 +0000401 // Generators for external array types. See objects.h.
402 // These are similar to the generic IC; they optimize the case of
403 // operating upon external array types but fall back to the runtime
404 // for all other types.
405 static void GenerateExternalArray(MacroAssembler* masm,
406 ExternalArrayType array_type);
407
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 // Clear the inlined version so the IC is always hit.
409 static void ClearInlinedVersion(Address address);
410
411 // Restore the inlined version so the fast case can get hit.
412 static void RestoreInlinedVersion(Address address);
413
414 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 // Update the inline cache.
416 void UpdateCaches(LookupResult* lookup,
417 State state,
418 Handle<JSObject> receiver,
419 Handle<String> name,
420 Handle<Object> value);
421
422 // Stub accessors.
423 static Code* initialize_stub() {
424 return Builtins::builtin(Builtins::KeyedStoreIC_Initialize);
425 }
426 static Code* megamorphic_stub() {
427 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
428 }
429 static Code* generic_stub() {
430 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
431 }
Steve Block3ce2e202009-11-05 08:53:23 +0000432 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000433
434 static void Clear(Address address, Code* target);
435
436 // Support for patching the map that is checked in an inlined
437 // version of keyed store.
438 // The address is the patch point for the IC call
439 // (Assembler::kCallTargetAddressOffset before the end of
440 // the call/return address).
441 // The map is the new map that the inlined code should check against.
442 static bool PatchInlinedStore(Address address, Object* map);
443
444 friend class IC;
445};
446
447
Steve Block6ded16b2010-05-10 14:33:55 +0100448class BinaryOpIC: public IC {
449 public:
450
451 enum TypeInfo {
452 DEFAULT, // Initial state. When first executed, patches to one
453 // of the following states depending on the operands types.
454 HEAP_NUMBERS, // Both arguments are HeapNumbers.
455 STRINGS, // At least one of the arguments is String.
456 GENERIC // Non-specialized case (processes any type combination).
457 };
458
459 BinaryOpIC() : IC(NO_EXTRA_FRAME) { }
460
461 void patch(Code* code);
462
463 static void Clear(Address address, Code* target);
464
465 static const char* GetName(TypeInfo type_info);
466
467 static State ToState(TypeInfo type_info);
468
469 static TypeInfo GetTypeInfo(Object* left, Object* right);
470};
471
Steve Blocka7e24c12009-10-30 11:49:00 +0000472} } // namespace v8::internal
473
474#endif // V8_IC_H_