blob: 8f0eb376f4bcfbe163f0f2db2bf6c68580d01080 [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) \
48 ICU(SharedStoreIC_ExtendStorage) \
49 ICU(KeyedStoreIC_Miss) \
50 /* Utilities for IC stubs. */ \
51 ICU(LoadCallbackProperty) \
52 ICU(StoreCallbackProperty) \
53 ICU(LoadPropertyWithInterceptorOnly) \
54 ICU(LoadPropertyWithInterceptorForLoad) \
55 ICU(LoadPropertyWithInterceptorForCall) \
56 ICU(StoreInterceptorProperty)
57
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
94 // Compute the current IC state based on the target stub and the receiver.
95 static State StateFrom(Code* target, Object* receiver);
96
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
120 // Returns the map to use for caching stubs for a given object.
121 // This method should not be called with undefined or null.
122 static inline Map* GetCodeCacheMapForObject(Object* object);
123
124 protected:
125 Address fp() const { return fp_; }
126 Address pc() const { return *pc_address_; }
127
128#ifdef ENABLE_DEBUGGER_SUPPORT
129 // Computes the address in the original code when the code running is
130 // containing break points (calls to DebugBreakXXX builtins).
131 Address OriginalCodeAddress();
132#endif
133
134 // Set the call-site target.
135 void set_target(Code* code) { SetTargetAtAddress(address(), code); }
136
137#ifdef DEBUG
138 static void TraceIC(const char* type,
139 Handle<String> name,
140 State old_state,
141 Code* new_target,
142 const char* extra_info = "");
143#endif
144
145 static Failure* TypeError(const char* type,
146 Handle<Object> object,
147 Handle<String> name);
148 static Failure* ReferenceError(const char* type, Handle<String> name);
149
150 // Access the target code for the given IC address.
151 static inline Code* GetTargetAtAddress(Address address);
152 static inline void SetTargetAtAddress(Address address, Code* target);
153
154 private:
155 // Frame pointer for the frame that uses (calls) the IC.
156 Address fp_;
157
158 // All access to the program counter of an IC structure is indirect
159 // to make the code GC safe. This feature is crucial since
160 // GetProperty and SetProperty are called and they in turn might
161 // invoke the garbage collector.
162 Address* pc_address_;
163
164 DISALLOW_IMPLICIT_CONSTRUCTORS(IC);
165};
166
167
168// An IC_Utility encapsulates IC::UtilityId. It exists mainly because you
169// cannot make forward declarations to an enum.
170class IC_Utility {
171 public:
172 explicit IC_Utility(IC::UtilityId id)
173 : address_(IC::AddressFromUtilityId(id)), id_(id) {}
174
175 Address address() const { return address_; }
176
177 IC::UtilityId id() const { return id_; }
178 private:
179 Address address_;
180 IC::UtilityId id_;
181};
182
183
184class CallIC: public IC {
185 public:
186 CallIC() : IC(EXTRA_CALL_FRAME) { ASSERT(target()->is_call_stub()); }
187
188 Object* LoadFunction(State state, Handle<Object> object, Handle<String> name);
189
190
191 // Code generator routines.
Leon Clarkee46be812010-01-19 14:06:41 +0000192 static void GenerateInitialize(MacroAssembler* masm, int argc) {
193 GenerateMiss(masm, argc);
194 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 static void GenerateMiss(MacroAssembler* masm, int argc);
196 static void GenerateMegamorphic(MacroAssembler* masm, int argc);
197 static void GenerateNormal(MacroAssembler* masm, int argc);
198
199 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 // Update the inline cache and the global stub cache based on the
201 // lookup result.
202 void UpdateCaches(LookupResult* lookup,
203 State state,
204 Handle<Object> object,
205 Handle<String> name);
206
207 // Returns a JSFunction if the object can be called as a function,
208 // and patches the stack to be ready for the call.
209 // Otherwise, it returns the undefined value.
210 Object* TryCallAsFunction(Object* object);
211
Leon Clarkee46be812010-01-19 14:06:41 +0000212 void ReceiverToObject(Handle<Object> object);
213
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 static void Clear(Address address, Code* target);
215 friend class IC;
216};
217
218
219class LoadIC: public IC {
220 public:
221 LoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_load_stub()); }
222
223 Object* Load(State state, Handle<Object> object, Handle<String> name);
224
225 // Code generator routines.
226 static void GenerateInitialize(MacroAssembler* masm);
227 static void GeneratePreMonomorphic(MacroAssembler* masm);
228 static void GenerateMiss(MacroAssembler* masm);
229 static void GenerateMegamorphic(MacroAssembler* masm);
230 static void GenerateNormal(MacroAssembler* masm);
231
232 // Specialized code generator routines.
233 static void GenerateArrayLength(MacroAssembler* masm);
234 static void GenerateStringLength(MacroAssembler* masm);
235 static void GenerateFunctionPrototype(MacroAssembler* masm);
236
237 // The offset from the inlined patch site to the start of the
238 // inlined load instruction. It is architecture-dependent, and not
239 // used on ARM.
240 static const int kOffsetToLoadInstruction;
241
242 private:
243 static void Generate(MacroAssembler* masm, const ExternalReference& f);
244
245 // Update the inline cache and the global stub cache based on the
246 // lookup result.
247 void UpdateCaches(LookupResult* lookup,
248 State state,
249 Handle<Object> object,
250 Handle<String> name);
251
252 // Stub accessors.
253 static Code* megamorphic_stub() {
254 return Builtins::builtin(Builtins::LoadIC_Megamorphic);
255 }
256 static Code* initialize_stub() {
257 return Builtins::builtin(Builtins::LoadIC_Initialize);
258 }
259 static Code* pre_monomorphic_stub() {
260 return Builtins::builtin(Builtins::LoadIC_PreMonomorphic);
261 }
262
263 static void Clear(Address address, Code* target);
264
265 // Clear the use of the inlined version.
266 static void ClearInlinedVersion(Address address);
267
268 static bool PatchInlinedLoad(Address address, Object* map, int index);
269
270 friend class IC;
271};
272
273
274class KeyedLoadIC: public IC {
275 public:
276 KeyedLoadIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_keyed_load_stub()); }
277
278 Object* Load(State state, Handle<Object> object, Handle<Object> key);
279
280 // Code generator routines.
281 static void GenerateMiss(MacroAssembler* masm);
282 static void GenerateInitialize(MacroAssembler* masm);
283 static void GeneratePreMonomorphic(MacroAssembler* masm);
284 static void GenerateGeneric(MacroAssembler* masm);
Leon Clarkee46be812010-01-19 14:06:41 +0000285 static void GenerateString(MacroAssembler* masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
Steve Block3ce2e202009-11-05 08:53:23 +0000287 // Generators for external array types. See objects.h.
288 // These are similar to the generic IC; they optimize the case of
289 // operating upon external array types but fall back to the runtime
290 // for all other types.
291 static void GenerateExternalArray(MacroAssembler* masm,
292 ExternalArrayType array_type);
293
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 // Clear the use of the inlined version.
295 static void ClearInlinedVersion(Address address);
296
297 private:
Leon Clarked91b9f72010-01-27 17:25:45 +0000298 // Bit mask to be tested against bit field for the cases when
299 // generic stub should go into slow case.
300 // Access check is necessary explicitly since generic stub does not perform
301 // map checks.
302 static const int kSlowCaseBitFieldMask =
303 (1 << Map::kIsAccessCheckNeeded) | (1 << Map::kHasIndexedInterceptor);
304
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 static void Generate(MacroAssembler* masm, const ExternalReference& f);
306
307 // Update the inline cache.
308 void UpdateCaches(LookupResult* lookup,
309 State state,
310 Handle<Object> object,
311 Handle<String> name);
312
313 // Stub accessors.
314 static Code* initialize_stub() {
315 return Builtins::builtin(Builtins::KeyedLoadIC_Initialize);
316 }
317 static Code* megamorphic_stub() {
318 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
319 }
320 static Code* generic_stub() {
321 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
322 }
323 static Code* pre_monomorphic_stub() {
324 return Builtins::builtin(Builtins::KeyedLoadIC_PreMonomorphic);
325 }
Leon Clarkee46be812010-01-19 14:06:41 +0000326 static Code* string_stub() {
327 return Builtins::builtin(Builtins::KeyedLoadIC_String);
328 }
Steve Block3ce2e202009-11-05 08:53:23 +0000329 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000330
331 static void Clear(Address address, Code* target);
332
333 // Support for patching the map that is checked in an inlined
334 // version of keyed load.
335 static bool PatchInlinedLoad(Address address, Object* map);
336
337 friend class IC;
338};
339
340
341class StoreIC: public IC {
342 public:
343 StoreIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_store_stub()); }
344
345 Object* Store(State state,
346 Handle<Object> object,
347 Handle<String> name,
348 Handle<Object> value);
349
350 // Code generators for stub routines. Only called once at startup.
351 static void GenerateInitialize(MacroAssembler* masm);
352 static void GenerateMiss(MacroAssembler* masm);
353 static void GenerateMegamorphic(MacroAssembler* masm);
354 static void GenerateExtendStorage(MacroAssembler* masm);
355
356 private:
357 static void Generate(MacroAssembler* masm, const ExternalReference& f);
358
359 // Update the inline cache and the global stub cache based on the
360 // lookup result.
361 void UpdateCaches(LookupResult* lookup,
362 State state, Handle<JSObject> receiver,
363 Handle<String> name,
364 Handle<Object> value);
365
366 // Stub accessors.
367 static Code* megamorphic_stub() {
368 return Builtins::builtin(Builtins::StoreIC_Megamorphic);
369 }
370 static Code* initialize_stub() {
371 return Builtins::builtin(Builtins::StoreIC_Initialize);
372 }
373
374 static void Clear(Address address, Code* target);
375 friend class IC;
376};
377
378
379class KeyedStoreIC: public IC {
380 public:
381 KeyedStoreIC() : IC(NO_EXTRA_FRAME) { }
382
383 Object* Store(State state,
384 Handle<Object> object,
385 Handle<Object> name,
386 Handle<Object> value);
387
388 // Code generators for stub routines. Only called once at startup.
389 static void GenerateInitialize(MacroAssembler* masm);
390 static void GenerateMiss(MacroAssembler* masm);
391 static void GenerateGeneric(MacroAssembler* masm);
392 static void GenerateExtendStorage(MacroAssembler* masm);
393
Steve Block3ce2e202009-11-05 08:53:23 +0000394 // Generators for external array types. See objects.h.
395 // These are similar to the generic IC; they optimize the case of
396 // operating upon external array types but fall back to the runtime
397 // for all other types.
398 static void GenerateExternalArray(MacroAssembler* masm,
399 ExternalArrayType array_type);
400
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 // Clear the inlined version so the IC is always hit.
402 static void ClearInlinedVersion(Address address);
403
404 // Restore the inlined version so the fast case can get hit.
405 static void RestoreInlinedVersion(Address address);
406
407 private:
408 static void Generate(MacroAssembler* masm, const ExternalReference& f);
409
410 // Update the inline cache.
411 void UpdateCaches(LookupResult* lookup,
412 State state,
413 Handle<JSObject> receiver,
414 Handle<String> name,
415 Handle<Object> value);
416
417 // Stub accessors.
418 static Code* initialize_stub() {
419 return Builtins::builtin(Builtins::KeyedStoreIC_Initialize);
420 }
421 static Code* megamorphic_stub() {
422 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
423 }
424 static Code* generic_stub() {
425 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
426 }
Steve Block3ce2e202009-11-05 08:53:23 +0000427 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000428
429 static void Clear(Address address, Code* target);
430
431 // Support for patching the map that is checked in an inlined
432 // version of keyed store.
433 // The address is the patch point for the IC call
434 // (Assembler::kCallTargetAddressOffset before the end of
435 // the call/return address).
436 // The map is the new map that the inlined code should check against.
437 static bool PatchInlinedStore(Address address, Object* map);
438
439 friend class IC;
440};
441
442
443} } // namespace v8::internal
444
445#endif // V8_IC_H_