blob: 6ebe495f16830b276ba0a115a1d1efe69a5ee018 [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#include "v8.h"
29
30#include "api.h"
31#include "arguments.h"
32#include "ic-inl.h"
33#include "stub-cache.h"
34
35namespace v8 {
36namespace internal {
37
38// -----------------------------------------------------------------------
39// StubCache implementation.
40
41
42StubCache::Entry StubCache::primary_[StubCache::kPrimaryTableSize];
43StubCache::Entry StubCache::secondary_[StubCache::kSecondaryTableSize];
44
45void StubCache::Initialize(bool create_heap_objects) {
46 ASSERT(IsPowerOf2(kPrimaryTableSize));
47 ASSERT(IsPowerOf2(kSecondaryTableSize));
48 if (create_heap_objects) {
49 HandleScope scope;
50 Clear();
51 }
52}
53
54
55Code* StubCache::Set(String* name, Map* map, Code* code) {
56 // Get the flags from the code.
57 Code::Flags flags = Code::RemoveTypeFromFlags(code->flags());
58
59 // Validate that the name does not move on scavenge, and that we
60 // can use identity checks instead of string equality checks.
61 ASSERT(!Heap::InNewSpace(name));
62 ASSERT(name->IsSymbol());
63
64 // The state bits are not important to the hash function because
65 // the stub cache only contains monomorphic stubs. Make sure that
66 // the bits are the least significant so they will be the ones
67 // masked out.
68 ASSERT(Code::ExtractICStateFromFlags(flags) == MONOMORPHIC);
69 ASSERT(Code::kFlagsICStateShift == 0);
70
71 // Make sure that the code type is not included in the hash.
72 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
73
74 // Compute the primary entry.
75 int primary_offset = PrimaryOffset(name, flags, map);
76 Entry* primary = entry(primary_, primary_offset);
77 Code* hit = primary->value;
78
79 // If the primary entry has useful data in it, we retire it to the
80 // secondary cache before overwriting it.
81 if (hit != Builtins::builtin(Builtins::Illegal)) {
82 Code::Flags primary_flags = Code::RemoveTypeFromFlags(hit->flags());
83 int secondary_offset =
84 SecondaryOffset(primary->key, primary_flags, primary_offset);
85 Entry* secondary = entry(secondary_, secondary_offset);
86 *secondary = *primary;
87 }
88
89 // Update primary cache.
90 primary->key = name;
91 primary->value = code;
92 return code;
93}
94
95
Steve Block6ded16b2010-05-10 14:33:55 +010096Object* StubCache::ComputeLoadNonexistent(String* name, JSObject* receiver) {
97 // If no global objects are present in the prototype chain, the load
98 // nonexistent IC stub can be shared for all names for a given map
99 // and we use the empty string for the map cache in that case. If
100 // there are global objects involved, we need to check global
101 // property cells in the stub and therefore the stub will be
102 // specific to the name.
103 String* cache_name = Heap::empty_string();
104 if (receiver->IsGlobalObject()) cache_name = name;
105 JSObject* last = receiver;
106 while (last->GetPrototype() != Heap::null_value()) {
107 last = JSObject::cast(last->GetPrototype());
108 if (last->IsGlobalObject()) cache_name = name;
109 }
110 // Compile the stub that is either shared for all names or
111 // name specific if there are global objects involved.
112 Code::Flags flags =
113 Code::ComputeMonomorphicFlags(Code::LOAD_IC, NONEXISTENT);
114 Object* code = receiver->map()->FindInCodeCache(cache_name, flags);
115 if (code->IsUndefined()) {
116 LoadStubCompiler compiler;
117 code = compiler.CompileLoadNonexistent(cache_name, receiver, last);
118 if (code->IsFailure()) return code;
119 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), cache_name));
120 Object* result =
121 receiver->map()->UpdateCodeCache(cache_name, Code::cast(code));
122 if (result->IsFailure()) return result;
123 }
124 return Set(name, receiver->map(), Code::cast(code));
125}
126
127
Steve Blocka7e24c12009-10-30 11:49:00 +0000128Object* StubCache::ComputeLoadField(String* name,
129 JSObject* receiver,
130 JSObject* holder,
131 int field_index) {
132 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, FIELD);
133 Object* code = receiver->map()->FindInCodeCache(name, flags);
134 if (code->IsUndefined()) {
135 LoadStubCompiler compiler;
136 code = compiler.CompileLoadField(receiver, holder, field_index, name);
137 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100138 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000140 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 }
142 return Set(name, receiver->map(), Code::cast(code));
143}
144
145
146Object* StubCache::ComputeLoadCallback(String* name,
147 JSObject* receiver,
148 JSObject* holder,
149 AccessorInfo* callback) {
150 ASSERT(v8::ToCData<Address>(callback->getter()) != 0);
151 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, CALLBACKS);
152 Object* code = receiver->map()->FindInCodeCache(name, flags);
153 if (code->IsUndefined()) {
154 LoadStubCompiler compiler;
Leon Clarkee46be812010-01-19 14:06:41 +0000155 code = compiler.CompileLoadCallback(name, receiver, holder, callback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100157 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000159 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 }
161 return Set(name, receiver->map(), Code::cast(code));
162}
163
164
165Object* StubCache::ComputeLoadConstant(String* name,
166 JSObject* receiver,
167 JSObject* holder,
168 Object* value) {
169 Code::Flags flags =
170 Code::ComputeMonomorphicFlags(Code::LOAD_IC, CONSTANT_FUNCTION);
171 Object* code = receiver->map()->FindInCodeCache(name, flags);
172 if (code->IsUndefined()) {
173 LoadStubCompiler compiler;
174 code = compiler.CompileLoadConstant(receiver, holder, value, name);
175 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100176 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000178 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 }
180 return Set(name, receiver->map(), Code::cast(code));
181}
182
183
184Object* StubCache::ComputeLoadInterceptor(String* name,
185 JSObject* receiver,
186 JSObject* holder) {
187 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, INTERCEPTOR);
188 Object* code = receiver->map()->FindInCodeCache(name, flags);
189 if (code->IsUndefined()) {
190 LoadStubCompiler compiler;
191 code = compiler.CompileLoadInterceptor(receiver, holder, name);
192 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100193 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000195 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 }
197 return Set(name, receiver->map(), Code::cast(code));
198}
199
200
201Object* StubCache::ComputeLoadNormal(String* name, JSObject* receiver) {
202 Code* code = Builtins::builtin(Builtins::LoadIC_Normal);
203 return Set(name, receiver->map(), code);
204}
205
206
207Object* StubCache::ComputeLoadGlobal(String* name,
208 JSObject* receiver,
209 GlobalObject* holder,
210 JSGlobalPropertyCell* cell,
211 bool is_dont_delete) {
212 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, NORMAL);
213 Object* code = receiver->map()->FindInCodeCache(name, flags);
214 if (code->IsUndefined()) {
215 LoadStubCompiler compiler;
216 code = compiler.CompileLoadGlobal(receiver,
217 holder,
218 cell,
219 name,
220 is_dont_delete);
221 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100222 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000224 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 }
226 return Set(name, receiver->map(), Code::cast(code));
227}
228
229
230Object* StubCache::ComputeKeyedLoadField(String* name,
231 JSObject* receiver,
232 JSObject* holder,
233 int field_index) {
234 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, FIELD);
235 Object* code = receiver->map()->FindInCodeCache(name, flags);
236 if (code->IsUndefined()) {
237 KeyedLoadStubCompiler compiler;
238 code = compiler.CompileLoadField(name, receiver, holder, field_index);
239 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100240 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
242 if (result->IsFailure()) return result;
243 }
244 return code;
245}
246
247
248Object* StubCache::ComputeKeyedLoadConstant(String* name,
249 JSObject* receiver,
250 JSObject* holder,
251 Object* value) {
252 Code::Flags flags =
253 Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CONSTANT_FUNCTION);
254 Object* code = receiver->map()->FindInCodeCache(name, flags);
255 if (code->IsUndefined()) {
256 KeyedLoadStubCompiler compiler;
257 code = compiler.CompileLoadConstant(name, receiver, holder, value);
258 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100259 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
261 if (result->IsFailure()) return result;
262 }
263 return code;
264}
265
266
267Object* StubCache::ComputeKeyedLoadInterceptor(String* name,
268 JSObject* receiver,
269 JSObject* holder) {
270 Code::Flags flags =
271 Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, INTERCEPTOR);
272 Object* code = receiver->map()->FindInCodeCache(name, flags);
273 if (code->IsUndefined()) {
274 KeyedLoadStubCompiler compiler;
275 code = compiler.CompileLoadInterceptor(receiver, holder, name);
276 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100277 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
279 if (result->IsFailure()) return result;
280 }
281 return code;
282}
283
284
285Object* StubCache::ComputeKeyedLoadCallback(String* name,
286 JSObject* receiver,
287 JSObject* holder,
288 AccessorInfo* callback) {
289 Code::Flags flags =
290 Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS);
291 Object* code = receiver->map()->FindInCodeCache(name, flags);
292 if (code->IsUndefined()) {
293 KeyedLoadStubCompiler compiler;
294 code = compiler.CompileLoadCallback(name, receiver, holder, callback);
295 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100296 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
298 if (result->IsFailure()) return result;
299 }
300 return code;
301}
302
303
304
305Object* StubCache::ComputeKeyedLoadArrayLength(String* name,
306 JSArray* receiver) {
307 Code::Flags flags =
308 Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS);
309 Object* code = receiver->map()->FindInCodeCache(name, flags);
310 if (code->IsUndefined()) {
311 KeyedLoadStubCompiler compiler;
312 code = compiler.CompileLoadArrayLength(name);
313 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100314 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
316 if (result->IsFailure()) return result;
317 }
318 return code;
319}
320
321
322Object* StubCache::ComputeKeyedLoadStringLength(String* name,
323 String* receiver) {
324 Code::Flags flags =
325 Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS);
326 Object* code = receiver->map()->FindInCodeCache(name, flags);
327 if (code->IsUndefined()) {
328 KeyedLoadStubCompiler compiler;
329 code = compiler.CompileLoadStringLength(name);
330 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100331 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000332 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
333 if (result->IsFailure()) return result;
334 }
335 return code;
336}
337
338
339Object* StubCache::ComputeKeyedLoadFunctionPrototype(String* name,
340 JSFunction* receiver) {
341 Code::Flags flags =
342 Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, CALLBACKS);
343 Object* code = receiver->map()->FindInCodeCache(name, flags);
344 if (code->IsUndefined()) {
345 KeyedLoadStubCompiler compiler;
346 code = compiler.CompileLoadFunctionPrototype(name);
347 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100348 PROFILE(CodeCreateEvent(Logger::KEYED_LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
350 if (result->IsFailure()) return result;
351 }
352 return code;
353}
354
355
356Object* StubCache::ComputeStoreField(String* name,
357 JSObject* receiver,
358 int field_index,
359 Map* transition) {
360 PropertyType type = (transition == NULL) ? FIELD : MAP_TRANSITION;
361 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, type);
362 Object* code = receiver->map()->FindInCodeCache(name, flags);
363 if (code->IsUndefined()) {
364 StoreStubCompiler compiler;
365 code = compiler.CompileStoreField(receiver, field_index, transition, name);
366 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100367 PROFILE(CodeCreateEvent(Logger::STORE_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
369 if (result->IsFailure()) return result;
370 }
371 return Set(name, receiver->map(), Code::cast(code));
372}
373
374
375Object* StubCache::ComputeStoreGlobal(String* name,
376 GlobalObject* receiver,
377 JSGlobalPropertyCell* cell) {
378 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, NORMAL);
379 Object* code = receiver->map()->FindInCodeCache(name, flags);
380 if (code->IsUndefined()) {
381 StoreStubCompiler compiler;
382 code = compiler.CompileStoreGlobal(receiver, cell, name);
383 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100384 PROFILE(CodeCreateEvent(Logger::LOAD_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000386 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 }
388 return Set(name, receiver->map(), Code::cast(code));
389}
390
391
392Object* StubCache::ComputeStoreCallback(String* name,
393 JSObject* receiver,
394 AccessorInfo* callback) {
395 ASSERT(v8::ToCData<Address>(callback->setter()) != 0);
396 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, CALLBACKS);
397 Object* code = receiver->map()->FindInCodeCache(name, flags);
398 if (code->IsUndefined()) {
399 StoreStubCompiler compiler;
400 code = compiler.CompileStoreCallback(receiver, callback, name);
401 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100402 PROFILE(CodeCreateEvent(Logger::STORE_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
404 if (result->IsFailure()) return result;
405 }
406 return Set(name, receiver->map(), Code::cast(code));
407}
408
409
410Object* StubCache::ComputeStoreInterceptor(String* name,
411 JSObject* receiver) {
412 Code::Flags flags =
413 Code::ComputeMonomorphicFlags(Code::STORE_IC, INTERCEPTOR);
414 Object* code = receiver->map()->FindInCodeCache(name, flags);
415 if (code->IsUndefined()) {
416 StoreStubCompiler compiler;
417 code = compiler.CompileStoreInterceptor(receiver, name);
418 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100419 PROFILE(CodeCreateEvent(Logger::STORE_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
421 if (result->IsFailure()) return result;
422 }
423 return Set(name, receiver->map(), Code::cast(code));
424}
425
426
427Object* StubCache::ComputeKeyedStoreField(String* name, JSObject* receiver,
428 int field_index, Map* transition) {
429 PropertyType type = (transition == NULL) ? FIELD : MAP_TRANSITION;
430 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_STORE_IC, type);
431 Object* code = receiver->map()->FindInCodeCache(name, flags);
432 if (code->IsUndefined()) {
433 KeyedStoreStubCompiler compiler;
434 code = compiler.CompileStoreField(receiver, field_index, transition, name);
435 if (code->IsFailure()) return code;
Steve Block6ded16b2010-05-10 14:33:55 +0100436 PROFILE(CodeCreateEvent(
437 Logger::KEYED_STORE_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
439 if (result->IsFailure()) return result;
440 }
441 return code;
442}
443
444
445Object* StubCache::ComputeCallConstant(int argc,
446 InLoopFlag in_loop,
447 String* name,
448 Object* object,
449 JSObject* holder,
450 JSFunction* function) {
451 // Compute the check type and the map.
452 Map* map = IC::GetCodeCacheMapForObject(object);
453
454 // Compute check type based on receiver/holder.
455 StubCompiler::CheckType check = StubCompiler::RECEIVER_MAP_CHECK;
456 if (object->IsString()) {
457 check = StubCompiler::STRING_CHECK;
458 } else if (object->IsNumber()) {
459 check = StubCompiler::NUMBER_CHECK;
460 } else if (object->IsBoolean()) {
461 check = StubCompiler::BOOLEAN_CHECK;
462 }
463
464 Code::Flags flags =
465 Code::ComputeMonomorphicFlags(Code::CALL_IC,
466 CONSTANT_FUNCTION,
467 in_loop,
468 argc);
469 Object* code = map->FindInCodeCache(name, flags);
470 if (code->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 // If the function hasn't been compiled yet, we cannot do it now
472 // because it may cause GC. To avoid this issue, we return an
473 // internal error which will make sure we do not update any
474 // caches.
475 if (!function->is_compiled()) return Failure::InternalError();
476 // Compile the stub - only create stubs for fully compiled functions.
477 CallStubCompiler compiler(argc, in_loop);
478 code = compiler.CompileCallConstant(object, holder, function, name, check);
479 if (code->IsFailure()) return code;
480 ASSERT_EQ(flags, Code::cast(code)->flags());
Steve Block6ded16b2010-05-10 14:33:55 +0100481 PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 Object* result = map->UpdateCodeCache(name, Code::cast(code));
483 if (result->IsFailure()) return result;
484 }
485 return Set(name, map, Code::cast(code));
486}
487
488
489Object* StubCache::ComputeCallField(int argc,
490 InLoopFlag in_loop,
491 String* name,
492 Object* object,
493 JSObject* holder,
494 int index) {
495 // Compute the check type and the map.
496 Map* map = IC::GetCodeCacheMapForObject(object);
497
498 // TODO(1233596): We cannot do receiver map check for non-JS objects
499 // because they may be represented as immediates without a
500 // map. Instead, we check against the map in the holder.
501 if (object->IsNumber() || object->IsBoolean() || object->IsString()) {
502 object = holder;
503 }
504
505 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::CALL_IC,
506 FIELD,
507 in_loop,
508 argc);
509 Object* code = map->FindInCodeCache(name, flags);
510 if (code->IsUndefined()) {
511 CallStubCompiler compiler(argc, in_loop);
Andrei Popescu402d9372010-02-26 13:31:12 +0000512 code = compiler.CompileCallField(JSObject::cast(object),
513 holder,
514 index,
515 name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 if (code->IsFailure()) return code;
517 ASSERT_EQ(flags, Code::cast(code)->flags());
Steve Block6ded16b2010-05-10 14:33:55 +0100518 PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 Object* result = map->UpdateCodeCache(name, Code::cast(code));
520 if (result->IsFailure()) return result;
521 }
522 return Set(name, map, Code::cast(code));
523}
524
525
526Object* StubCache::ComputeCallInterceptor(int argc,
527 String* name,
528 Object* object,
529 JSObject* holder) {
530 // Compute the check type and the map.
531 // If the object is a value, we use the prototype map for the cache.
532 Map* map = IC::GetCodeCacheMapForObject(object);
533
534 // TODO(1233596): We cannot do receiver map check for non-JS objects
535 // because they may be represented as immediates without a
536 // map. Instead, we check against the map in the holder.
537 if (object->IsNumber() || object->IsBoolean() || object->IsString()) {
538 object = holder;
539 }
540
541 Code::Flags flags =
542 Code::ComputeMonomorphicFlags(Code::CALL_IC,
543 INTERCEPTOR,
544 NOT_IN_LOOP,
545 argc);
546 Object* code = map->FindInCodeCache(name, flags);
547 if (code->IsUndefined()) {
548 CallStubCompiler compiler(argc, NOT_IN_LOOP);
Andrei Popescu402d9372010-02-26 13:31:12 +0000549 code = compiler.CompileCallInterceptor(JSObject::cast(object),
550 holder,
551 name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 if (code->IsFailure()) return code;
553 ASSERT_EQ(flags, Code::cast(code)->flags());
Steve Block6ded16b2010-05-10 14:33:55 +0100554 PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000555 Object* result = map->UpdateCodeCache(name, Code::cast(code));
556 if (result->IsFailure()) return result;
557 }
558 return Set(name, map, Code::cast(code));
559}
560
561
562Object* StubCache::ComputeCallNormal(int argc,
563 InLoopFlag in_loop,
564 String* name,
565 JSObject* receiver) {
566 Object* code = ComputeCallNormal(argc, in_loop);
567 if (code->IsFailure()) return code;
568 return Set(name, receiver->map(), Code::cast(code));
569}
570
571
572Object* StubCache::ComputeCallGlobal(int argc,
573 InLoopFlag in_loop,
574 String* name,
575 JSObject* receiver,
576 GlobalObject* holder,
577 JSGlobalPropertyCell* cell,
578 JSFunction* function) {
579 Code::Flags flags =
580 Code::ComputeMonomorphicFlags(Code::CALL_IC, NORMAL, in_loop, argc);
581 Object* code = receiver->map()->FindInCodeCache(name, flags);
582 if (code->IsUndefined()) {
583 // If the function hasn't been compiled yet, we cannot do it now
584 // because it may cause GC. To avoid this issue, we return an
585 // internal error which will make sure we do not update any
586 // caches.
587 if (!function->is_compiled()) return Failure::InternalError();
588 CallStubCompiler compiler(argc, in_loop);
589 code = compiler.CompileCallGlobal(receiver, holder, cell, function, name);
590 if (code->IsFailure()) return code;
591 ASSERT_EQ(flags, Code::cast(code)->flags());
Steve Block6ded16b2010-05-10 14:33:55 +0100592 PROFILE(CodeCreateEvent(Logger::CALL_IC_TAG, Code::cast(code), name));
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 Object* result = receiver->map()->UpdateCodeCache(name, Code::cast(code));
Andrei Popescu402d9372010-02-26 13:31:12 +0000594 if (result->IsFailure()) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000595 }
596 return Set(name, receiver->map(), Code::cast(code));
597}
598
599
600static Object* GetProbeValue(Code::Flags flags) {
601 // Use raw_unchecked... so we don't get assert failures during GC.
602 NumberDictionary* dictionary = Heap::raw_unchecked_non_monomorphic_cache();
603 int entry = dictionary->FindEntry(flags);
604 if (entry != -1) return dictionary->ValueAt(entry);
605 return Heap::raw_unchecked_undefined_value();
606}
607
608
609static Object* ProbeCache(Code::Flags flags) {
610 Object* probe = GetProbeValue(flags);
611 if (probe != Heap::undefined_value()) return probe;
612 // Seed the cache with an undefined value to make sure that any
613 // generated code object can always be inserted into the cache
614 // without causing allocation failures.
615 Object* result =
616 Heap::non_monomorphic_cache()->AtNumberPut(flags,
617 Heap::undefined_value());
618 if (result->IsFailure()) return result;
619 Heap::public_set_non_monomorphic_cache(NumberDictionary::cast(result));
620 return probe;
621}
622
623
624static Object* FillCache(Object* code) {
625 if (code->IsCode()) {
626 int entry =
627 Heap::non_monomorphic_cache()->FindEntry(
628 Code::cast(code)->flags());
629 // The entry must be present see comment in ProbeCache.
630 ASSERT(entry != -1);
631 ASSERT(Heap::non_monomorphic_cache()->ValueAt(entry) ==
632 Heap::undefined_value());
633 Heap::non_monomorphic_cache()->ValueAtPut(entry, code);
634 CHECK(GetProbeValue(Code::cast(code)->flags()) == code);
635 }
636 return code;
637}
638
639
640Code* StubCache::FindCallInitialize(int argc, InLoopFlag in_loop) {
641 Code::Flags flags =
642 Code::ComputeFlags(Code::CALL_IC, in_loop, UNINITIALIZED, NORMAL, argc);
643 Object* result = ProbeCache(flags);
644 ASSERT(!result->IsUndefined());
645 // This might be called during the marking phase of the collector
646 // hence the unchecked cast.
647 return reinterpret_cast<Code*>(result);
648}
649
650
651Object* StubCache::ComputeCallInitialize(int argc, InLoopFlag in_loop) {
652 Code::Flags flags =
653 Code::ComputeFlags(Code::CALL_IC, in_loop, UNINITIALIZED, NORMAL, argc);
654 Object* probe = ProbeCache(flags);
655 if (!probe->IsUndefined()) return probe;
656 StubCompiler compiler;
657 return FillCache(compiler.CompileCallInitialize(flags));
658}
659
660
661Object* StubCache::ComputeCallPreMonomorphic(int argc, InLoopFlag in_loop) {
662 Code::Flags flags =
663 Code::ComputeFlags(Code::CALL_IC, in_loop, PREMONOMORPHIC, NORMAL, argc);
664 Object* probe = ProbeCache(flags);
665 if (!probe->IsUndefined()) return probe;
666 StubCompiler compiler;
667 return FillCache(compiler.CompileCallPreMonomorphic(flags));
668}
669
670
671Object* StubCache::ComputeCallNormal(int argc, InLoopFlag in_loop) {
672 Code::Flags flags =
673 Code::ComputeFlags(Code::CALL_IC, in_loop, MONOMORPHIC, NORMAL, argc);
674 Object* probe = ProbeCache(flags);
675 if (!probe->IsUndefined()) return probe;
676 StubCompiler compiler;
677 return FillCache(compiler.CompileCallNormal(flags));
678}
679
680
681Object* StubCache::ComputeCallMegamorphic(int argc, InLoopFlag in_loop) {
682 Code::Flags flags =
683 Code::ComputeFlags(Code::CALL_IC, in_loop, MEGAMORPHIC, NORMAL, argc);
684 Object* probe = ProbeCache(flags);
685 if (!probe->IsUndefined()) return probe;
686 StubCompiler compiler;
687 return FillCache(compiler.CompileCallMegamorphic(flags));
688}
689
690
691Object* StubCache::ComputeCallMiss(int argc) {
692 Code::Flags flags =
693 Code::ComputeFlags(Code::STUB, NOT_IN_LOOP, MEGAMORPHIC, NORMAL, argc);
694 Object* probe = ProbeCache(flags);
695 if (!probe->IsUndefined()) return probe;
696 StubCompiler compiler;
697 return FillCache(compiler.CompileCallMiss(flags));
698}
699
700
701#ifdef ENABLE_DEBUGGER_SUPPORT
702Object* StubCache::ComputeCallDebugBreak(int argc) {
703 Code::Flags flags =
704 Code::ComputeFlags(Code::CALL_IC, NOT_IN_LOOP, DEBUG_BREAK, NORMAL, argc);
705 Object* probe = ProbeCache(flags);
706 if (!probe->IsUndefined()) return probe;
707 StubCompiler compiler;
708 return FillCache(compiler.CompileCallDebugBreak(flags));
709}
710
711
712Object* StubCache::ComputeCallDebugPrepareStepIn(int argc) {
713 Code::Flags flags =
714 Code::ComputeFlags(Code::CALL_IC,
715 NOT_IN_LOOP,
716 DEBUG_PREPARE_STEP_IN,
717 NORMAL,
718 argc);
719 Object* probe = ProbeCache(flags);
720 if (!probe->IsUndefined()) return probe;
721 StubCompiler compiler;
722 return FillCache(compiler.CompileCallDebugPrepareStepIn(flags));
723}
724#endif
725
726
727Object* StubCache::ComputeLazyCompile(int argc) {
728 Code::Flags flags =
729 Code::ComputeFlags(Code::STUB, NOT_IN_LOOP, UNINITIALIZED, NORMAL, argc);
730 Object* probe = ProbeCache(flags);
731 if (!probe->IsUndefined()) return probe;
732 StubCompiler compiler;
733 Object* result = FillCache(compiler.CompileLazyCompile(flags));
734 if (result->IsCode()) {
735 Code* code = Code::cast(result);
736 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +0100737 PROFILE(CodeCreateEvent(Logger::LAZY_COMPILE_TAG,
738 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 }
740 return result;
741}
742
743
744void StubCache::Clear() {
745 for (int i = 0; i < kPrimaryTableSize; i++) {
746 primary_[i].key = Heap::empty_string();
747 primary_[i].value = Builtins::builtin(Builtins::Illegal);
748 }
749 for (int j = 0; j < kSecondaryTableSize; j++) {
750 secondary_[j].key = Heap::empty_string();
751 secondary_[j].value = Builtins::builtin(Builtins::Illegal);
752 }
753}
754
755
756// ------------------------------------------------------------------------
757// StubCompiler implementation.
758
759
760// Support function for computing call IC miss stubs.
761Handle<Code> ComputeCallMiss(int argc) {
762 CALL_HEAP_FUNCTION(StubCache::ComputeCallMiss(argc), Code);
763}
764
765
766
767Object* LoadCallbackProperty(Arguments args) {
Steve Blockd0582a62009-12-15 09:54:21 +0000768 ASSERT(args[0]->IsJSObject());
769 ASSERT(args[1]->IsJSObject());
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 AccessorInfo* callback = AccessorInfo::cast(args[2]);
771 Address getter_address = v8::ToCData<Address>(callback->getter());
772 v8::AccessorGetter fun = FUNCTION_CAST<v8::AccessorGetter>(getter_address);
773 ASSERT(fun != NULL);
Steve Blockd0582a62009-12-15 09:54:21 +0000774 CustomArguments custom_args(callback->data(),
775 JSObject::cast(args[0]),
776 JSObject::cast(args[1]));
777 v8::AccessorInfo info(custom_args.end());
Steve Blocka7e24c12009-10-30 11:49:00 +0000778 HandleScope scope;
779 v8::Handle<v8::Value> result;
780 {
781 // Leaving JavaScript.
782 VMState state(EXTERNAL);
Steve Blockd0582a62009-12-15 09:54:21 +0000783#ifdef ENABLE_LOGGING_AND_PROFILING
784 state.set_external_callback(getter_address);
785#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000786 result = fun(v8::Utils::ToLocal(args.at<String>(4)), info);
787 }
788 RETURN_IF_SCHEDULED_EXCEPTION();
789 if (result.IsEmpty()) return Heap::undefined_value();
790 return *v8::Utils::OpenHandle(*result);
791}
792
793
794Object* StoreCallbackProperty(Arguments args) {
795 JSObject* recv = JSObject::cast(args[0]);
796 AccessorInfo* callback = AccessorInfo::cast(args[1]);
797 Address setter_address = v8::ToCData<Address>(callback->setter());
798 v8::AccessorSetter fun = FUNCTION_CAST<v8::AccessorSetter>(setter_address);
799 ASSERT(fun != NULL);
800 Handle<String> name = args.at<String>(2);
801 Handle<Object> value = args.at<Object>(3);
802 HandleScope scope;
803 LOG(ApiNamedPropertyAccess("store", recv, *name));
804 CustomArguments custom_args(callback->data(), recv, recv);
805 v8::AccessorInfo info(custom_args.end());
806 {
807 // Leaving JavaScript.
808 VMState state(EXTERNAL);
Steve Blockd0582a62009-12-15 09:54:21 +0000809#ifdef ENABLE_LOGGING_AND_PROFILING
810 state.set_external_callback(setter_address);
811#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 fun(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), info);
813 }
814 RETURN_IF_SCHEDULED_EXCEPTION();
815 return *value;
816}
817
Steve Block6ded16b2010-05-10 14:33:55 +0100818
819static const int kAccessorInfoOffsetInInterceptorArgs = 2;
820
821
Steve Blocka7e24c12009-10-30 11:49:00 +0000822/**
823 * Attempts to load a property with an interceptor (which must be present),
824 * but doesn't search the prototype chain.
825 *
826 * Returns |Heap::no_interceptor_result_sentinel()| if interceptor doesn't
827 * provide any value for the given name.
828 */
829Object* LoadPropertyWithInterceptorOnly(Arguments args) {
Steve Block6ded16b2010-05-10 14:33:55 +0100830 Handle<String> name_handle = args.at<String>(0);
831 Handle<InterceptorInfo> interceptor_info = args.at<InterceptorInfo>(1);
832 ASSERT(kAccessorInfoOffsetInInterceptorArgs == 2);
833 ASSERT(args[2]->IsJSObject()); // Receiver.
834 ASSERT(args[3]->IsJSObject()); // Holder.
835 ASSERT(args.length() == 5); // Last arg is data object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000836
837 Address getter_address = v8::ToCData<Address>(interceptor_info->getter());
838 v8::NamedPropertyGetter getter =
839 FUNCTION_CAST<v8::NamedPropertyGetter>(getter_address);
840 ASSERT(getter != NULL);
841
842 {
843 // Use the interceptor getter.
Steve Block6ded16b2010-05-10 14:33:55 +0100844 v8::AccessorInfo info(args.arguments() -
845 kAccessorInfoOffsetInInterceptorArgs);
Steve Blocka7e24c12009-10-30 11:49:00 +0000846 HandleScope scope;
847 v8::Handle<v8::Value> r;
848 {
849 // Leaving JavaScript.
850 VMState state(EXTERNAL);
851 r = getter(v8::Utils::ToLocal(name_handle), info);
852 }
853 RETURN_IF_SCHEDULED_EXCEPTION();
854 if (!r.IsEmpty()) {
855 return *v8::Utils::OpenHandle(*r);
856 }
857 }
858
859 return Heap::no_interceptor_result_sentinel();
860}
861
862
863static Object* ThrowReferenceError(String* name) {
864 // If the load is non-contextual, just return the undefined result.
865 // Note that both keyed and non-keyed loads may end up here, so we
866 // can't use either LoadIC or KeyedLoadIC constructors.
867 IC ic(IC::NO_EXTRA_FRAME);
868 ASSERT(ic.target()->is_load_stub() || ic.target()->is_keyed_load_stub());
Leon Clarkee46be812010-01-19 14:06:41 +0000869 if (!ic.SlowIsContextual()) return Heap::undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000870
871 // Throw a reference error.
872 HandleScope scope;
873 Handle<String> name_handle(name);
874 Handle<Object> error =
875 Factory::NewReferenceError("not_defined",
876 HandleVector(&name_handle, 1));
877 return Top::Throw(*error);
878}
879
880
881static Object* LoadWithInterceptor(Arguments* args,
882 PropertyAttributes* attrs) {
Steve Block6ded16b2010-05-10 14:33:55 +0100883 Handle<String> name_handle = args->at<String>(0);
884 Handle<InterceptorInfo> interceptor_info = args->at<InterceptorInfo>(1);
885 ASSERT(kAccessorInfoOffsetInInterceptorArgs == 2);
886 Handle<JSObject> receiver_handle = args->at<JSObject>(2);
887 Handle<JSObject> holder_handle = args->at<JSObject>(3);
888 ASSERT(args->length() == 5); // Last arg is data object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000889
890 Address getter_address = v8::ToCData<Address>(interceptor_info->getter());
891 v8::NamedPropertyGetter getter =
892 FUNCTION_CAST<v8::NamedPropertyGetter>(getter_address);
893 ASSERT(getter != NULL);
894
895 {
896 // Use the interceptor getter.
Steve Block6ded16b2010-05-10 14:33:55 +0100897 v8::AccessorInfo info(args->arguments() -
898 kAccessorInfoOffsetInInterceptorArgs);
Steve Blocka7e24c12009-10-30 11:49:00 +0000899 HandleScope scope;
900 v8::Handle<v8::Value> r;
901 {
902 // Leaving JavaScript.
903 VMState state(EXTERNAL);
904 r = getter(v8::Utils::ToLocal(name_handle), info);
905 }
906 RETURN_IF_SCHEDULED_EXCEPTION();
907 if (!r.IsEmpty()) {
908 *attrs = NONE;
909 return *v8::Utils::OpenHandle(*r);
910 }
911 }
912
913 Object* result = holder_handle->GetPropertyPostInterceptor(
914 *receiver_handle,
915 *name_handle,
916 attrs);
917 RETURN_IF_SCHEDULED_EXCEPTION();
918 return result;
919}
920
921
922/**
923 * Loads a property with an interceptor performing post interceptor
924 * lookup if interceptor failed.
925 */
926Object* LoadPropertyWithInterceptorForLoad(Arguments args) {
927 PropertyAttributes attr = NONE;
928 Object* result = LoadWithInterceptor(&args, &attr);
929 if (result->IsFailure()) return result;
930
931 // If the property is present, return it.
932 if (attr != ABSENT) return result;
Steve Block6ded16b2010-05-10 14:33:55 +0100933 return ThrowReferenceError(String::cast(args[0]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000934}
935
936
937Object* LoadPropertyWithInterceptorForCall(Arguments args) {
938 PropertyAttributes attr;
939 Object* result = LoadWithInterceptor(&args, &attr);
940 RETURN_IF_SCHEDULED_EXCEPTION();
941 // This is call IC. In this case, we simply return the undefined result which
942 // will lead to an exception when trying to invoke the result as a
943 // function.
944 return result;
945}
946
947
948Object* StoreInterceptorProperty(Arguments args) {
949 JSObject* recv = JSObject::cast(args[0]);
950 String* name = String::cast(args[1]);
951 Object* value = args[2];
952 ASSERT(recv->HasNamedInterceptor());
953 PropertyAttributes attr = NONE;
954 Object* result = recv->SetPropertyWithInterceptor(name, value, attr);
955 return result;
956}
957
958
Andrei Popescu402d9372010-02-26 13:31:12 +0000959Object* KeyedLoadPropertyWithInterceptor(Arguments args) {
960 JSObject* receiver = JSObject::cast(args[0]);
961 uint32_t index = Smi::cast(args[1])->value();
962 return receiver->GetElementWithInterceptor(receiver, index);
963}
964
965
Steve Blocka7e24c12009-10-30 11:49:00 +0000966Object* StubCompiler::CompileCallInitialize(Code::Flags flags) {
967 HandleScope scope;
968 int argc = Code::ExtractArgumentsCountFromFlags(flags);
969 CallIC::GenerateInitialize(masm(), argc);
970 Object* result = GetCodeWithFlags(flags, "CompileCallInitialize");
971 if (!result->IsFailure()) {
972 Counters::call_initialize_stubs.Increment();
973 Code* code = Code::cast(result);
974 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +0100975 PROFILE(CodeCreateEvent(Logger::CALL_INITIALIZE_TAG,
976 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 }
978 return result;
979}
980
981
982Object* StubCompiler::CompileCallPreMonomorphic(Code::Flags flags) {
983 HandleScope scope;
984 int argc = Code::ExtractArgumentsCountFromFlags(flags);
985 // The code of the PreMonomorphic stub is the same as the code
986 // of the Initialized stub. They just differ on the code object flags.
987 CallIC::GenerateInitialize(masm(), argc);
988 Object* result = GetCodeWithFlags(flags, "CompileCallPreMonomorphic");
989 if (!result->IsFailure()) {
990 Counters::call_premonomorphic_stubs.Increment();
991 Code* code = Code::cast(result);
992 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +0100993 PROFILE(CodeCreateEvent(Logger::CALL_PRE_MONOMORPHIC_TAG,
994 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000995 }
996 return result;
997}
998
999
1000Object* StubCompiler::CompileCallNormal(Code::Flags flags) {
1001 HandleScope scope;
1002 int argc = Code::ExtractArgumentsCountFromFlags(flags);
1003 CallIC::GenerateNormal(masm(), argc);
1004 Object* result = GetCodeWithFlags(flags, "CompileCallNormal");
1005 if (!result->IsFailure()) {
1006 Counters::call_normal_stubs.Increment();
1007 Code* code = Code::cast(result);
1008 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +01001009 PROFILE(CodeCreateEvent(Logger::CALL_NORMAL_TAG,
1010 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001011 }
1012 return result;
1013}
1014
1015
1016Object* StubCompiler::CompileCallMegamorphic(Code::Flags flags) {
1017 HandleScope scope;
1018 int argc = Code::ExtractArgumentsCountFromFlags(flags);
1019 CallIC::GenerateMegamorphic(masm(), argc);
1020 Object* result = GetCodeWithFlags(flags, "CompileCallMegamorphic");
1021 if (!result->IsFailure()) {
1022 Counters::call_megamorphic_stubs.Increment();
1023 Code* code = Code::cast(result);
1024 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +01001025 PROFILE(CodeCreateEvent(Logger::CALL_MEGAMORPHIC_TAG,
1026 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 }
1028 return result;
1029}
1030
1031
1032Object* StubCompiler::CompileCallMiss(Code::Flags flags) {
1033 HandleScope scope;
1034 int argc = Code::ExtractArgumentsCountFromFlags(flags);
1035 CallIC::GenerateMiss(masm(), argc);
1036 Object* result = GetCodeWithFlags(flags, "CompileCallMiss");
1037 if (!result->IsFailure()) {
1038 Counters::call_megamorphic_stubs.Increment();
1039 Code* code = Code::cast(result);
1040 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +01001041 PROFILE(CodeCreateEvent(Logger::CALL_MISS_TAG,
1042 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001043 }
1044 return result;
1045}
1046
1047
1048#ifdef ENABLE_DEBUGGER_SUPPORT
1049Object* StubCompiler::CompileCallDebugBreak(Code::Flags flags) {
1050 HandleScope scope;
1051 Debug::GenerateCallICDebugBreak(masm());
1052 Object* result = GetCodeWithFlags(flags, "CompileCallDebugBreak");
1053 if (!result->IsFailure()) {
1054 Code* code = Code::cast(result);
1055 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +01001056 PROFILE(CodeCreateEvent(Logger::CALL_DEBUG_BREAK_TAG,
1057 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001058 }
1059 return result;
1060}
1061
1062
1063Object* StubCompiler::CompileCallDebugPrepareStepIn(Code::Flags flags) {
1064 HandleScope scope;
1065 // Use the same code for the the step in preparations as we do for
1066 // the miss case.
1067 int argc = Code::ExtractArgumentsCountFromFlags(flags);
1068 CallIC::GenerateMiss(masm(), argc);
1069 Object* result = GetCodeWithFlags(flags, "CompileCallDebugPrepareStepIn");
1070 if (!result->IsFailure()) {
1071 Code* code = Code::cast(result);
1072 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +01001073 PROFILE(CodeCreateEvent(Logger::CALL_DEBUG_PREPARE_STEP_IN_TAG,
1074 code, code->arguments_count()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001075 }
1076 return result;
1077}
1078#endif
1079
1080
1081Object* StubCompiler::GetCodeWithFlags(Code::Flags flags, const char* name) {
1082 // Check for allocation failures during stub compilation.
1083 if (failure_->IsFailure()) return failure_;
1084
1085 // Create code object in the heap.
1086 CodeDesc desc;
1087 masm_.GetCode(&desc);
1088 Object* result = Heap::CreateCode(desc, NULL, flags, masm_.CodeObject());
1089#ifdef ENABLE_DISASSEMBLER
1090 if (FLAG_print_code_stubs && !result->IsFailure()) {
1091 Code::cast(result)->Disassemble(name);
1092 }
1093#endif
1094 return result;
1095}
1096
1097
1098Object* StubCompiler::GetCodeWithFlags(Code::Flags flags, String* name) {
1099 if (FLAG_print_code_stubs && (name != NULL)) {
1100 return GetCodeWithFlags(flags, *name->ToCString());
1101 }
1102 return GetCodeWithFlags(flags, reinterpret_cast<char*>(NULL));
1103}
1104
Andrei Popescu402d9372010-02-26 13:31:12 +00001105
Leon Clarke4515c472010-02-03 11:58:03 +00001106void StubCompiler::LookupPostInterceptor(JSObject* holder,
1107 String* name,
1108 LookupResult* lookup) {
1109 holder->LocalLookupRealNamedProperty(name, lookup);
Andrei Popescu402d9372010-02-26 13:31:12 +00001110 if (!lookup->IsProperty()) {
1111 lookup->NotFound();
Leon Clarke4515c472010-02-03 11:58:03 +00001112 Object* proto = holder->GetPrototype();
1113 if (proto != Heap::null_value()) {
1114 proto->Lookup(name, lookup);
1115 }
1116 }
1117}
1118
1119
Steve Blocka7e24c12009-10-30 11:49:00 +00001120
1121Object* LoadStubCompiler::GetCode(PropertyType type, String* name) {
1122 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::LOAD_IC, type);
1123 return GetCodeWithFlags(flags, name);
1124}
1125
1126
1127Object* KeyedLoadStubCompiler::GetCode(PropertyType type, String* name) {
1128 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_LOAD_IC, type);
1129 return GetCodeWithFlags(flags, name);
1130}
1131
1132
1133Object* StoreStubCompiler::GetCode(PropertyType type, String* name) {
1134 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::STORE_IC, type);
1135 return GetCodeWithFlags(flags, name);
1136}
1137
1138
1139Object* KeyedStoreStubCompiler::GetCode(PropertyType type, String* name) {
1140 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::KEYED_STORE_IC, type);
1141 return GetCodeWithFlags(flags, name);
1142}
1143
1144
Kristian Monsen25f61362010-05-21 11:50:48 +01001145Object* CallStubCompiler::CompileCustomCall(int generator_id,
1146 Object* object,
1147 JSObject* holder,
1148 JSFunction* function,
1149 String* fname,
1150 CheckType check) {
1151 ASSERT(generator_id >= 0 && generator_id < kNumCallGenerators);
1152 switch (generator_id) {
1153#define CALL_GENERATOR_CASE(ignored1, ignored2, name) \
1154 case k##name##CallGenerator: \
1155 return CallStubCompiler::Compile##name##Call(object, \
1156 holder, \
1157 function, \
1158 fname, \
1159 check);
1160 CUSTOM_CALL_IC_GENERATORS(CALL_GENERATOR_CASE)
1161#undef CALL_GENERATOR_CASE
1162 }
1163 UNREACHABLE();
1164 return Heap::undefined_value();
1165}
1166
1167
Steve Blocka7e24c12009-10-30 11:49:00 +00001168Object* CallStubCompiler::GetCode(PropertyType type, String* name) {
1169 int argc = arguments_.immediate();
1170 Code::Flags flags = Code::ComputeMonomorphicFlags(Code::CALL_IC,
1171 type,
1172 in_loop_,
1173 argc);
1174 return GetCodeWithFlags(flags, name);
1175}
1176
1177
Kristian Monsen25f61362010-05-21 11:50:48 +01001178Object* CallStubCompiler::GetCode(JSFunction* function) {
1179 String* function_name = NULL;
1180 if (function->shared()->name()->IsString()) {
1181 function_name = String::cast(function->shared()->name());
1182 }
1183 return GetCode(CONSTANT_FUNCTION, function_name);
1184}
1185
1186
Steve Blocka7e24c12009-10-30 11:49:00 +00001187Object* ConstructStubCompiler::GetCode() {
1188 Code::Flags flags = Code::ComputeFlags(Code::STUB);
1189 Object* result = GetCodeWithFlags(flags, "ConstructStub");
1190 if (!result->IsFailure()) {
1191 Code* code = Code::cast(result);
1192 USE(code);
Steve Block6ded16b2010-05-10 14:33:55 +01001193 PROFILE(CodeCreateEvent(Logger::STUB_TAG, code, "ConstructStub"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001194 }
1195 return result;
1196}
1197
1198
Steve Block6ded16b2010-05-10 14:33:55 +01001199CallOptimization::CallOptimization(LookupResult* lookup) {
1200 if (!lookup->IsProperty() || !lookup->IsCacheable() ||
1201 lookup->type() != CONSTANT_FUNCTION) {
1202 Initialize(NULL);
1203 } else {
1204 // We only optimize constant function calls.
1205 Initialize(lookup->GetConstantFunction());
1206 }
1207}
1208
1209CallOptimization::CallOptimization(JSFunction* function) {
1210 Initialize(function);
1211}
1212
1213
1214int CallOptimization::GetPrototypeDepthOfExpectedType(JSObject* object,
1215 JSObject* holder) const {
1216 ASSERT(is_simple_api_call_);
1217 if (expected_receiver_type_ == NULL) return 0;
1218 int depth = 0;
1219 while (object != holder) {
1220 if (object->IsInstanceOf(expected_receiver_type_)) return depth;
1221 object = JSObject::cast(object->GetPrototype());
1222 ++depth;
1223 }
1224 if (holder->IsInstanceOf(expected_receiver_type_)) return depth;
1225 return kInvalidProtoDepth;
1226}
1227
1228
1229void CallOptimization::Initialize(JSFunction* function) {
1230 constant_function_ = NULL;
1231 is_simple_api_call_ = false;
1232 expected_receiver_type_ = NULL;
1233 api_call_info_ = NULL;
1234
1235 if (function == NULL || !function->is_compiled()) return;
1236
1237 constant_function_ = function;
1238 AnalyzePossibleApiFunction(function);
1239}
1240
1241
1242void CallOptimization::AnalyzePossibleApiFunction(JSFunction* function) {
1243 SharedFunctionInfo* sfi = function->shared();
1244 if (!sfi->IsApiFunction()) return;
1245 FunctionTemplateInfo* info = sfi->get_api_func_data();
1246
1247 // Require a C++ callback.
1248 if (info->call_code()->IsUndefined()) return;
1249 api_call_info_ = CallHandlerInfo::cast(info->call_code());
1250
1251 // Accept signatures that either have no restrictions at all or
1252 // only have restrictions on the receiver.
1253 if (!info->signature()->IsUndefined()) {
1254 SignatureInfo* signature = SignatureInfo::cast(info->signature());
1255 if (!signature->args()->IsUndefined()) return;
1256 if (!signature->receiver()->IsUndefined()) {
1257 expected_receiver_type_ =
1258 FunctionTemplateInfo::cast(signature->receiver());
1259 }
1260 }
1261
1262 is_simple_api_call_ = true;
1263}
1264
1265
Steve Blocka7e24c12009-10-30 11:49:00 +00001266} } // namespace v8::internal