blob: be7f956b8e0dbaf8896cd39b09cb0b65e389ce94 [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:
298 static void Generate(MacroAssembler* masm, const ExternalReference& f);
299
300 // Update the inline cache.
301 void UpdateCaches(LookupResult* lookup,
302 State state,
303 Handle<Object> object,
304 Handle<String> name);
305
306 // Stub accessors.
307 static Code* initialize_stub() {
308 return Builtins::builtin(Builtins::KeyedLoadIC_Initialize);
309 }
310 static Code* megamorphic_stub() {
311 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
312 }
313 static Code* generic_stub() {
314 return Builtins::builtin(Builtins::KeyedLoadIC_Generic);
315 }
316 static Code* pre_monomorphic_stub() {
317 return Builtins::builtin(Builtins::KeyedLoadIC_PreMonomorphic);
318 }
Leon Clarkee46be812010-01-19 14:06:41 +0000319 static Code* string_stub() {
320 return Builtins::builtin(Builtins::KeyedLoadIC_String);
321 }
Steve Block3ce2e202009-11-05 08:53:23 +0000322 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
324 static void Clear(Address address, Code* target);
325
326 // Support for patching the map that is checked in an inlined
327 // version of keyed load.
328 static bool PatchInlinedLoad(Address address, Object* map);
329
330 friend class IC;
331};
332
333
334class StoreIC: public IC {
335 public:
336 StoreIC() : IC(NO_EXTRA_FRAME) { ASSERT(target()->is_store_stub()); }
337
338 Object* Store(State state,
339 Handle<Object> object,
340 Handle<String> name,
341 Handle<Object> value);
342
343 // Code generators for stub routines. Only called once at startup.
344 static void GenerateInitialize(MacroAssembler* masm);
345 static void GenerateMiss(MacroAssembler* masm);
346 static void GenerateMegamorphic(MacroAssembler* masm);
347 static void GenerateExtendStorage(MacroAssembler* masm);
348
349 private:
350 static void Generate(MacroAssembler* masm, const ExternalReference& f);
351
352 // Update the inline cache and the global stub cache based on the
353 // lookup result.
354 void UpdateCaches(LookupResult* lookup,
355 State state, Handle<JSObject> receiver,
356 Handle<String> name,
357 Handle<Object> value);
358
359 // Stub accessors.
360 static Code* megamorphic_stub() {
361 return Builtins::builtin(Builtins::StoreIC_Megamorphic);
362 }
363 static Code* initialize_stub() {
364 return Builtins::builtin(Builtins::StoreIC_Initialize);
365 }
366
367 static void Clear(Address address, Code* target);
368 friend class IC;
369};
370
371
372class KeyedStoreIC: public IC {
373 public:
374 KeyedStoreIC() : IC(NO_EXTRA_FRAME) { }
375
376 Object* Store(State state,
377 Handle<Object> object,
378 Handle<Object> name,
379 Handle<Object> value);
380
381 // Code generators for stub routines. Only called once at startup.
382 static void GenerateInitialize(MacroAssembler* masm);
383 static void GenerateMiss(MacroAssembler* masm);
384 static void GenerateGeneric(MacroAssembler* masm);
385 static void GenerateExtendStorage(MacroAssembler* masm);
386
Steve Block3ce2e202009-11-05 08:53:23 +0000387 // Generators for external array types. See objects.h.
388 // These are similar to the generic IC; they optimize the case of
389 // operating upon external array types but fall back to the runtime
390 // for all other types.
391 static void GenerateExternalArray(MacroAssembler* masm,
392 ExternalArrayType array_type);
393
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 // Clear the inlined version so the IC is always hit.
395 static void ClearInlinedVersion(Address address);
396
397 // Restore the inlined version so the fast case can get hit.
398 static void RestoreInlinedVersion(Address address);
399
400 private:
401 static void Generate(MacroAssembler* masm, const ExternalReference& f);
402
403 // Update the inline cache.
404 void UpdateCaches(LookupResult* lookup,
405 State state,
406 Handle<JSObject> receiver,
407 Handle<String> name,
408 Handle<Object> value);
409
410 // Stub accessors.
411 static Code* initialize_stub() {
412 return Builtins::builtin(Builtins::KeyedStoreIC_Initialize);
413 }
414 static Code* megamorphic_stub() {
415 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
416 }
417 static Code* generic_stub() {
418 return Builtins::builtin(Builtins::KeyedStoreIC_Generic);
419 }
Steve Block3ce2e202009-11-05 08:53:23 +0000420 static Code* external_array_stub(JSObject::ElementsKind elements_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +0000421
422 static void Clear(Address address, Code* target);
423
424 // Support for patching the map that is checked in an inlined
425 // version of keyed store.
426 // The address is the patch point for the IC call
427 // (Assembler::kCallTargetAddressOffset before the end of
428 // the call/return address).
429 // The map is the new map that the inlined code should check against.
430 static bool PatchInlinedStore(Address address, Object* map);
431
432 friend class IC;
433};
434
435
436} } // namespace v8::internal
437
438#endif // V8_IC_H_