Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: src/ic.cc

Issue 483683005: Move IC code into a subdir and move ic-compilation related code from stub-cache into ic-compiler (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix BUILD.gn Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ic.h ('k') | src/ic-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #include "src/accessors.h"
8 #include "src/api.h"
9 #include "src/arguments.h"
10 #include "src/codegen.h"
11 #include "src/conversions.h"
12 #include "src/execution.h"
13 #include "src/ic-inl.h"
14 #include "src/prototype.h"
15 #include "src/runtime.h"
16 #include "src/stub-cache.h"
17
18 namespace v8 {
19 namespace internal {
20
21 char IC::TransitionMarkFromState(IC::State state) {
22 switch (state) {
23 case UNINITIALIZED: return '0';
24 case PREMONOMORPHIC: return '.';
25 case MONOMORPHIC: return '1';
26 case PROTOTYPE_FAILURE:
27 return '^';
28 case POLYMORPHIC: return 'P';
29 case MEGAMORPHIC: return 'N';
30 case GENERIC: return 'G';
31
32 // We never see the debugger states here, because the state is
33 // computed from the original code - not the patched code. Let
34 // these cases fall through to the unreachable code below.
35 case DEBUG_STUB: break;
36 // Type-vector-based ICs resolve state to one of the above.
37 case DEFAULT:
38 break;
39 }
40 UNREACHABLE();
41 return 0;
42 }
43
44
45 const char* GetTransitionMarkModifier(KeyedAccessStoreMode mode) {
46 if (mode == STORE_NO_TRANSITION_HANDLE_COW) return ".COW";
47 if (mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS) {
48 return ".IGNORE_OOB";
49 }
50 if (IsGrowStoreMode(mode)) return ".GROW";
51 return "";
52 }
53
54
55 #ifdef DEBUG
56
57 #define TRACE_GENERIC_IC(isolate, type, reason) \
58 do { \
59 if (FLAG_trace_ic) { \
60 PrintF("[%s patching generic stub in ", type); \
61 JavaScriptFrame::PrintTop(isolate, stdout, false, true); \
62 PrintF(" (%s)]\n", reason); \
63 } \
64 } while (false)
65
66 #else
67
68 #define TRACE_GENERIC_IC(isolate, type, reason)
69
70 #endif // DEBUG
71
72
73 void IC::TraceIC(const char* type, Handle<Object> name) {
74 if (FLAG_trace_ic) {
75 Code* new_target = raw_target();
76 State new_state = new_target->ic_state();
77 TraceIC(type, name, state(), new_state);
78 }
79 }
80
81
82 void IC::TraceIC(const char* type, Handle<Object> name, State old_state,
83 State new_state) {
84 if (FLAG_trace_ic) {
85 Code* new_target = raw_target();
86 PrintF("[%s%s in ", new_target->is_keyed_stub() ? "Keyed" : "", type);
87
88 // TODO(jkummerow): Add support for "apply". The logic is roughly:
89 // marker = [fp_ + kMarkerOffset];
90 // if marker is smi and marker.value == INTERNAL and
91 // the frame's code == builtin(Builtins::kFunctionApply):
92 // then print "apply from" and advance one frame
93
94 Object* maybe_function =
95 Memory::Object_at(fp_ + JavaScriptFrameConstants::kFunctionOffset);
96 if (maybe_function->IsJSFunction()) {
97 JSFunction* function = JSFunction::cast(maybe_function);
98 JavaScriptFrame::PrintFunctionAndOffset(function, function->code(), pc(),
99 stdout, true);
100 }
101
102 ExtraICState extra_state = new_target->extra_ic_state();
103 const char* modifier = "";
104 if (new_target->kind() == Code::KEYED_STORE_IC) {
105 modifier = GetTransitionMarkModifier(
106 KeyedStoreIC::GetKeyedAccessStoreMode(extra_state));
107 }
108 PrintF(" (%c->%c%s)", TransitionMarkFromState(old_state),
109 TransitionMarkFromState(new_state), modifier);
110 #ifdef OBJECT_PRINT
111 OFStream os(stdout);
112 name->Print(os);
113 #else
114 name->ShortPrint(stdout);
115 #endif
116 PrintF("]\n");
117 }
118 }
119
120 #define TRACE_IC(type, name) TraceIC(type, name)
121 #define TRACE_VECTOR_IC(type, name, old_state, new_state) \
122 TraceIC(type, name, old_state, new_state)
123
124 IC::IC(FrameDepth depth, Isolate* isolate)
125 : isolate_(isolate),
126 target_set_(false),
127 target_maps_set_(false) {
128 // To improve the performance of the (much used) IC code, we unfold a few
129 // levels of the stack frame iteration code. This yields a ~35% speedup when
130 // running DeltaBlue and a ~25% speedup of gbemu with the '--nouse-ic' flag.
131 const Address entry =
132 Isolate::c_entry_fp(isolate->thread_local_top());
133 Address constant_pool = NULL;
134 if (FLAG_enable_ool_constant_pool) {
135 constant_pool = Memory::Address_at(
136 entry + ExitFrameConstants::kConstantPoolOffset);
137 }
138 Address* pc_address =
139 reinterpret_cast<Address*>(entry + ExitFrameConstants::kCallerPCOffset);
140 Address fp = Memory::Address_at(entry + ExitFrameConstants::kCallerFPOffset);
141 // If there's another JavaScript frame on the stack or a
142 // StubFailureTrampoline, we need to look one frame further down the stack to
143 // find the frame pointer and the return address stack slot.
144 if (depth == EXTRA_CALL_FRAME) {
145 if (FLAG_enable_ool_constant_pool) {
146 constant_pool = Memory::Address_at(
147 fp + StandardFrameConstants::kConstantPoolOffset);
148 }
149 const int kCallerPCOffset = StandardFrameConstants::kCallerPCOffset;
150 pc_address = reinterpret_cast<Address*>(fp + kCallerPCOffset);
151 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
152 }
153 #ifdef DEBUG
154 StackFrameIterator it(isolate);
155 for (int i = 0; i < depth + 1; i++) it.Advance();
156 StackFrame* frame = it.frame();
157 DCHECK(fp == frame->fp() && pc_address == frame->pc_address());
158 #endif
159 fp_ = fp;
160 if (FLAG_enable_ool_constant_pool) {
161 raw_constant_pool_ = handle(
162 ConstantPoolArray::cast(reinterpret_cast<Object*>(constant_pool)),
163 isolate);
164 }
165 pc_address_ = StackFrame::ResolveReturnAddressLocation(pc_address);
166 target_ = handle(raw_target(), isolate);
167 state_ = target_->ic_state();
168 kind_ = target_->kind();
169 extra_ic_state_ = target_->extra_ic_state();
170 }
171
172
173 SharedFunctionInfo* IC::GetSharedFunctionInfo() const {
174 // Compute the JavaScript frame for the frame pointer of this IC
175 // structure. We need this to be able to find the function
176 // corresponding to the frame.
177 StackFrameIterator it(isolate());
178 while (it.frame()->fp() != this->fp()) it.Advance();
179 JavaScriptFrame* frame = JavaScriptFrame::cast(it.frame());
180 // Find the function on the stack and both the active code for the
181 // function and the original code.
182 JSFunction* function = frame->function();
183 return function->shared();
184 }
185
186
187 Code* IC::GetCode() const {
188 HandleScope scope(isolate());
189 Handle<SharedFunctionInfo> shared(GetSharedFunctionInfo(), isolate());
190 Code* code = shared->code();
191 return code;
192 }
193
194
195 Code* IC::GetOriginalCode() const {
196 HandleScope scope(isolate());
197 Handle<SharedFunctionInfo> shared(GetSharedFunctionInfo(), isolate());
198 DCHECK(Debug::HasDebugInfo(shared));
199 Code* original_code = Debug::GetDebugInfo(shared)->original_code();
200 DCHECK(original_code->IsCode());
201 return original_code;
202 }
203
204
205 static void LookupForRead(LookupIterator* it) {
206 for (; it->IsFound(); it->Next()) {
207 switch (it->state()) {
208 case LookupIterator::NOT_FOUND:
209 case LookupIterator::TRANSITION:
210 UNREACHABLE();
211 case LookupIterator::JSPROXY:
212 return;
213 case LookupIterator::INTERCEPTOR: {
214 // If there is a getter, return; otherwise loop to perform the lookup.
215 Handle<JSObject> holder = it->GetHolder<JSObject>();
216 if (!holder->GetNamedInterceptor()->getter()->IsUndefined()) {
217 return;
218 }
219 break;
220 }
221 case LookupIterator::ACCESS_CHECK:
222 // PropertyHandlerCompiler::CheckPrototypes() knows how to emit
223 // access checks for global proxies.
224 if (it->GetHolder<JSObject>()->IsJSGlobalProxy() &&
225 it->HasAccess(v8::ACCESS_GET)) {
226 break;
227 }
228 return;
229 case LookupIterator::PROPERTY:
230 if (it->HasProperty()) return; // Yay!
231 break;
232 }
233 }
234 }
235
236
237 bool IC::TryRemoveInvalidPrototypeDependentStub(Handle<Object> receiver,
238 Handle<String> name) {
239 if (!IsNameCompatibleWithPrototypeFailure(name)) return false;
240 Handle<Map> receiver_map = TypeToMap(*receiver_type(), isolate());
241 maybe_handler_ = target()->FindHandlerForMap(*receiver_map);
242
243 // The current map wasn't handled yet. There's no reason to stay monomorphic,
244 // *unless* we're moving from a deprecated map to its replacement, or
245 // to a more general elements kind.
246 // TODO(verwaest): Check if the current map is actually what the old map
247 // would transition to.
248 if (maybe_handler_.is_null()) {
249 if (!receiver_map->IsJSObjectMap()) return false;
250 Map* first_map = FirstTargetMap();
251 if (first_map == NULL) return false;
252 Handle<Map> old_map(first_map);
253 if (old_map->is_deprecated()) return true;
254 if (IsMoreGeneralElementsKindTransition(old_map->elements_kind(),
255 receiver_map->elements_kind())) {
256 return true;
257 }
258 return false;
259 }
260
261 CacheHolderFlag flag;
262 Handle<Map> ic_holder_map(
263 GetICCacheHolder(*receiver_type(), isolate(), &flag));
264
265 DCHECK(flag != kCacheOnReceiver || receiver->IsJSObject());
266 DCHECK(flag != kCacheOnPrototype || !receiver->IsJSReceiver());
267 DCHECK(flag != kCacheOnPrototypeReceiverIsDictionary);
268
269 if (state() == MONOMORPHIC) {
270 int index = ic_holder_map->IndexInCodeCache(*name, *target());
271 if (index >= 0) {
272 ic_holder_map->RemoveFromCodeCache(*name, *target(), index);
273 }
274 }
275
276 if (receiver->IsGlobalObject()) {
277 Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver);
278 LookupIterator it(global, name, LookupIterator::CHECK_PROPERTY);
279 if (!it.IsFound() || !it.HasProperty()) return false;
280 Handle<PropertyCell> cell = it.GetPropertyCell();
281 return cell->type()->IsConstant();
282 }
283
284 return true;
285 }
286
287
288 bool IC::IsNameCompatibleWithPrototypeFailure(Handle<Object> name) {
289 if (target()->is_keyed_stub()) {
290 // Determine whether the failure is due to a name failure.
291 if (!name->IsName()) return false;
292 Name* stub_name = target()->FindFirstName();
293 if (*name != stub_name) return false;
294 }
295
296 return true;
297 }
298
299
300 void IC::UpdateState(Handle<Object> receiver, Handle<Object> name) {
301 update_receiver_type(receiver);
302 if (!name->IsString()) return;
303 if (state() != MONOMORPHIC && state() != POLYMORPHIC) return;
304 if (receiver->IsUndefined() || receiver->IsNull()) return;
305
306 // Remove the target from the code cache if it became invalid
307 // because of changes in the prototype chain to avoid hitting it
308 // again.
309 if (TryRemoveInvalidPrototypeDependentStub(receiver,
310 Handle<String>::cast(name))) {
311 MarkPrototypeFailure(name);
312 return;
313 }
314
315 // The builtins object is special. It only changes when JavaScript
316 // builtins are loaded lazily. It is important to keep inline
317 // caches for the builtins object monomorphic. Therefore, if we get
318 // an inline cache miss for the builtins object after lazily loading
319 // JavaScript builtins, we return uninitialized as the state to
320 // force the inline cache back to monomorphic state.
321 if (receiver->IsJSBuiltinsObject()) state_ = UNINITIALIZED;
322 }
323
324
325 MaybeHandle<Object> IC::TypeError(const char* type,
326 Handle<Object> object,
327 Handle<Object> key) {
328 HandleScope scope(isolate());
329 Handle<Object> args[2] = { key, object };
330 Handle<Object> error = isolate()->factory()->NewTypeError(
331 type, HandleVector(args, 2));
332 return isolate()->Throw<Object>(error);
333 }
334
335
336 MaybeHandle<Object> IC::ReferenceError(const char* type, Handle<Name> name) {
337 HandleScope scope(isolate());
338 Handle<Object> error = isolate()->factory()->NewReferenceError(
339 type, HandleVector(&name, 1));
340 return isolate()->Throw<Object>(error);
341 }
342
343
344 static void ComputeTypeInfoCountDelta(IC::State old_state, IC::State new_state,
345 int* polymorphic_delta,
346 int* generic_delta) {
347 switch (old_state) {
348 case UNINITIALIZED:
349 case PREMONOMORPHIC:
350 if (new_state == UNINITIALIZED || new_state == PREMONOMORPHIC) break;
351 if (new_state == MONOMORPHIC || new_state == POLYMORPHIC) {
352 *polymorphic_delta = 1;
353 } else if (new_state == MEGAMORPHIC || new_state == GENERIC) {
354 *generic_delta = 1;
355 }
356 break;
357 case MONOMORPHIC:
358 case POLYMORPHIC:
359 if (new_state == MONOMORPHIC || new_state == POLYMORPHIC) break;
360 *polymorphic_delta = -1;
361 if (new_state == MEGAMORPHIC || new_state == GENERIC) {
362 *generic_delta = 1;
363 }
364 break;
365 case MEGAMORPHIC:
366 case GENERIC:
367 if (new_state == MEGAMORPHIC || new_state == GENERIC) break;
368 *generic_delta = -1;
369 if (new_state == MONOMORPHIC || new_state == POLYMORPHIC) {
370 *polymorphic_delta = 1;
371 }
372 break;
373 case PROTOTYPE_FAILURE:
374 case DEBUG_STUB:
375 case DEFAULT:
376 UNREACHABLE();
377 }
378 }
379
380
381 void IC::OnTypeFeedbackChanged(Isolate* isolate, Address address,
382 State old_state, State new_state,
383 bool target_remains_ic_stub) {
384 Code* host = isolate->
385 inner_pointer_to_code_cache()->GetCacheEntry(address)->code;
386 if (host->kind() != Code::FUNCTION) return;
387
388 if (FLAG_type_info_threshold > 0 && target_remains_ic_stub &&
389 // Not all Code objects have TypeFeedbackInfo.
390 host->type_feedback_info()->IsTypeFeedbackInfo()) {
391 int polymorphic_delta = 0; // "Polymorphic" here includes monomorphic.
392 int generic_delta = 0; // "Generic" here includes megamorphic.
393 ComputeTypeInfoCountDelta(old_state, new_state, &polymorphic_delta,
394 &generic_delta);
395 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(host->type_feedback_info());
396 info->change_ic_with_type_info_count(polymorphic_delta);
397 info->change_ic_generic_count(generic_delta);
398 }
399 if (host->type_feedback_info()->IsTypeFeedbackInfo()) {
400 TypeFeedbackInfo* info =
401 TypeFeedbackInfo::cast(host->type_feedback_info());
402 info->change_own_type_change_checksum();
403 }
404 host->set_profiler_ticks(0);
405 isolate->runtime_profiler()->NotifyICChanged();
406 // TODO(2029): When an optimized function is patched, it would
407 // be nice to propagate the corresponding type information to its
408 // unoptimized version for the benefit of later inlining.
409 }
410
411
412 void IC::PostPatching(Address address, Code* target, Code* old_target) {
413 // Type vector based ICs update these statistics at a different time because
414 // they don't always patch on state change.
415 if (target->kind() == Code::CALL_IC) return;
416
417 Isolate* isolate = target->GetHeap()->isolate();
418 State old_state = UNINITIALIZED;
419 State new_state = UNINITIALIZED;
420 bool target_remains_ic_stub = false;
421 if (old_target->is_inline_cache_stub() && target->is_inline_cache_stub()) {
422 old_state = old_target->ic_state();
423 new_state = target->ic_state();
424 target_remains_ic_stub = true;
425 }
426
427 OnTypeFeedbackChanged(isolate, address, old_state, new_state,
428 target_remains_ic_stub);
429 }
430
431
432 void IC::RegisterWeakMapDependency(Handle<Code> stub) {
433 if (FLAG_collect_maps && FLAG_weak_embedded_maps_in_ic &&
434 stub->CanBeWeakStub()) {
435 DCHECK(!stub->is_weak_stub());
436 MapHandleList maps;
437 stub->FindAllMaps(&maps);
438 if (maps.length() == 1 && stub->IsWeakObjectInIC(*maps.at(0))) {
439 Map::AddDependentIC(maps.at(0), stub);
440 stub->mark_as_weak_stub();
441 if (FLAG_enable_ool_constant_pool) {
442 stub->constant_pool()->set_weak_object_state(
443 ConstantPoolArray::WEAK_OBJECTS_IN_IC);
444 }
445 }
446 }
447 }
448
449
450 void IC::InvalidateMaps(Code* stub) {
451 DCHECK(stub->is_weak_stub());
452 stub->mark_as_invalidated_weak_stub();
453 Isolate* isolate = stub->GetIsolate();
454 Heap* heap = isolate->heap();
455 Object* undefined = heap->undefined_value();
456 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
457 for (RelocIterator it(stub, mode_mask); !it.done(); it.next()) {
458 RelocInfo::Mode mode = it.rinfo()->rmode();
459 if (mode == RelocInfo::EMBEDDED_OBJECT &&
460 it.rinfo()->target_object()->IsMap()) {
461 it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER);
462 }
463 }
464 CpuFeatures::FlushICache(stub->instruction_start(), stub->instruction_size());
465 }
466
467
468 void IC::Clear(Isolate* isolate, Address address,
469 ConstantPoolArray* constant_pool) {
470 Code* target = GetTargetAtAddress(address, constant_pool);
471
472 // Don't clear debug break inline cache as it will remove the break point.
473 if (target->is_debug_stub()) return;
474
475 switch (target->kind()) {
476 case Code::LOAD_IC:
477 return LoadIC::Clear(isolate, address, target, constant_pool);
478 case Code::KEYED_LOAD_IC:
479 return KeyedLoadIC::Clear(isolate, address, target, constant_pool);
480 case Code::STORE_IC:
481 return StoreIC::Clear(isolate, address, target, constant_pool);
482 case Code::KEYED_STORE_IC:
483 return KeyedStoreIC::Clear(isolate, address, target, constant_pool);
484 case Code::CALL_IC:
485 return CallIC::Clear(isolate, address, target, constant_pool);
486 case Code::COMPARE_IC:
487 return CompareIC::Clear(isolate, address, target, constant_pool);
488 case Code::COMPARE_NIL_IC:
489 return CompareNilIC::Clear(address, target, constant_pool);
490 case Code::BINARY_OP_IC:
491 case Code::TO_BOOLEAN_IC:
492 // Clearing these is tricky and does not
493 // make any performance difference.
494 return;
495 default: UNREACHABLE();
496 }
497 }
498
499
500 void KeyedLoadIC::Clear(Isolate* isolate,
501 Address address,
502 Code* target,
503 ConstantPoolArray* constant_pool) {
504 if (IsCleared(target)) return;
505 // Make sure to also clear the map used in inline fast cases. If we
506 // do not clear these maps, cached code can keep objects alive
507 // through the embedded maps.
508 SetTargetAtAddress(address, *pre_monomorphic_stub(isolate), constant_pool);
509 }
510
511
512 void CallIC::Clear(Isolate* isolate,
513 Address address,
514 Code* target,
515 ConstantPoolArray* constant_pool) {
516 // Currently, CallIC doesn't have state changes.
517 }
518
519
520 void LoadIC::Clear(Isolate* isolate,
521 Address address,
522 Code* target,
523 ConstantPoolArray* constant_pool) {
524 if (IsCleared(target)) return;
525 Code* code = PropertyICCompiler::FindPreMonomorphic(isolate, Code::LOAD_IC,
526 target->extra_ic_state());
527 SetTargetAtAddress(address, code, constant_pool);
528 }
529
530
531 void StoreIC::Clear(Isolate* isolate,
532 Address address,
533 Code* target,
534 ConstantPoolArray* constant_pool) {
535 if (IsCleared(target)) return;
536 Code* code = PropertyICCompiler::FindPreMonomorphic(isolate, Code::STORE_IC,
537 target->extra_ic_state());
538 SetTargetAtAddress(address, code, constant_pool);
539 }
540
541
542 void KeyedStoreIC::Clear(Isolate* isolate,
543 Address address,
544 Code* target,
545 ConstantPoolArray* constant_pool) {
546 if (IsCleared(target)) return;
547 SetTargetAtAddress(address,
548 *pre_monomorphic_stub(
549 isolate, StoreIC::GetStrictMode(target->extra_ic_state())),
550 constant_pool);
551 }
552
553
554 void CompareIC::Clear(Isolate* isolate,
555 Address address,
556 Code* target,
557 ConstantPoolArray* constant_pool) {
558 DCHECK(CodeStub::GetMajorKey(target) == CodeStub::CompareIC);
559 CompareIC::State handler_state;
560 Token::Value op;
561 ICCompareStub::DecodeKey(target->stub_key(), NULL, NULL, &handler_state, &op);
562 // Only clear CompareICs that can retain objects.
563 if (handler_state != KNOWN_OBJECT) return;
564 SetTargetAtAddress(address, GetRawUninitialized(isolate, op), constant_pool);
565 PatchInlinedSmiCode(address, DISABLE_INLINED_SMI_CHECK);
566 }
567
568
569 // static
570 Handle<Code> KeyedLoadIC::generic_stub(Isolate* isolate) {
571 if (FLAG_compiled_keyed_generic_loads) {
572 return KeyedLoadGenericStub(isolate).GetCode();
573 } else {
574 return isolate->builtins()->KeyedLoadIC_Generic();
575 }
576 }
577
578
579 static bool MigrateDeprecated(Handle<Object> object) {
580 if (!object->IsJSObject()) return false;
581 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
582 if (!receiver->map()->is_deprecated()) return false;
583 JSObject::MigrateInstance(Handle<JSObject>::cast(object));
584 return true;
585 }
586
587
588 MaybeHandle<Object> LoadIC::Load(Handle<Object> object, Handle<Name> name) {
589 // If the object is undefined or null it's illegal to try to get any
590 // of its properties; throw a TypeError in that case.
591 if (object->IsUndefined() || object->IsNull()) {
592 return TypeError("non_object_property_load", object, name);
593 }
594
595 // Check if the name is trivially convertible to an index and get
596 // the element or char if so.
597 uint32_t index;
598 if (kind() == Code::KEYED_LOAD_IC && name->AsArrayIndex(&index)) {
599 // Rewrite to the generic keyed load stub.
600 if (FLAG_use_ic) {
601 set_target(*KeyedLoadIC::generic_stub(isolate()));
602 TRACE_IC("LoadIC", name);
603 TRACE_GENERIC_IC(isolate(), "LoadIC", "name as array index");
604 }
605 Handle<Object> result;
606 ASSIGN_RETURN_ON_EXCEPTION(
607 isolate(),
608 result,
609 Runtime::GetElementOrCharAt(isolate(), object, index),
610 Object);
611 return result;
612 }
613
614 bool use_ic = MigrateDeprecated(object) ? false : FLAG_use_ic;
615
616 // Named lookup in the object.
617 LookupIterator it(object, name);
618 LookupForRead(&it);
619
620 if (it.IsFound() || !IsUndeclaredGlobal(object)) {
621 // Update inline cache and stub cache.
622 if (use_ic) UpdateCaches(&it);
623
624 // Get the property.
625 Handle<Object> result;
626 ASSIGN_RETURN_ON_EXCEPTION(isolate(), result, Object::GetProperty(&it),
627 Object);
628 if (it.IsFound()) {
629 return result;
630 } else if (!IsUndeclaredGlobal(object)) {
631 LOG(isolate(), SuspectReadEvent(*name, *object));
632 return result;
633 }
634 }
635 return ReferenceError("not_defined", name);
636 }
637
638
639 static bool AddOneReceiverMapIfMissing(MapHandleList* receiver_maps,
640 Handle<Map> new_receiver_map) {
641 DCHECK(!new_receiver_map.is_null());
642 for (int current = 0; current < receiver_maps->length(); ++current) {
643 if (!receiver_maps->at(current).is_null() &&
644 receiver_maps->at(current).is_identical_to(new_receiver_map)) {
645 return false;
646 }
647 }
648 receiver_maps->Add(new_receiver_map);
649 return true;
650 }
651
652
653 bool IC::UpdatePolymorphicIC(Handle<Name> name, Handle<Code> code) {
654 if (!code->is_handler()) return false;
655 if (target()->is_keyed_stub() && state() != PROTOTYPE_FAILURE) return false;
656 Handle<HeapType> type = receiver_type();
657 TypeHandleList types;
658 CodeHandleList handlers;
659
660 TargetTypes(&types);
661 int number_of_types = types.length();
662 int deprecated_types = 0;
663 int handler_to_overwrite = -1;
664
665 for (int i = 0; i < number_of_types; i++) {
666 Handle<HeapType> current_type = types.at(i);
667 if (current_type->IsClass() &&
668 current_type->AsClass()->Map()->is_deprecated()) {
669 // Filter out deprecated maps to ensure their instances get migrated.
670 ++deprecated_types;
671 } else if (type->NowIs(current_type)) {
672 // If the receiver type is already in the polymorphic IC, this indicates
673 // there was a prototoype chain failure. In that case, just overwrite the
674 // handler.
675 handler_to_overwrite = i;
676 } else if (handler_to_overwrite == -1 &&
677 current_type->IsClass() &&
678 type->IsClass() &&
679 IsTransitionOfMonomorphicTarget(*current_type->AsClass()->Map(),
680 *type->AsClass()->Map())) {
681 handler_to_overwrite = i;
682 }
683 }
684
685 int number_of_valid_types =
686 number_of_types - deprecated_types - (handler_to_overwrite != -1);
687
688 if (number_of_valid_types >= 4) return false;
689 if (number_of_types == 0) return false;
690 if (!target()->FindHandlers(&handlers, types.length())) return false;
691
692 number_of_valid_types++;
693 if (number_of_valid_types > 1 && target()->is_keyed_stub()) return false;
694 Handle<Code> ic;
695 if (number_of_valid_types == 1) {
696 ic = PropertyICCompiler::ComputeMonomorphic(kind(), name, type, code,
697 extra_ic_state());
698 } else {
699 if (handler_to_overwrite >= 0) {
700 handlers.Set(handler_to_overwrite, code);
701 if (!type->NowIs(types.at(handler_to_overwrite))) {
702 types.Set(handler_to_overwrite, type);
703 }
704 } else {
705 types.Add(type);
706 handlers.Add(code);
707 }
708 ic = PropertyICCompiler::ComputePolymorphic(kind(), &types, &handlers,
709 number_of_valid_types, name,
710 extra_ic_state());
711 }
712 set_target(*ic);
713 return true;
714 }
715
716
717 Handle<HeapType> IC::CurrentTypeOf(Handle<Object> object, Isolate* isolate) {
718 return object->IsJSGlobalObject()
719 ? HeapType::Constant(Handle<JSGlobalObject>::cast(object), isolate)
720 : HeapType::NowOf(object, isolate);
721 }
722
723
724 Handle<Map> IC::TypeToMap(HeapType* type, Isolate* isolate) {
725 if (type->Is(HeapType::Number()))
726 return isolate->factory()->heap_number_map();
727 if (type->Is(HeapType::Boolean())) return isolate->factory()->boolean_map();
728 if (type->IsConstant()) {
729 return handle(
730 Handle<JSGlobalObject>::cast(type->AsConstant()->Value())->map());
731 }
732 DCHECK(type->IsClass());
733 return type->AsClass()->Map();
734 }
735
736
737 template <class T>
738 typename T::TypeHandle IC::MapToType(Handle<Map> map,
739 typename T::Region* region) {
740 if (map->instance_type() == HEAP_NUMBER_TYPE) {
741 return T::Number(region);
742 } else if (map->instance_type() == ODDBALL_TYPE) {
743 // The only oddballs that can be recorded in ICs are booleans.
744 return T::Boolean(region);
745 } else {
746 return T::Class(map, region);
747 }
748 }
749
750
751 template
752 Type* IC::MapToType<Type>(Handle<Map> map, Zone* zone);
753
754
755 template
756 Handle<HeapType> IC::MapToType<HeapType>(Handle<Map> map, Isolate* region);
757
758
759 void IC::UpdateMonomorphicIC(Handle<Code> handler, Handle<Name> name) {
760 DCHECK(handler->is_handler());
761 Handle<Code> ic = PropertyICCompiler::ComputeMonomorphic(
762 kind(), name, receiver_type(), handler, extra_ic_state());
763 set_target(*ic);
764 }
765
766
767 void IC::CopyICToMegamorphicCache(Handle<Name> name) {
768 TypeHandleList types;
769 CodeHandleList handlers;
770 TargetTypes(&types);
771 if (!target()->FindHandlers(&handlers, types.length())) return;
772 for (int i = 0; i < types.length(); i++) {
773 UpdateMegamorphicCache(*types.at(i), *name, *handlers.at(i));
774 }
775 }
776
777
778 bool IC::IsTransitionOfMonomorphicTarget(Map* source_map, Map* target_map) {
779 if (source_map == NULL) return true;
780 if (target_map == NULL) return false;
781 ElementsKind target_elements_kind = target_map->elements_kind();
782 bool more_general_transition =
783 IsMoreGeneralElementsKindTransition(
784 source_map->elements_kind(), target_elements_kind);
785 Map* transitioned_map = more_general_transition
786 ? source_map->LookupElementsTransitionMap(target_elements_kind)
787 : NULL;
788
789 return transitioned_map == target_map;
790 }
791
792
793 void IC::PatchCache(Handle<Name> name, Handle<Code> code) {
794 switch (state()) {
795 case UNINITIALIZED:
796 case PREMONOMORPHIC:
797 UpdateMonomorphicIC(code, name);
798 break;
799 case PROTOTYPE_FAILURE:
800 case MONOMORPHIC:
801 case POLYMORPHIC:
802 if (!target()->is_keyed_stub() || state() == PROTOTYPE_FAILURE) {
803 if (UpdatePolymorphicIC(name, code)) break;
804 CopyICToMegamorphicCache(name);
805 }
806 set_target(*megamorphic_stub());
807 // Fall through.
808 case MEGAMORPHIC:
809 UpdateMegamorphicCache(*receiver_type(), *name, *code);
810 break;
811 case DEBUG_STUB:
812 break;
813 case DEFAULT:
814 case GENERIC:
815 UNREACHABLE();
816 break;
817 }
818 }
819
820
821 Handle<Code> LoadIC::initialize_stub(Isolate* isolate,
822 ExtraICState extra_state) {
823 return PropertyICCompiler::ComputeLoad(isolate, UNINITIALIZED, extra_state);
824 }
825
826
827 Handle<Code> LoadIC::megamorphic_stub() {
828 if (kind() == Code::LOAD_IC) {
829 return PropertyICCompiler::ComputeLoad(isolate(), MEGAMORPHIC,
830 extra_ic_state());
831 } else {
832 DCHECK_EQ(Code::KEYED_LOAD_IC, kind());
833 return KeyedLoadIC::generic_stub(isolate());
834 }
835 }
836
837
838 Handle<Code> LoadIC::pre_monomorphic_stub(Isolate* isolate,
839 ExtraICState extra_state) {
840 return PropertyICCompiler::ComputeLoad(isolate, PREMONOMORPHIC, extra_state);
841 }
842
843
844 Handle<Code> KeyedLoadIC::pre_monomorphic_stub(Isolate* isolate) {
845 return isolate->builtins()->KeyedLoadIC_PreMonomorphic();
846 }
847
848
849 Handle<Code> LoadIC::pre_monomorphic_stub() const {
850 if (kind() == Code::LOAD_IC) {
851 return LoadIC::pre_monomorphic_stub(isolate(), extra_ic_state());
852 } else {
853 DCHECK_EQ(Code::KEYED_LOAD_IC, kind());
854 return KeyedLoadIC::pre_monomorphic_stub(isolate());
855 }
856 }
857
858
859 Handle<Code> LoadIC::SimpleFieldLoad(FieldIndex index) {
860 LoadFieldStub stub(isolate(), index);
861 return stub.GetCode();
862 }
863
864
865 void LoadIC::UpdateCaches(LookupIterator* lookup) {
866 if (state() == UNINITIALIZED) {
867 // This is the first time we execute this inline cache. Set the target to
868 // the pre monomorphic stub to delay setting the monomorphic state.
869 set_target(*pre_monomorphic_stub());
870 TRACE_IC("LoadIC", lookup->name());
871 return;
872 }
873
874 Handle<Code> code;
875 if (lookup->state() == LookupIterator::JSPROXY ||
876 lookup->state() == LookupIterator::ACCESS_CHECK) {
877 code = slow_stub();
878 } else if (!lookup->IsFound()) {
879 if (kind() == Code::LOAD_IC) {
880 code = NamedLoadHandlerCompiler::ComputeLoadNonexistent(lookup->name(),
881 receiver_type());
882 // TODO(jkummerow/verwaest): Introduce a builtin that handles this case.
883 if (code.is_null()) code = slow_stub();
884 } else {
885 code = slow_stub();
886 }
887 } else {
888 code = ComputeHandler(lookup);
889 }
890
891 PatchCache(lookup->name(), code);
892 TRACE_IC("LoadIC", lookup->name());
893 }
894
895
896 void IC::UpdateMegamorphicCache(HeapType* type, Name* name, Code* code) {
897 if (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC) return;
898 Map* map = *TypeToMap(type, isolate());
899 isolate()->stub_cache()->Set(name, map, code);
900 }
901
902
903 Handle<Code> IC::ComputeHandler(LookupIterator* lookup, Handle<Object> value) {
904 bool receiver_is_holder =
905 lookup->GetReceiver().is_identical_to(lookup->GetHolder<JSObject>());
906 CacheHolderFlag flag;
907 Handle<Map> stub_holder_map = IC::GetHandlerCacheHolder(
908 *receiver_type(), receiver_is_holder, isolate(), &flag);
909
910 Handle<Code> code = PropertyHandlerCompiler::Find(
911 lookup->name(), stub_holder_map, kind(), flag,
912 lookup->holder_map()->is_dictionary_map() ? Code::NORMAL : Code::FAST);
913 // Use the cached value if it exists, and if it is different from the
914 // handler that just missed.
915 if (!code.is_null()) {
916 if (!maybe_handler_.is_null() &&
917 !maybe_handler_.ToHandleChecked().is_identical_to(code)) {
918 return code;
919 }
920 if (maybe_handler_.is_null()) {
921 // maybe_handler_ is only populated for MONOMORPHIC and POLYMORPHIC ICs.
922 // In MEGAMORPHIC case, check if the handler in the megamorphic stub
923 // cache (which just missed) is different from the cached handler.
924 if (state() == MEGAMORPHIC && lookup->GetReceiver()->IsHeapObject()) {
925 Map* map = Handle<HeapObject>::cast(lookup->GetReceiver())->map();
926 Code* megamorphic_cached_code =
927 isolate()->stub_cache()->Get(*lookup->name(), map, code->flags());
928 if (megamorphic_cached_code != *code) return code;
929 } else {
930 return code;
931 }
932 }
933 }
934
935 code = CompileHandler(lookup, value, flag);
936 DCHECK(code->is_handler());
937
938 if (code->type() != Code::NORMAL) {
939 Map::UpdateCodeCache(stub_holder_map, lookup->name(), code);
940 }
941
942 return code;
943 }
944
945
946 Handle<Code> LoadIC::CompileHandler(LookupIterator* lookup,
947 Handle<Object> unused,
948 CacheHolderFlag cache_holder) {
949 Handle<Object> receiver = lookup->GetReceiver();
950 if (receiver->IsString() &&
951 Name::Equals(isolate()->factory()->length_string(), lookup->name())) {
952 FieldIndex index = FieldIndex::ForInObjectOffset(String::kLengthOffset);
953 return SimpleFieldLoad(index);
954 }
955
956 if (receiver->IsStringWrapper() &&
957 Name::Equals(isolate()->factory()->length_string(), lookup->name())) {
958 StringLengthStub string_length_stub(isolate());
959 return string_length_stub.GetCode();
960 }
961
962 // Use specialized code for getting prototype of functions.
963 if (receiver->IsJSFunction() &&
964 Name::Equals(isolate()->factory()->prototype_string(), lookup->name()) &&
965 Handle<JSFunction>::cast(receiver)->should_have_prototype() &&
966 !Handle<JSFunction>::cast(receiver)
967 ->map()
968 ->has_non_instance_prototype()) {
969 Handle<Code> stub;
970 FunctionPrototypeStub function_prototype_stub(isolate());
971 return function_prototype_stub.GetCode();
972 }
973
974 Handle<HeapType> type = receiver_type();
975 Handle<JSObject> holder = lookup->GetHolder<JSObject>();
976 bool receiver_is_holder = receiver.is_identical_to(holder);
977 // -------------- Interceptors --------------
978 if (lookup->state() == LookupIterator::INTERCEPTOR) {
979 DCHECK(!holder->GetNamedInterceptor()->getter()->IsUndefined());
980 NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder,
981 cache_holder);
982 // Perform a lookup behind the interceptor. Copy the LookupIterator since
983 // the original iterator will be used to fetch the value.
984 LookupIterator it(lookup);
985 it.Next();
986 LookupForRead(&it);
987 return compiler.CompileLoadInterceptor(&it);
988 }
989
990 // -------------- Accessors --------------
991 DCHECK(lookup->state() == LookupIterator::PROPERTY);
992 if (lookup->property_kind() == LookupIterator::ACCESSOR) {
993 // Use simple field loads for some well-known callback properties.
994 if (receiver_is_holder) {
995 DCHECK(receiver->IsJSObject());
996 Handle<JSObject> js_receiver = Handle<JSObject>::cast(receiver);
997 int object_offset;
998 if (Accessors::IsJSObjectFieldAccessor<HeapType>(type, lookup->name(),
999 &object_offset)) {
1000 FieldIndex index =
1001 FieldIndex::ForInObjectOffset(object_offset, js_receiver->map());
1002 return SimpleFieldLoad(index);
1003 }
1004 }
1005
1006 Handle<Object> accessors = lookup->GetAccessors();
1007 if (accessors->IsExecutableAccessorInfo()) {
1008 Handle<ExecutableAccessorInfo> info =
1009 Handle<ExecutableAccessorInfo>::cast(accessors);
1010 if (v8::ToCData<Address>(info->getter()) == 0) return slow_stub();
1011 if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info,
1012 type)) {
1013 return slow_stub();
1014 }
1015 if (!holder->HasFastProperties()) return slow_stub();
1016 NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder,
1017 cache_holder);
1018 return compiler.CompileLoadCallback(lookup->name(), info);
1019 }
1020 if (accessors->IsAccessorPair()) {
1021 Handle<Object> getter(Handle<AccessorPair>::cast(accessors)->getter(),
1022 isolate());
1023 if (!getter->IsJSFunction()) return slow_stub();
1024 if (!holder->HasFastProperties()) return slow_stub();
1025 Handle<JSFunction> function = Handle<JSFunction>::cast(getter);
1026 if (!receiver->IsJSObject() && !function->IsBuiltin() &&
1027 function->shared()->strict_mode() == SLOPPY) {
1028 // Calling sloppy non-builtins with a value as the receiver
1029 // requires boxing.
1030 return slow_stub();
1031 }
1032 CallOptimization call_optimization(function);
1033 NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder,
1034 cache_holder);
1035 if (call_optimization.is_simple_api_call() &&
1036 call_optimization.IsCompatibleReceiver(receiver, holder)) {
1037 return compiler.CompileLoadCallback(lookup->name(), call_optimization);
1038 }
1039 return compiler.CompileLoadViaGetter(lookup->name(), function);
1040 }
1041 // TODO(dcarney): Handle correctly.
1042 DCHECK(accessors->IsDeclaredAccessorInfo());
1043 return slow_stub();
1044 }
1045
1046 // -------------- Dictionary properties --------------
1047 DCHECK(lookup->property_kind() == LookupIterator::DATA);
1048 if (lookup->property_encoding() == LookupIterator::DICTIONARY) {
1049 if (kind() != Code::LOAD_IC) return slow_stub();
1050 if (holder->IsGlobalObject()) {
1051 NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder,
1052 cache_holder);
1053 Handle<PropertyCell> cell = lookup->GetPropertyCell();
1054 Handle<Code> code = compiler.CompileLoadGlobal(cell, lookup->name(),
1055 lookup->IsConfigurable());
1056 // TODO(verwaest): Move caching of these NORMAL stubs outside as well.
1057 CacheHolderFlag flag;
1058 Handle<Map> stub_holder_map =
1059 GetHandlerCacheHolder(*type, receiver_is_holder, isolate(), &flag);
1060 Map::UpdateCodeCache(stub_holder_map, lookup->name(), code);
1061 return code;
1062 }
1063 // There is only one shared stub for loading normalized
1064 // properties. It does not traverse the prototype chain, so the
1065 // property must be found in the object for the stub to be
1066 // applicable.
1067 if (!receiver_is_holder) return slow_stub();
1068 return isolate()->builtins()->LoadIC_Normal();
1069 }
1070
1071 // -------------- Fields --------------
1072 DCHECK(lookup->property_encoding() == LookupIterator::DESCRIPTOR);
1073 if (lookup->property_details().type() == FIELD) {
1074 FieldIndex field = lookup->GetFieldIndex();
1075 if (receiver_is_holder) {
1076 return SimpleFieldLoad(field);
1077 }
1078 NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder,
1079 cache_holder);
1080 return compiler.CompileLoadField(lookup->name(), field);
1081 }
1082
1083 // -------------- Constant properties --------------
1084 DCHECK(lookup->property_details().type() == CONSTANT);
1085 if (receiver_is_holder) {
1086 LoadConstantStub stub(isolate(), lookup->GetConstantIndex());
1087 return stub.GetCode();
1088 }
1089 NamedLoadHandlerCompiler compiler(isolate(), receiver_type(), holder,
1090 cache_holder);
1091 return compiler.CompileLoadConstant(lookup->name(),
1092 lookup->GetConstantIndex());
1093 }
1094
1095
1096 static Handle<Object> TryConvertKey(Handle<Object> key, Isolate* isolate) {
1097 // This helper implements a few common fast cases for converting
1098 // non-smi keys of keyed loads/stores to a smi or a string.
1099 if (key->IsHeapNumber()) {
1100 double value = Handle<HeapNumber>::cast(key)->value();
1101 if (std::isnan(value)) {
1102 key = isolate->factory()->nan_string();
1103 } else {
1104 int int_value = FastD2I(value);
1105 if (value == int_value && Smi::IsValid(int_value)) {
1106 key = Handle<Smi>(Smi::FromInt(int_value), isolate);
1107 }
1108 }
1109 } else if (key->IsUndefined()) {
1110 key = isolate->factory()->undefined_string();
1111 }
1112 return key;
1113 }
1114
1115
1116 Handle<Code> KeyedLoadIC::LoadElementStub(Handle<JSObject> receiver) {
1117 // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS
1118 // via megamorphic stubs, since they don't have a map in their relocation info
1119 // and so the stubs can't be harvested for the object needed for a map check.
1120 if (target()->type() != Code::NORMAL) {
1121 TRACE_GENERIC_IC(isolate(), "KeyedIC", "non-NORMAL target type");
1122 return generic_stub();
1123 }
1124
1125 Handle<Map> receiver_map(receiver->map(), isolate());
1126 MapHandleList target_receiver_maps;
1127 if (target().is_identical_to(string_stub())) {
1128 target_receiver_maps.Add(isolate()->factory()->string_map());
1129 } else {
1130 TargetMaps(&target_receiver_maps);
1131 }
1132 if (target_receiver_maps.length() == 0) {
1133 return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map);
1134 }
1135
1136 // The first time a receiver is seen that is a transitioned version of the
1137 // previous monomorphic receiver type, assume the new ElementsKind is the
1138 // monomorphic type. This benefits global arrays that only transition
1139 // once, and all call sites accessing them are faster if they remain
1140 // monomorphic. If this optimistic assumption is not true, the IC will
1141 // miss again and it will become polymorphic and support both the
1142 // untransitioned and transitioned maps.
1143 if (state() == MONOMORPHIC &&
1144 IsMoreGeneralElementsKindTransition(
1145 target_receiver_maps.at(0)->elements_kind(),
1146 receiver->GetElementsKind())) {
1147 return PropertyICCompiler::ComputeKeyedLoadMonomorphic(receiver_map);
1148 }
1149
1150 DCHECK(state() != GENERIC);
1151
1152 // Determine the list of receiver maps that this call site has seen,
1153 // adding the map that was just encountered.
1154 if (!AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map)) {
1155 // If the miss wasn't due to an unseen map, a polymorphic stub
1156 // won't help, use the generic stub.
1157 TRACE_GENERIC_IC(isolate(), "KeyedIC", "same map added twice");
1158 return generic_stub();
1159 }
1160
1161 // If the maximum number of receiver maps has been exceeded, use the generic
1162 // version of the IC.
1163 if (target_receiver_maps.length() > kMaxKeyedPolymorphism) {
1164 TRACE_GENERIC_IC(isolate(), "KeyedIC", "max polymorph exceeded");
1165 return generic_stub();
1166 }
1167
1168 return PropertyICCompiler::ComputeKeyedLoadPolymorphic(&target_receiver_maps);
1169 }
1170
1171
1172 MaybeHandle<Object> KeyedLoadIC::Load(Handle<Object> object,
1173 Handle<Object> key) {
1174 if (MigrateDeprecated(object)) {
1175 Handle<Object> result;
1176 ASSIGN_RETURN_ON_EXCEPTION(
1177 isolate(),
1178 result,
1179 Runtime::GetObjectProperty(isolate(), object, key),
1180 Object);
1181 return result;
1182 }
1183
1184 Handle<Object> load_handle;
1185 Handle<Code> stub = generic_stub();
1186
1187 // Check for non-string values that can be converted into an
1188 // internalized string directly or is representable as a smi.
1189 key = TryConvertKey(key, isolate());
1190
1191 if (key->IsInternalizedString() || key->IsSymbol()) {
1192 ASSIGN_RETURN_ON_EXCEPTION(
1193 isolate(),
1194 load_handle,
1195 LoadIC::Load(object, Handle<Name>::cast(key)),
1196 Object);
1197 } else if (FLAG_use_ic && !object->IsAccessCheckNeeded()) {
1198 if (object->IsString() && key->IsNumber()) {
1199 if (state() == UNINITIALIZED) stub = string_stub();
1200 } else if (object->IsJSObject()) {
1201 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1202 if (receiver->elements()->map() ==
1203 isolate()->heap()->sloppy_arguments_elements_map()) {
1204 stub = sloppy_arguments_stub();
1205 } else if (receiver->HasIndexedInterceptor()) {
1206 stub = indexed_interceptor_stub();
1207 } else if (!Object::ToSmi(isolate(), key).is_null() &&
1208 (!target().is_identical_to(sloppy_arguments_stub()))) {
1209 stub = LoadElementStub(receiver);
1210 }
1211 }
1212 }
1213
1214 if (!is_target_set()) {
1215 Code* generic = *generic_stub();
1216 if (*stub == generic) {
1217 TRACE_GENERIC_IC(isolate(), "KeyedLoadIC", "set generic");
1218 }
1219 set_target(*stub);
1220 TRACE_IC("LoadIC", key);
1221 }
1222
1223 if (!load_handle.is_null()) return load_handle;
1224 Handle<Object> result;
1225 ASSIGN_RETURN_ON_EXCEPTION(
1226 isolate(),
1227 result,
1228 Runtime::GetObjectProperty(isolate(), object, key),
1229 Object);
1230 return result;
1231 }
1232
1233
1234 bool StoreIC::LookupForWrite(LookupIterator* it, Handle<Object> value,
1235 JSReceiver::StoreFromKeyed store_mode) {
1236 // Disable ICs for non-JSObjects for now.
1237 if (!it->GetReceiver()->IsJSObject()) return false;
1238 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
1239 DCHECK(!receiver->map()->is_deprecated());
1240
1241 for (; it->IsFound(); it->Next()) {
1242 switch (it->state()) {
1243 case LookupIterator::NOT_FOUND:
1244 case LookupIterator::TRANSITION:
1245 UNREACHABLE();
1246 case LookupIterator::JSPROXY:
1247 return false;
1248 case LookupIterator::INTERCEPTOR: {
1249 Handle<JSObject> holder = it->GetHolder<JSObject>();
1250 InterceptorInfo* info = holder->GetNamedInterceptor();
1251 if (it->HolderIsReceiverOrHiddenPrototype()) {
1252 if (!info->setter()->IsUndefined()) return true;
1253 } else if (!info->getter()->IsUndefined() ||
1254 !info->query()->IsUndefined()) {
1255 return false;
1256 }
1257 break;
1258 }
1259 case LookupIterator::ACCESS_CHECK:
1260 if (it->GetHolder<JSObject>()->IsAccessCheckNeeded()) return false;
1261 break;
1262 case LookupIterator::PROPERTY:
1263 if (!it->HasProperty()) break;
1264 if (it->IsReadOnly()) return false;
1265 if (it->property_kind() == LookupIterator::ACCESSOR) return true;
1266 if (it->GetHolder<JSObject>().is_identical_to(receiver)) {
1267 it->PrepareForDataProperty(value);
1268 // The previous receiver map might just have been deprecated,
1269 // so reload it.
1270 update_receiver_type(receiver);
1271 return true;
1272 }
1273
1274 // Receiver != holder.
1275 if (receiver->IsJSGlobalProxy()) {
1276 PrototypeIterator iter(it->isolate(), receiver);
1277 return it->GetHolder<Object>().is_identical_to(
1278 PrototypeIterator::GetCurrent(iter));
1279 }
1280
1281 it->PrepareTransitionToDataProperty(value, NONE, store_mode);
1282 return it->IsCacheableTransition();
1283 }
1284 }
1285
1286 it->PrepareTransitionToDataProperty(value, NONE, store_mode);
1287 return it->IsCacheableTransition();
1288 }
1289
1290
1291 MaybeHandle<Object> StoreIC::Store(Handle<Object> object,
1292 Handle<Name> name,
1293 Handle<Object> value,
1294 JSReceiver::StoreFromKeyed store_mode) {
1295 // TODO(verwaest): Let SetProperty do the migration, since storing a property
1296 // might deprecate the current map again, if value does not fit.
1297 if (MigrateDeprecated(object) || object->IsJSProxy()) {
1298 Handle<Object> result;
1299 ASSIGN_RETURN_ON_EXCEPTION(
1300 isolate(), result,
1301 Object::SetProperty(object, name, value, strict_mode()), Object);
1302 return result;
1303 }
1304
1305 // If the object is undefined or null it's illegal to try to set any
1306 // properties on it; throw a TypeError in that case.
1307 if (object->IsUndefined() || object->IsNull()) {
1308 return TypeError("non_object_property_store", object, name);
1309 }
1310
1311 // Check if the given name is an array index.
1312 uint32_t index;
1313 if (name->AsArrayIndex(&index)) {
1314 // Ignore other stores where the receiver is not a JSObject.
1315 // TODO(1475): Must check prototype chains of object wrappers.
1316 if (!object->IsJSObject()) return value;
1317 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1318
1319 Handle<Object> result;
1320 ASSIGN_RETURN_ON_EXCEPTION(
1321 isolate(),
1322 result,
1323 JSObject::SetElement(receiver, index, value, NONE, strict_mode()),
1324 Object);
1325 return value;
1326 }
1327
1328 // Observed objects are always modified through the runtime.
1329 if (object->IsHeapObject() &&
1330 Handle<HeapObject>::cast(object)->map()->is_observed()) {
1331 Handle<Object> result;
1332 ASSIGN_RETURN_ON_EXCEPTION(
1333 isolate(), result,
1334 Object::SetProperty(object, name, value, strict_mode(), store_mode),
1335 Object);
1336 return result;
1337 }
1338
1339 LookupIterator it(object, name);
1340 if (FLAG_use_ic) UpdateCaches(&it, value, store_mode);
1341
1342 // Set the property.
1343 Handle<Object> result;
1344 ASSIGN_RETURN_ON_EXCEPTION(
1345 isolate(), result,
1346 Object::SetProperty(&it, value, strict_mode(), store_mode), Object);
1347 return result;
1348 }
1349
1350
1351 OStream& operator<<(OStream& os, const CallIC::State& s) {
1352 return os << "(args(" << s.arg_count() << "), "
1353 << (s.call_type() == CallIC::METHOD ? "METHOD" : "FUNCTION")
1354 << ", ";
1355 }
1356
1357
1358 Handle<Code> CallIC::initialize_stub(Isolate* isolate,
1359 int argc,
1360 CallType call_type) {
1361 CallICStub stub(isolate, State(argc, call_type));
1362 Handle<Code> code = stub.GetCode();
1363 return code;
1364 }
1365
1366
1367 Handle<Code> StoreIC::initialize_stub(Isolate* isolate,
1368 StrictMode strict_mode) {
1369 ExtraICState extra_state = ComputeExtraICState(strict_mode);
1370 Handle<Code> ic =
1371 PropertyICCompiler::ComputeStore(isolate, UNINITIALIZED, extra_state);
1372 return ic;
1373 }
1374
1375
1376 Handle<Code> StoreIC::megamorphic_stub() {
1377 return PropertyICCompiler::ComputeStore(isolate(), MEGAMORPHIC,
1378 extra_ic_state());
1379 }
1380
1381
1382 Handle<Code> StoreIC::generic_stub() const {
1383 return PropertyICCompiler::ComputeStore(isolate(), GENERIC, extra_ic_state());
1384 }
1385
1386
1387 Handle<Code> StoreIC::pre_monomorphic_stub(Isolate* isolate,
1388 StrictMode strict_mode) {
1389 ExtraICState state = ComputeExtraICState(strict_mode);
1390 return PropertyICCompiler::ComputeStore(isolate, PREMONOMORPHIC, state);
1391 }
1392
1393
1394 void StoreIC::UpdateCaches(LookupIterator* lookup, Handle<Object> value,
1395 JSReceiver::StoreFromKeyed store_mode) {
1396 if (state() == UNINITIALIZED) {
1397 // This is the first time we execute this inline cache. Set the target to
1398 // the pre monomorphic stub to delay setting the monomorphic state.
1399 set_target(*pre_monomorphic_stub());
1400 TRACE_IC("StoreIC", lookup->name());
1401 return;
1402 }
1403
1404 Handle<Code> code = LookupForWrite(lookup, value, store_mode)
1405 ? ComputeHandler(lookup, value)
1406 : slow_stub();
1407
1408 PatchCache(lookup->name(), code);
1409 TRACE_IC("StoreIC", lookup->name());
1410 }
1411
1412
1413 Handle<Code> StoreIC::CompileHandler(LookupIterator* lookup,
1414 Handle<Object> value,
1415 CacheHolderFlag cache_holder) {
1416 DCHECK_NE(LookupIterator::JSPROXY, lookup->state());
1417
1418 // This is currently guaranteed by checks in StoreIC::Store.
1419 Handle<JSObject> receiver = Handle<JSObject>::cast(lookup->GetReceiver());
1420 Handle<JSObject> holder = lookup->GetHolder<JSObject>();
1421 DCHECK(!receiver->IsAccessCheckNeeded());
1422
1423 // -------------- Transition --------------
1424 if (lookup->state() == LookupIterator::TRANSITION) {
1425 Handle<Map> transition = lookup->transition_map();
1426 // Currently not handled by CompileStoreTransition.
1427 if (!holder->HasFastProperties()) return slow_stub();
1428
1429 DCHECK(lookup->IsCacheableTransition());
1430 NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), holder);
1431 return compiler.CompileStoreTransition(transition, lookup->name());
1432 }
1433
1434 // -------------- Interceptors --------------
1435 if (lookup->state() == LookupIterator::INTERCEPTOR) {
1436 DCHECK(!holder->GetNamedInterceptor()->setter()->IsUndefined());
1437 NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), holder);
1438 return compiler.CompileStoreInterceptor(lookup->name());
1439 }
1440
1441 // -------------- Accessors --------------
1442 DCHECK(lookup->state() == LookupIterator::PROPERTY);
1443 if (lookup->property_kind() == LookupIterator::ACCESSOR) {
1444 if (!holder->HasFastProperties()) return slow_stub();
1445 Handle<Object> accessors = lookup->GetAccessors();
1446 if (accessors->IsExecutableAccessorInfo()) {
1447 Handle<ExecutableAccessorInfo> info =
1448 Handle<ExecutableAccessorInfo>::cast(accessors);
1449 if (v8::ToCData<Address>(info->setter()) == 0) return slow_stub();
1450 if (!ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), info,
1451 receiver_type())) {
1452 return slow_stub();
1453 }
1454 NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), holder);
1455 return compiler.CompileStoreCallback(receiver, lookup->name(), info);
1456 } else if (accessors->IsAccessorPair()) {
1457 Handle<Object> setter(Handle<AccessorPair>::cast(accessors)->setter(),
1458 isolate());
1459 if (!setter->IsJSFunction()) return slow_stub();
1460 Handle<JSFunction> function = Handle<JSFunction>::cast(setter);
1461 CallOptimization call_optimization(function);
1462 NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), holder);
1463 if (call_optimization.is_simple_api_call() &&
1464 call_optimization.IsCompatibleReceiver(receiver, holder)) {
1465 return compiler.CompileStoreCallback(receiver, lookup->name(),
1466 call_optimization);
1467 }
1468 return compiler.CompileStoreViaSetter(receiver, lookup->name(),
1469 Handle<JSFunction>::cast(setter));
1470 }
1471 // TODO(dcarney): Handle correctly.
1472 DCHECK(accessors->IsDeclaredAccessorInfo());
1473 return slow_stub();
1474 }
1475
1476 // -------------- Dictionary properties --------------
1477 DCHECK(lookup->property_kind() == LookupIterator::DATA);
1478 if (lookup->property_encoding() == LookupIterator::DICTIONARY) {
1479 if (holder->IsGlobalObject()) {
1480 Handle<PropertyCell> cell = lookup->GetPropertyCell();
1481 Handle<HeapType> union_type = PropertyCell::UpdatedType(cell, value);
1482 StoreGlobalStub stub(isolate(), union_type->IsConstant(),
1483 receiver->IsJSGlobalProxy());
1484 Handle<Code> code = stub.GetCodeCopyFromTemplate(
1485 Handle<GlobalObject>::cast(holder), cell);
1486 // TODO(verwaest): Move caching of these NORMAL stubs outside as well.
1487 HeapObject::UpdateMapCodeCache(receiver, lookup->name(), code);
1488 return code;
1489 }
1490 DCHECK(holder.is_identical_to(receiver));
1491 return isolate()->builtins()->StoreIC_Normal();
1492 }
1493
1494 // -------------- Fields --------------
1495 DCHECK(lookup->property_encoding() == LookupIterator::DESCRIPTOR);
1496 if (lookup->property_details().type() == FIELD) {
1497 bool use_stub = true;
1498 if (lookup->representation().IsHeapObject()) {
1499 // Only use a generic stub if no types need to be tracked.
1500 Handle<HeapType> field_type = lookup->GetFieldType();
1501 HeapType::Iterator<Map> it = field_type->Classes();
1502 use_stub = it.Done();
1503 }
1504 if (use_stub) {
1505 StoreFieldStub stub(isolate(), lookup->GetFieldIndex(),
1506 lookup->representation());
1507 return stub.GetCode();
1508 }
1509 NamedStoreHandlerCompiler compiler(isolate(), receiver_type(), holder);
1510 return compiler.CompileStoreField(lookup);
1511 }
1512
1513 // -------------- Constant properties --------------
1514 DCHECK(lookup->property_details().type() == CONSTANT);
1515 return slow_stub();
1516 }
1517
1518
1519 Handle<Code> KeyedStoreIC::StoreElementStub(Handle<JSObject> receiver,
1520 KeyedAccessStoreMode store_mode) {
1521 // Don't handle megamorphic property accesses for INTERCEPTORS or CALLBACKS
1522 // via megamorphic stubs, since they don't have a map in their relocation info
1523 // and so the stubs can't be harvested for the object needed for a map check.
1524 if (target()->type() != Code::NORMAL) {
1525 TRACE_GENERIC_IC(isolate(), "KeyedIC", "non-NORMAL target type");
1526 return generic_stub();
1527 }
1528
1529 Handle<Map> receiver_map(receiver->map(), isolate());
1530 MapHandleList target_receiver_maps;
1531 TargetMaps(&target_receiver_maps);
1532 if (target_receiver_maps.length() == 0) {
1533 Handle<Map> monomorphic_map =
1534 ComputeTransitionedMap(receiver_map, store_mode);
1535 store_mode = GetNonTransitioningStoreMode(store_mode);
1536 return PropertyICCompiler::ComputeKeyedStoreMonomorphic(
1537 monomorphic_map, strict_mode(), store_mode);
1538 }
1539
1540 // There are several special cases where an IC that is MONOMORPHIC can still
1541 // transition to a different GetNonTransitioningStoreMode IC that handles a
1542 // superset of the original IC. Handle those here if the receiver map hasn't
1543 // changed or it has transitioned to a more general kind.
1544 KeyedAccessStoreMode old_store_mode =
1545 KeyedStoreIC::GetKeyedAccessStoreMode(target()->extra_ic_state());
1546 Handle<Map> previous_receiver_map = target_receiver_maps.at(0);
1547 if (state() == MONOMORPHIC) {
1548 Handle<Map> transitioned_receiver_map = receiver_map;
1549 if (IsTransitionStoreMode(store_mode)) {
1550 transitioned_receiver_map =
1551 ComputeTransitionedMap(receiver_map, store_mode);
1552 }
1553 if ((receiver_map.is_identical_to(previous_receiver_map) &&
1554 IsTransitionStoreMode(store_mode)) ||
1555 IsTransitionOfMonomorphicTarget(*previous_receiver_map,
1556 *transitioned_receiver_map)) {
1557 // If the "old" and "new" maps are in the same elements map family, or
1558 // if they at least come from the same origin for a transitioning store,
1559 // stay MONOMORPHIC and use the map for the most generic ElementsKind.
1560 store_mode = GetNonTransitioningStoreMode(store_mode);
1561 return PropertyICCompiler::ComputeKeyedStoreMonomorphic(
1562 transitioned_receiver_map, strict_mode(), store_mode);
1563 } else if (*previous_receiver_map == receiver->map() &&
1564 old_store_mode == STANDARD_STORE &&
1565 (store_mode == STORE_AND_GROW_NO_TRANSITION ||
1566 store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS ||
1567 store_mode == STORE_NO_TRANSITION_HANDLE_COW)) {
1568 // A "normal" IC that handles stores can switch to a version that can
1569 // grow at the end of the array, handle OOB accesses or copy COW arrays
1570 // and still stay MONOMORPHIC.
1571 return PropertyICCompiler::ComputeKeyedStoreMonomorphic(
1572 receiver_map, strict_mode(), store_mode);
1573 }
1574 }
1575
1576 DCHECK(state() != GENERIC);
1577
1578 bool map_added =
1579 AddOneReceiverMapIfMissing(&target_receiver_maps, receiver_map);
1580
1581 if (IsTransitionStoreMode(store_mode)) {
1582 Handle<Map> transitioned_receiver_map =
1583 ComputeTransitionedMap(receiver_map, store_mode);
1584 map_added |= AddOneReceiverMapIfMissing(&target_receiver_maps,
1585 transitioned_receiver_map);
1586 }
1587
1588 if (!map_added) {
1589 // If the miss wasn't due to an unseen map, a polymorphic stub
1590 // won't help, use the generic stub.
1591 TRACE_GENERIC_IC(isolate(), "KeyedIC", "same map added twice");
1592 return generic_stub();
1593 }
1594
1595 // If the maximum number of receiver maps has been exceeded, use the generic
1596 // version of the IC.
1597 if (target_receiver_maps.length() > kMaxKeyedPolymorphism) {
1598 TRACE_GENERIC_IC(isolate(), "KeyedIC", "max polymorph exceeded");
1599 return generic_stub();
1600 }
1601
1602 // Make sure all polymorphic handlers have the same store mode, otherwise the
1603 // generic stub must be used.
1604 store_mode = GetNonTransitioningStoreMode(store_mode);
1605 if (old_store_mode != STANDARD_STORE) {
1606 if (store_mode == STANDARD_STORE) {
1607 store_mode = old_store_mode;
1608 } else if (store_mode != old_store_mode) {
1609 TRACE_GENERIC_IC(isolate(), "KeyedIC", "store mode mismatch");
1610 return generic_stub();
1611 }
1612 }
1613
1614 // If the store mode isn't the standard mode, make sure that all polymorphic
1615 // receivers are either external arrays, or all "normal" arrays. Otherwise,
1616 // use the generic stub.
1617 if (store_mode != STANDARD_STORE) {
1618 int external_arrays = 0;
1619 for (int i = 0; i < target_receiver_maps.length(); ++i) {
1620 if (target_receiver_maps[i]->has_external_array_elements() ||
1621 target_receiver_maps[i]->has_fixed_typed_array_elements()) {
1622 external_arrays++;
1623 }
1624 }
1625 if (external_arrays != 0 &&
1626 external_arrays != target_receiver_maps.length()) {
1627 TRACE_GENERIC_IC(isolate(), "KeyedIC",
1628 "unsupported combination of external and normal arrays");
1629 return generic_stub();
1630 }
1631 }
1632
1633 return PropertyICCompiler::ComputeKeyedStorePolymorphic(
1634 &target_receiver_maps, store_mode, strict_mode());
1635 }
1636
1637
1638 Handle<Map> KeyedStoreIC::ComputeTransitionedMap(
1639 Handle<Map> map,
1640 KeyedAccessStoreMode store_mode) {
1641 switch (store_mode) {
1642 case STORE_TRANSITION_SMI_TO_OBJECT:
1643 case STORE_TRANSITION_DOUBLE_TO_OBJECT:
1644 case STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT:
1645 case STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT:
1646 return Map::TransitionElementsTo(map, FAST_ELEMENTS);
1647 case STORE_TRANSITION_SMI_TO_DOUBLE:
1648 case STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE:
1649 return Map::TransitionElementsTo(map, FAST_DOUBLE_ELEMENTS);
1650 case STORE_TRANSITION_HOLEY_SMI_TO_OBJECT:
1651 case STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT:
1652 case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT:
1653 case STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT:
1654 return Map::TransitionElementsTo(map, FAST_HOLEY_ELEMENTS);
1655 case STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE:
1656 case STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE:
1657 return Map::TransitionElementsTo(map, FAST_HOLEY_DOUBLE_ELEMENTS);
1658 case STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS:
1659 DCHECK(map->has_external_array_elements());
1660 // Fall through
1661 case STORE_NO_TRANSITION_HANDLE_COW:
1662 case STANDARD_STORE:
1663 case STORE_AND_GROW_NO_TRANSITION:
1664 return map;
1665 }
1666 UNREACHABLE();
1667 return MaybeHandle<Map>().ToHandleChecked();
1668 }
1669
1670
1671 bool IsOutOfBoundsAccess(Handle<JSObject> receiver,
1672 int index) {
1673 if (receiver->IsJSArray()) {
1674 return JSArray::cast(*receiver)->length()->IsSmi() &&
1675 index >= Smi::cast(JSArray::cast(*receiver)->length())->value();
1676 }
1677 return index >= receiver->elements()->length();
1678 }
1679
1680
1681 KeyedAccessStoreMode KeyedStoreIC::GetStoreMode(Handle<JSObject> receiver,
1682 Handle<Object> key,
1683 Handle<Object> value) {
1684 Handle<Smi> smi_key = Object::ToSmi(isolate(), key).ToHandleChecked();
1685 int index = smi_key->value();
1686 bool oob_access = IsOutOfBoundsAccess(receiver, index);
1687 // Don't consider this a growing store if the store would send the receiver to
1688 // dictionary mode.
1689 bool allow_growth = receiver->IsJSArray() && oob_access &&
1690 !receiver->WouldConvertToSlowElements(key);
1691 if (allow_growth) {
1692 // Handle growing array in stub if necessary.
1693 if (receiver->HasFastSmiElements()) {
1694 if (value->IsHeapNumber()) {
1695 if (receiver->HasFastHoleyElements()) {
1696 return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_DOUBLE;
1697 } else {
1698 return STORE_AND_GROW_TRANSITION_SMI_TO_DOUBLE;
1699 }
1700 }
1701 if (value->IsHeapObject()) {
1702 if (receiver->HasFastHoleyElements()) {
1703 return STORE_AND_GROW_TRANSITION_HOLEY_SMI_TO_OBJECT;
1704 } else {
1705 return STORE_AND_GROW_TRANSITION_SMI_TO_OBJECT;
1706 }
1707 }
1708 } else if (receiver->HasFastDoubleElements()) {
1709 if (!value->IsSmi() && !value->IsHeapNumber()) {
1710 if (receiver->HasFastHoleyElements()) {
1711 return STORE_AND_GROW_TRANSITION_HOLEY_DOUBLE_TO_OBJECT;
1712 } else {
1713 return STORE_AND_GROW_TRANSITION_DOUBLE_TO_OBJECT;
1714 }
1715 }
1716 }
1717 return STORE_AND_GROW_NO_TRANSITION;
1718 } else {
1719 // Handle only in-bounds elements accesses.
1720 if (receiver->HasFastSmiElements()) {
1721 if (value->IsHeapNumber()) {
1722 if (receiver->HasFastHoleyElements()) {
1723 return STORE_TRANSITION_HOLEY_SMI_TO_DOUBLE;
1724 } else {
1725 return STORE_TRANSITION_SMI_TO_DOUBLE;
1726 }
1727 } else if (value->IsHeapObject()) {
1728 if (receiver->HasFastHoleyElements()) {
1729 return STORE_TRANSITION_HOLEY_SMI_TO_OBJECT;
1730 } else {
1731 return STORE_TRANSITION_SMI_TO_OBJECT;
1732 }
1733 }
1734 } else if (receiver->HasFastDoubleElements()) {
1735 if (!value->IsSmi() && !value->IsHeapNumber()) {
1736 if (receiver->HasFastHoleyElements()) {
1737 return STORE_TRANSITION_HOLEY_DOUBLE_TO_OBJECT;
1738 } else {
1739 return STORE_TRANSITION_DOUBLE_TO_OBJECT;
1740 }
1741 }
1742 }
1743 if (!FLAG_trace_external_array_abuse &&
1744 receiver->map()->has_external_array_elements() && oob_access) {
1745 return STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS;
1746 }
1747 Heap* heap = receiver->GetHeap();
1748 if (receiver->elements()->map() == heap->fixed_cow_array_map()) {
1749 return STORE_NO_TRANSITION_HANDLE_COW;
1750 } else {
1751 return STANDARD_STORE;
1752 }
1753 }
1754 }
1755
1756
1757 MaybeHandle<Object> KeyedStoreIC::Store(Handle<Object> object,
1758 Handle<Object> key,
1759 Handle<Object> value) {
1760 // TODO(verwaest): Let SetProperty do the migration, since storing a property
1761 // might deprecate the current map again, if value does not fit.
1762 if (MigrateDeprecated(object)) {
1763 Handle<Object> result;
1764 ASSIGN_RETURN_ON_EXCEPTION(
1765 isolate(),
1766 result,
1767 Runtime::SetObjectProperty(
1768 isolate(), object, key, value, strict_mode()),
1769 Object);
1770 return result;
1771 }
1772
1773 // Check for non-string values that can be converted into an
1774 // internalized string directly or is representable as a smi.
1775 key = TryConvertKey(key, isolate());
1776
1777 Handle<Object> store_handle;
1778 Handle<Code> stub = generic_stub();
1779
1780 if (key->IsInternalizedString()) {
1781 ASSIGN_RETURN_ON_EXCEPTION(
1782 isolate(),
1783 store_handle,
1784 StoreIC::Store(object,
1785 Handle<String>::cast(key),
1786 value,
1787 JSReceiver::MAY_BE_STORE_FROM_KEYED),
1788 Object);
1789 TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic");
1790 set_target(*stub);
1791 return store_handle;
1792 }
1793
1794 bool use_ic =
1795 FLAG_use_ic && !object->IsStringWrapper() &&
1796 !object->IsAccessCheckNeeded() && !object->IsJSGlobalProxy() &&
1797 !(object->IsJSObject() && JSObject::cast(*object)->map()->is_observed());
1798 if (use_ic && !object->IsSmi()) {
1799 // Don't use ICs for maps of the objects in Array's prototype chain. We
1800 // expect to be able to trap element sets to objects with those maps in
1801 // the runtime to enable optimization of element hole access.
1802 Handle<HeapObject> heap_object = Handle<HeapObject>::cast(object);
1803 if (heap_object->map()->IsMapInArrayPrototypeChain()) use_ic = false;
1804 }
1805
1806 if (use_ic) {
1807 DCHECK(!object->IsAccessCheckNeeded());
1808
1809 if (object->IsJSObject()) {
1810 Handle<JSObject> receiver = Handle<JSObject>::cast(object);
1811 bool key_is_smi_like = !Object::ToSmi(isolate(), key).is_null();
1812 if (receiver->elements()->map() ==
1813 isolate()->heap()->sloppy_arguments_elements_map()) {
1814 if (strict_mode() == SLOPPY) {
1815 stub = sloppy_arguments_stub();
1816 }
1817 } else if (key_is_smi_like &&
1818 !(target().is_identical_to(sloppy_arguments_stub()))) {
1819 // We should go generic if receiver isn't a dictionary, but our
1820 // prototype chain does have dictionary elements. This ensures that
1821 // other non-dictionary receivers in the polymorphic case benefit
1822 // from fast path keyed stores.
1823 if (!(receiver->map()->DictionaryElementsInPrototypeChainOnly())) {
1824 KeyedAccessStoreMode store_mode = GetStoreMode(receiver, key, value);
1825 stub = StoreElementStub(receiver, store_mode);
1826 }
1827 }
1828 }
1829 }
1830
1831 if (store_handle.is_null()) {
1832 ASSIGN_RETURN_ON_EXCEPTION(
1833 isolate(),
1834 store_handle,
1835 Runtime::SetObjectProperty(
1836 isolate(), object, key, value, strict_mode()),
1837 Object);
1838 }
1839
1840 DCHECK(!is_target_set());
1841 Code* generic = *generic_stub();
1842 if (*stub == generic) {
1843 TRACE_GENERIC_IC(isolate(), "KeyedStoreIC", "set generic");
1844 }
1845 DCHECK(!stub.is_null());
1846 set_target(*stub);
1847 TRACE_IC("StoreIC", key);
1848
1849 return store_handle;
1850 }
1851
1852
1853 CallIC::State::State(ExtraICState extra_ic_state)
1854 : argc_(ArgcBits::decode(extra_ic_state)),
1855 call_type_(CallTypeBits::decode(extra_ic_state)) {
1856 }
1857
1858
1859 ExtraICState CallIC::State::GetExtraICState() const {
1860 ExtraICState extra_ic_state =
1861 ArgcBits::encode(argc_) |
1862 CallTypeBits::encode(call_type_);
1863 return extra_ic_state;
1864 }
1865
1866
1867 bool CallIC::DoCustomHandler(Handle<Object> receiver,
1868 Handle<Object> function,
1869 Handle<FixedArray> vector,
1870 Handle<Smi> slot,
1871 const State& state) {
1872 DCHECK(FLAG_use_ic && function->IsJSFunction());
1873
1874 // Are we the array function?
1875 Handle<JSFunction> array_function = Handle<JSFunction>(
1876 isolate()->native_context()->array_function());
1877 if (array_function.is_identical_to(Handle<JSFunction>::cast(function))) {
1878 // Alter the slot.
1879 IC::State old_state = FeedbackToState(vector, slot);
1880 Object* feedback = vector->get(slot->value());
1881 if (!feedback->IsAllocationSite()) {
1882 Handle<AllocationSite> new_site =
1883 isolate()->factory()->NewAllocationSite();
1884 vector->set(slot->value(), *new_site);
1885 }
1886
1887 CallIC_ArrayStub stub(isolate(), state);
1888 set_target(*stub.GetCode());
1889 Handle<String> name;
1890 if (array_function->shared()->name()->IsString()) {
1891 name = Handle<String>(String::cast(array_function->shared()->name()),
1892 isolate());
1893 }
1894
1895 IC::State new_state = FeedbackToState(vector, slot);
1896 OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true);
1897 TRACE_VECTOR_IC("CallIC (custom handler)", name, old_state, new_state);
1898 return true;
1899 }
1900 return false;
1901 }
1902
1903
1904 void CallIC::PatchMegamorphic(Handle<Object> function,
1905 Handle<FixedArray> vector, Handle<Smi> slot) {
1906 State state(target()->extra_ic_state());
1907 IC::State old_state = FeedbackToState(vector, slot);
1908
1909 // We are going generic.
1910 vector->set(slot->value(),
1911 *TypeFeedbackInfo::MegamorphicSentinel(isolate()),
1912 SKIP_WRITE_BARRIER);
1913
1914 CallICStub stub(isolate(), state);
1915 Handle<Code> code = stub.GetCode();
1916 set_target(*code);
1917
1918 Handle<Object> name = isolate()->factory()->empty_string();
1919 if (function->IsJSFunction()) {
1920 Handle<JSFunction> js_function = Handle<JSFunction>::cast(function);
1921 name = handle(js_function->shared()->name(), isolate());
1922 }
1923
1924 IC::State new_state = FeedbackToState(vector, slot);
1925 OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true);
1926 TRACE_VECTOR_IC("CallIC", name, old_state, new_state);
1927 }
1928
1929
1930 void CallIC::HandleMiss(Handle<Object> receiver,
1931 Handle<Object> function,
1932 Handle<FixedArray> vector,
1933 Handle<Smi> slot) {
1934 State state(target()->extra_ic_state());
1935 IC::State old_state = FeedbackToState(vector, slot);
1936 Handle<Object> name = isolate()->factory()->empty_string();
1937 Object* feedback = vector->get(slot->value());
1938
1939 // Hand-coded MISS handling is easier if CallIC slots don't contain smis.
1940 DCHECK(!feedback->IsSmi());
1941
1942 if (feedback->IsJSFunction() || !function->IsJSFunction()) {
1943 // We are going generic.
1944 vector->set(slot->value(),
1945 *TypeFeedbackInfo::MegamorphicSentinel(isolate()),
1946 SKIP_WRITE_BARRIER);
1947 } else {
1948 // The feedback is either uninitialized or an allocation site.
1949 // It might be an allocation site because if we re-compile the full code
1950 // to add deoptimization support, we call with the default call-ic, and
1951 // merely need to patch the target to match the feedback.
1952 // TODO(mvstanton): the better approach is to dispense with patching
1953 // altogether, which is in progress.
1954 DCHECK(feedback == *TypeFeedbackInfo::UninitializedSentinel(isolate()) ||
1955 feedback->IsAllocationSite());
1956
1957 // Do we want to install a custom handler?
1958 if (FLAG_use_ic &&
1959 DoCustomHandler(receiver, function, vector, slot, state)) {
1960 return;
1961 }
1962
1963 vector->set(slot->value(), *function);
1964 }
1965
1966 if (function->IsJSFunction()) {
1967 Handle<JSFunction> js_function = Handle<JSFunction>::cast(function);
1968 name = handle(js_function->shared()->name(), isolate());
1969 }
1970
1971 IC::State new_state = FeedbackToState(vector, slot);
1972 OnTypeFeedbackChanged(isolate(), address(), old_state, new_state, true);
1973 TRACE_VECTOR_IC("CallIC", name, old_state, new_state);
1974 }
1975
1976
1977 #undef TRACE_IC
1978
1979
1980 // ----------------------------------------------------------------------------
1981 // Static IC stub generators.
1982 //
1983
1984 // Used from ic-<arch>.cc.
1985 RUNTIME_FUNCTION(CallIC_Miss) {
1986 TimerEventScope<TimerEventIcMiss> timer(isolate);
1987 HandleScope scope(isolate);
1988 DCHECK(args.length() == 4);
1989 CallIC ic(isolate);
1990 Handle<Object> receiver = args.at<Object>(0);
1991 Handle<Object> function = args.at<Object>(1);
1992 Handle<FixedArray> vector = args.at<FixedArray>(2);
1993 Handle<Smi> slot = args.at<Smi>(3);
1994 ic.HandleMiss(receiver, function, vector, slot);
1995 return *function;
1996 }
1997
1998
1999 RUNTIME_FUNCTION(CallIC_Customization_Miss) {
2000 TimerEventScope<TimerEventIcMiss> timer(isolate);
2001 HandleScope scope(isolate);
2002 DCHECK(args.length() == 4);
2003 // A miss on a custom call ic always results in going megamorphic.
2004 CallIC ic(isolate);
2005 Handle<Object> function = args.at<Object>(1);
2006 Handle<FixedArray> vector = args.at<FixedArray>(2);
2007 Handle<Smi> slot = args.at<Smi>(3);
2008 ic.PatchMegamorphic(function, vector, slot);
2009 return *function;
2010 }
2011
2012
2013 // Used from ic-<arch>.cc.
2014 RUNTIME_FUNCTION(LoadIC_Miss) {
2015 TimerEventScope<TimerEventIcMiss> timer(isolate);
2016 HandleScope scope(isolate);
2017 DCHECK(args.length() == 2);
2018 LoadIC ic(IC::NO_EXTRA_FRAME, isolate);
2019 Handle<Object> receiver = args.at<Object>(0);
2020 Handle<Name> key = args.at<Name>(1);
2021 ic.UpdateState(receiver, key);
2022 Handle<Object> result;
2023 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key));
2024 return *result;
2025 }
2026
2027
2028 // Used from ic-<arch>.cc
2029 RUNTIME_FUNCTION(KeyedLoadIC_Miss) {
2030 TimerEventScope<TimerEventIcMiss> timer(isolate);
2031 HandleScope scope(isolate);
2032 DCHECK(args.length() == 2);
2033 KeyedLoadIC ic(IC::NO_EXTRA_FRAME, isolate);
2034 Handle<Object> receiver = args.at<Object>(0);
2035 Handle<Object> key = args.at<Object>(1);
2036 ic.UpdateState(receiver, key);
2037 Handle<Object> result;
2038 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key));
2039 return *result;
2040 }
2041
2042
2043 RUNTIME_FUNCTION(KeyedLoadIC_MissFromStubFailure) {
2044 TimerEventScope<TimerEventIcMiss> timer(isolate);
2045 HandleScope scope(isolate);
2046 DCHECK(args.length() == 2);
2047 KeyedLoadIC ic(IC::EXTRA_CALL_FRAME, isolate);
2048 Handle<Object> receiver = args.at<Object>(0);
2049 Handle<Object> key = args.at<Object>(1);
2050 ic.UpdateState(receiver, key);
2051 Handle<Object> result;
2052 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, ic.Load(receiver, key));
2053 return *result;
2054 }
2055
2056
2057 // Used from ic-<arch>.cc.
2058 RUNTIME_FUNCTION(StoreIC_Miss) {
2059 TimerEventScope<TimerEventIcMiss> timer(isolate);
2060 HandleScope scope(isolate);
2061 DCHECK(args.length() == 3);
2062 StoreIC ic(IC::NO_EXTRA_FRAME, isolate);
2063 Handle<Object> receiver = args.at<Object>(0);
2064 Handle<String> key = args.at<String>(1);
2065 ic.UpdateState(receiver, key);
2066 Handle<Object> result;
2067 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2068 isolate,
2069 result,
2070 ic.Store(receiver, key, args.at<Object>(2)));
2071 return *result;
2072 }
2073
2074
2075 RUNTIME_FUNCTION(StoreIC_MissFromStubFailure) {
2076 TimerEventScope<TimerEventIcMiss> timer(isolate);
2077 HandleScope scope(isolate);
2078 DCHECK(args.length() == 3);
2079 StoreIC ic(IC::EXTRA_CALL_FRAME, isolate);
2080 Handle<Object> receiver = args.at<Object>(0);
2081 Handle<String> key = args.at<String>(1);
2082 ic.UpdateState(receiver, key);
2083 Handle<Object> result;
2084 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2085 isolate,
2086 result,
2087 ic.Store(receiver, key, args.at<Object>(2)));
2088 return *result;
2089 }
2090
2091
2092 // Extend storage is called in a store inline cache when
2093 // it is necessary to extend the properties array of a
2094 // JSObject.
2095 RUNTIME_FUNCTION(SharedStoreIC_ExtendStorage) {
2096 TimerEventScope<TimerEventIcMiss> timer(isolate);
2097 HandleScope shs(isolate);
2098 DCHECK(args.length() == 3);
2099
2100 // Convert the parameters
2101 Handle<JSObject> object = args.at<JSObject>(0);
2102 Handle<Map> transition = args.at<Map>(1);
2103 Handle<Object> value = args.at<Object>(2);
2104
2105 // Check the object has run out out property space.
2106 DCHECK(object->HasFastProperties());
2107 DCHECK(object->map()->unused_property_fields() == 0);
2108
2109 JSObject::MigrateToNewProperty(object, transition, value);
2110
2111 // Return the stored value.
2112 return *value;
2113 }
2114
2115
2116 // Used from ic-<arch>.cc.
2117 RUNTIME_FUNCTION(KeyedStoreIC_Miss) {
2118 TimerEventScope<TimerEventIcMiss> timer(isolate);
2119 HandleScope scope(isolate);
2120 DCHECK(args.length() == 3);
2121 KeyedStoreIC ic(IC::NO_EXTRA_FRAME, isolate);
2122 Handle<Object> receiver = args.at<Object>(0);
2123 Handle<Object> key = args.at<Object>(1);
2124 ic.UpdateState(receiver, key);
2125 Handle<Object> result;
2126 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2127 isolate,
2128 result,
2129 ic.Store(receiver, key, args.at<Object>(2)));
2130 return *result;
2131 }
2132
2133
2134 RUNTIME_FUNCTION(KeyedStoreIC_MissFromStubFailure) {
2135 TimerEventScope<TimerEventIcMiss> timer(isolate);
2136 HandleScope scope(isolate);
2137 DCHECK(args.length() == 3);
2138 KeyedStoreIC ic(IC::EXTRA_CALL_FRAME, isolate);
2139 Handle<Object> receiver = args.at<Object>(0);
2140 Handle<Object> key = args.at<Object>(1);
2141 ic.UpdateState(receiver, key);
2142 Handle<Object> result;
2143 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2144 isolate,
2145 result,
2146 ic.Store(receiver, key, args.at<Object>(2)));
2147 return *result;
2148 }
2149
2150
2151 RUNTIME_FUNCTION(StoreIC_Slow) {
2152 HandleScope scope(isolate);
2153 DCHECK(args.length() == 3);
2154 StoreIC ic(IC::NO_EXTRA_FRAME, isolate);
2155 Handle<Object> object = args.at<Object>(0);
2156 Handle<Object> key = args.at<Object>(1);
2157 Handle<Object> value = args.at<Object>(2);
2158 StrictMode strict_mode = ic.strict_mode();
2159 Handle<Object> result;
2160 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2161 isolate, result,
2162 Runtime::SetObjectProperty(
2163 isolate, object, key, value, strict_mode));
2164 return *result;
2165 }
2166
2167
2168 RUNTIME_FUNCTION(KeyedStoreIC_Slow) {
2169 HandleScope scope(isolate);
2170 DCHECK(args.length() == 3);
2171 KeyedStoreIC ic(IC::NO_EXTRA_FRAME, isolate);
2172 Handle<Object> object = args.at<Object>(0);
2173 Handle<Object> key = args.at<Object>(1);
2174 Handle<Object> value = args.at<Object>(2);
2175 StrictMode strict_mode = ic.strict_mode();
2176 Handle<Object> result;
2177 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2178 isolate, result,
2179 Runtime::SetObjectProperty(
2180 isolate, object, key, value, strict_mode));
2181 return *result;
2182 }
2183
2184
2185 RUNTIME_FUNCTION(ElementsTransitionAndStoreIC_Miss) {
2186 TimerEventScope<TimerEventIcMiss> timer(isolate);
2187 HandleScope scope(isolate);
2188 DCHECK(args.length() == 4);
2189 KeyedStoreIC ic(IC::EXTRA_CALL_FRAME, isolate);
2190 Handle<Object> value = args.at<Object>(0);
2191 Handle<Map> map = args.at<Map>(1);
2192 Handle<Object> key = args.at<Object>(2);
2193 Handle<Object> object = args.at<Object>(3);
2194 StrictMode strict_mode = ic.strict_mode();
2195 if (object->IsJSObject()) {
2196 JSObject::TransitionElementsKind(Handle<JSObject>::cast(object),
2197 map->elements_kind());
2198 }
2199 Handle<Object> result;
2200 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2201 isolate, result,
2202 Runtime::SetObjectProperty(
2203 isolate, object, key, value, strict_mode));
2204 return *result;
2205 }
2206
2207
2208 BinaryOpIC::State::State(Isolate* isolate, ExtraICState extra_ic_state)
2209 : isolate_(isolate) {
2210 op_ = static_cast<Token::Value>(
2211 FIRST_TOKEN + OpField::decode(extra_ic_state));
2212 mode_ = OverwriteModeField::decode(extra_ic_state);
2213 fixed_right_arg_ = Maybe<int>(
2214 HasFixedRightArgField::decode(extra_ic_state),
2215 1 << FixedRightArgValueField::decode(extra_ic_state));
2216 left_kind_ = LeftKindField::decode(extra_ic_state);
2217 if (fixed_right_arg_.has_value) {
2218 right_kind_ = Smi::IsValid(fixed_right_arg_.value) ? SMI : INT32;
2219 } else {
2220 right_kind_ = RightKindField::decode(extra_ic_state);
2221 }
2222 result_kind_ = ResultKindField::decode(extra_ic_state);
2223 DCHECK_LE(FIRST_TOKEN, op_);
2224 DCHECK_LE(op_, LAST_TOKEN);
2225 }
2226
2227
2228 ExtraICState BinaryOpIC::State::GetExtraICState() const {
2229 ExtraICState extra_ic_state =
2230 OpField::encode(op_ - FIRST_TOKEN) |
2231 OverwriteModeField::encode(mode_) |
2232 LeftKindField::encode(left_kind_) |
2233 ResultKindField::encode(result_kind_) |
2234 HasFixedRightArgField::encode(fixed_right_arg_.has_value);
2235 if (fixed_right_arg_.has_value) {
2236 extra_ic_state = FixedRightArgValueField::update(
2237 extra_ic_state, WhichPowerOf2(fixed_right_arg_.value));
2238 } else {
2239 extra_ic_state = RightKindField::update(extra_ic_state, right_kind_);
2240 }
2241 return extra_ic_state;
2242 }
2243
2244
2245 // static
2246 void BinaryOpIC::State::GenerateAheadOfTime(
2247 Isolate* isolate, void (*Generate)(Isolate*, const State&)) {
2248 // TODO(olivf) We should investigate why adding stubs to the snapshot is so
2249 // expensive at runtime. When solved we should be able to add most binops to
2250 // the snapshot instead of hand-picking them.
2251 // Generated list of commonly used stubs
2252 #define GENERATE(op, left_kind, right_kind, result_kind, mode) \
2253 do { \
2254 State state(isolate, op, mode); \
2255 state.left_kind_ = left_kind; \
2256 state.fixed_right_arg_.has_value = false; \
2257 state.right_kind_ = right_kind; \
2258 state.result_kind_ = result_kind; \
2259 Generate(isolate, state); \
2260 } while (false)
2261 GENERATE(Token::ADD, INT32, INT32, INT32, NO_OVERWRITE);
2262 GENERATE(Token::ADD, INT32, INT32, INT32, OVERWRITE_LEFT);
2263 GENERATE(Token::ADD, INT32, INT32, NUMBER, NO_OVERWRITE);
2264 GENERATE(Token::ADD, INT32, INT32, NUMBER, OVERWRITE_LEFT);
2265 GENERATE(Token::ADD, INT32, NUMBER, NUMBER, NO_OVERWRITE);
2266 GENERATE(Token::ADD, INT32, NUMBER, NUMBER, OVERWRITE_LEFT);
2267 GENERATE(Token::ADD, INT32, NUMBER, NUMBER, OVERWRITE_RIGHT);
2268 GENERATE(Token::ADD, INT32, SMI, INT32, NO_OVERWRITE);
2269 GENERATE(Token::ADD, INT32, SMI, INT32, OVERWRITE_LEFT);
2270 GENERATE(Token::ADD, INT32, SMI, INT32, OVERWRITE_RIGHT);
2271 GENERATE(Token::ADD, NUMBER, INT32, NUMBER, NO_OVERWRITE);
2272 GENERATE(Token::ADD, NUMBER, INT32, NUMBER, OVERWRITE_LEFT);
2273 GENERATE(Token::ADD, NUMBER, INT32, NUMBER, OVERWRITE_RIGHT);
2274 GENERATE(Token::ADD, NUMBER, NUMBER, NUMBER, NO_OVERWRITE);
2275 GENERATE(Token::ADD, NUMBER, NUMBER, NUMBER, OVERWRITE_LEFT);
2276 GENERATE(Token::ADD, NUMBER, NUMBER, NUMBER, OVERWRITE_RIGHT);
2277 GENERATE(Token::ADD, NUMBER, SMI, NUMBER, NO_OVERWRITE);
2278 GENERATE(Token::ADD, NUMBER, SMI, NUMBER, OVERWRITE_LEFT);
2279 GENERATE(Token::ADD, NUMBER, SMI, NUMBER, OVERWRITE_RIGHT);
2280 GENERATE(Token::ADD, SMI, INT32, INT32, NO_OVERWRITE);
2281 GENERATE(Token::ADD, SMI, INT32, INT32, OVERWRITE_LEFT);
2282 GENERATE(Token::ADD, SMI, INT32, NUMBER, NO_OVERWRITE);
2283 GENERATE(Token::ADD, SMI, NUMBER, NUMBER, NO_OVERWRITE);
2284 GENERATE(Token::ADD, SMI, NUMBER, NUMBER, OVERWRITE_LEFT);
2285 GENERATE(Token::ADD, SMI, NUMBER, NUMBER, OVERWRITE_RIGHT);
2286 GENERATE(Token::ADD, SMI, SMI, INT32, OVERWRITE_LEFT);
2287 GENERATE(Token::ADD, SMI, SMI, SMI, OVERWRITE_RIGHT);
2288 GENERATE(Token::BIT_AND, INT32, INT32, INT32, NO_OVERWRITE);
2289 GENERATE(Token::BIT_AND, INT32, INT32, INT32, OVERWRITE_LEFT);
2290 GENERATE(Token::BIT_AND, INT32, INT32, INT32, OVERWRITE_RIGHT);
2291 GENERATE(Token::BIT_AND, INT32, INT32, SMI, NO_OVERWRITE);
2292 GENERATE(Token::BIT_AND, INT32, INT32, SMI, OVERWRITE_RIGHT);
2293 GENERATE(Token::BIT_AND, INT32, SMI, INT32, NO_OVERWRITE);
2294 GENERATE(Token::BIT_AND, INT32, SMI, INT32, OVERWRITE_RIGHT);
2295 GENERATE(Token::BIT_AND, INT32, SMI, SMI, NO_OVERWRITE);
2296 GENERATE(Token::BIT_AND, INT32, SMI, SMI, OVERWRITE_LEFT);
2297 GENERATE(Token::BIT_AND, INT32, SMI, SMI, OVERWRITE_RIGHT);
2298 GENERATE(Token::BIT_AND, NUMBER, INT32, INT32, OVERWRITE_RIGHT);
2299 GENERATE(Token::BIT_AND, NUMBER, SMI, SMI, NO_OVERWRITE);
2300 GENERATE(Token::BIT_AND, NUMBER, SMI, SMI, OVERWRITE_RIGHT);
2301 GENERATE(Token::BIT_AND, SMI, INT32, INT32, NO_OVERWRITE);
2302 GENERATE(Token::BIT_AND, SMI, INT32, SMI, OVERWRITE_RIGHT);
2303 GENERATE(Token::BIT_AND, SMI, NUMBER, SMI, OVERWRITE_RIGHT);
2304 GENERATE(Token::BIT_AND, SMI, SMI, SMI, NO_OVERWRITE);
2305 GENERATE(Token::BIT_AND, SMI, SMI, SMI, OVERWRITE_LEFT);
2306 GENERATE(Token::BIT_AND, SMI, SMI, SMI, OVERWRITE_RIGHT);
2307 GENERATE(Token::BIT_OR, INT32, INT32, INT32, OVERWRITE_LEFT);
2308 GENERATE(Token::BIT_OR, INT32, INT32, INT32, OVERWRITE_RIGHT);
2309 GENERATE(Token::BIT_OR, INT32, INT32, SMI, OVERWRITE_LEFT);
2310 GENERATE(Token::BIT_OR, INT32, SMI, INT32, NO_OVERWRITE);
2311 GENERATE(Token::BIT_OR, INT32, SMI, INT32, OVERWRITE_LEFT);
2312 GENERATE(Token::BIT_OR, INT32, SMI, INT32, OVERWRITE_RIGHT);
2313 GENERATE(Token::BIT_OR, INT32, SMI, SMI, NO_OVERWRITE);
2314 GENERATE(Token::BIT_OR, INT32, SMI, SMI, OVERWRITE_RIGHT);
2315 GENERATE(Token::BIT_OR, NUMBER, SMI, INT32, NO_OVERWRITE);
2316 GENERATE(Token::BIT_OR, NUMBER, SMI, INT32, OVERWRITE_LEFT);
2317 GENERATE(Token::BIT_OR, NUMBER, SMI, INT32, OVERWRITE_RIGHT);
2318 GENERATE(Token::BIT_OR, NUMBER, SMI, SMI, NO_OVERWRITE);
2319 GENERATE(Token::BIT_OR, NUMBER, SMI, SMI, OVERWRITE_LEFT);
2320 GENERATE(Token::BIT_OR, SMI, INT32, INT32, OVERWRITE_LEFT);
2321 GENERATE(Token::BIT_OR, SMI, INT32, INT32, OVERWRITE_RIGHT);
2322 GENERATE(Token::BIT_OR, SMI, INT32, SMI, OVERWRITE_RIGHT);
2323 GENERATE(Token::BIT_OR, SMI, SMI, SMI, OVERWRITE_LEFT);
2324 GENERATE(Token::BIT_OR, SMI, SMI, SMI, OVERWRITE_RIGHT);
2325 GENERATE(Token::BIT_XOR, INT32, INT32, INT32, NO_OVERWRITE);
2326 GENERATE(Token::BIT_XOR, INT32, INT32, INT32, OVERWRITE_LEFT);
2327 GENERATE(Token::BIT_XOR, INT32, INT32, INT32, OVERWRITE_RIGHT);
2328 GENERATE(Token::BIT_XOR, INT32, INT32, SMI, NO_OVERWRITE);
2329 GENERATE(Token::BIT_XOR, INT32, INT32, SMI, OVERWRITE_LEFT);
2330 GENERATE(Token::BIT_XOR, INT32, NUMBER, SMI, NO_OVERWRITE);
2331 GENERATE(Token::BIT_XOR, INT32, SMI, INT32, NO_OVERWRITE);
2332 GENERATE(Token::BIT_XOR, INT32, SMI, INT32, OVERWRITE_LEFT);
2333 GENERATE(Token::BIT_XOR, INT32, SMI, INT32, OVERWRITE_RIGHT);
2334 GENERATE(Token::BIT_XOR, NUMBER, INT32, INT32, NO_OVERWRITE);
2335 GENERATE(Token::BIT_XOR, NUMBER, SMI, INT32, NO_OVERWRITE);
2336 GENERATE(Token::BIT_XOR, NUMBER, SMI, SMI, NO_OVERWRITE);
2337 GENERATE(Token::BIT_XOR, SMI, INT32, INT32, NO_OVERWRITE);
2338 GENERATE(Token::BIT_XOR, SMI, INT32, INT32, OVERWRITE_LEFT);
2339 GENERATE(Token::BIT_XOR, SMI, INT32, SMI, OVERWRITE_LEFT);
2340 GENERATE(Token::BIT_XOR, SMI, SMI, SMI, NO_OVERWRITE);
2341 GENERATE(Token::BIT_XOR, SMI, SMI, SMI, OVERWRITE_LEFT);
2342 GENERATE(Token::BIT_XOR, SMI, SMI, SMI, OVERWRITE_RIGHT);
2343 GENERATE(Token::DIV, INT32, INT32, INT32, NO_OVERWRITE);
2344 GENERATE(Token::DIV, INT32, INT32, NUMBER, NO_OVERWRITE);
2345 GENERATE(Token::DIV, INT32, NUMBER, NUMBER, NO_OVERWRITE);
2346 GENERATE(Token::DIV, INT32, NUMBER, NUMBER, OVERWRITE_LEFT);
2347 GENERATE(Token::DIV, INT32, SMI, INT32, NO_OVERWRITE);
2348 GENERATE(Token::DIV, INT32, SMI, NUMBER, NO_OVERWRITE);
2349 GENERATE(Token::DIV, NUMBER, INT32, NUMBER, NO_OVERWRITE);
2350 GENERATE(Token::DIV, NUMBER, INT32, NUMBER, OVERWRITE_LEFT);
2351 GENERATE(Token::DIV, NUMBER, NUMBER, NUMBER, NO_OVERWRITE);
2352 GENERATE(Token::DIV, NUMBER, NUMBER, NUMBER, OVERWRITE_LEFT);
2353 GENERATE(Token::DIV, NUMBER, NUMBER, NUMBER, OVERWRITE_RIGHT);
2354 GENERATE(Token::DIV, NUMBER, SMI, NUMBER, NO_OVERWRITE);
2355 GENERATE(Token::DIV, NUMBER, SMI, NUMBER, OVERWRITE_LEFT);
2356 GENERATE(Token::DIV, SMI, INT32, INT32, NO_OVERWRITE);
2357 GENERATE(Token::DIV, SMI, INT32, NUMBER, NO_OVERWRITE);
2358 GENERATE(Token::DIV, SMI, INT32, NUMBER, OVERWRITE_LEFT);
2359 GENERATE(Token::DIV, SMI, NUMBER, NUMBER, NO_OVERWRITE);
2360 GENERATE(Token::DIV, SMI, NUMBER, NUMBER, OVERWRITE_LEFT);
2361 GENERATE(Token::DIV, SMI, NUMBER, NUMBER, OVERWRITE_RIGHT);
2362 GENERATE(Token::DIV, SMI, SMI, NUMBER, NO_OVERWRITE);
2363 GENERATE(Token::DIV, SMI, SMI, NUMBER, OVERWRITE_LEFT);
2364 GENERATE(Token::DIV, SMI, SMI, NUMBER, OVERWRITE_RIGHT);
2365 GENERATE(Token::DIV, SMI, SMI, SMI, NO_OVERWRITE);
2366 GENERATE(Token::DIV, SMI, SMI, SMI, OVERWRITE_LEFT);
2367 GENERATE(Token::DIV, SMI, SMI, SMI, OVERWRITE_RIGHT);
2368 GENERATE(Token::MOD, NUMBER, SMI, NUMBER, OVERWRITE_LEFT);
2369 GENERATE(Token::MOD, SMI, SMI, SMI, NO_OVERWRITE);
2370 GENERATE(Token::MOD, SMI, SMI, SMI, OVERWRITE_LEFT);
2371 GENERATE(Token::MUL, INT32, INT32, INT32, NO_OVERWRITE);
2372 GENERATE(Token::MUL, INT32, INT32, NUMBER, NO_OVERWRITE);
2373 GENERATE(Token::MUL, INT32, NUMBER, NUMBER, NO_OVERWRITE);
2374 GENERATE(Token::MUL, INT32, NUMBER, NUMBER, OVERWRITE_LEFT);
2375 GENERATE(Token::MUL, INT32, SMI, INT32, NO_OVERWRITE);
2376 GENERATE(Token::MUL, INT32, SMI, INT32, OVERWRITE_LEFT);
2377 GENERATE(Token::MUL, INT32, SMI, NUMBER, NO_OVERWRITE);
2378 GENERATE(Token::MUL, NUMBER, INT32, NUMBER, NO_OVERWRITE);
2379 GENERATE(Token::MUL, NUMBER, INT32, NUMBER, OVERWRITE_LEFT);
2380 GENERATE(Token::MUL, NUMBER, INT32, NUMBER, OVERWRITE_RIGHT);
2381 GENERATE(Token::MUL, NUMBER, NUMBER, NUMBER, NO_OVERWRITE);
2382 GENERATE(Token::MUL, NUMBER, NUMBER, NUMBER, OVERWRITE_LEFT);
2383 GENERATE(Token::MUL, NUMBER, SMI, NUMBER, NO_OVERWRITE);
2384 GENERATE(Token::MUL, NUMBER, SMI, NUMBER, OVERWRITE_LEFT);
2385 GENERATE(Token::MUL, NUMBER, SMI, NUMBER, OVERWRITE_RIGHT);
2386 GENERATE(Token::MUL, SMI, INT32, INT32, NO_OVERWRITE);
2387 GENERATE(Token::MUL, SMI, INT32, INT32, OVERWRITE_LEFT);
2388 GENERATE(Token::MUL, SMI, INT32, NUMBER, NO_OVERWRITE);
2389 GENERATE(Token::MUL, SMI, NUMBER, NUMBER, NO_OVERWRITE);
2390 GENERATE(Token::MUL, SMI, NUMBER, NUMBER, OVERWRITE_LEFT);
2391 GENERATE(Token::MUL, SMI, NUMBER, NUMBER, OVERWRITE_RIGHT);
2392 GENERATE(Token::MUL, SMI, SMI, INT32, NO_OVERWRITE);
2393 GENERATE(Token::MUL, SMI, SMI, NUMBER, NO_OVERWRITE);
2394 GENERATE(Token::MUL, SMI, SMI, NUMBER, OVERWRITE_LEFT);
2395 GENERATE(Token::MUL, SMI, SMI, SMI, NO_OVERWRITE);
2396 GENERATE(Token::MUL, SMI, SMI, SMI, OVERWRITE_LEFT);
2397 GENERATE(Token::MUL, SMI, SMI, SMI, OVERWRITE_RIGHT);
2398 GENERATE(Token::SAR, INT32, SMI, INT32, OVERWRITE_RIGHT);
2399 GENERATE(Token::SAR, INT32, SMI, SMI, NO_OVERWRITE);
2400 GENERATE(Token::SAR, INT32, SMI, SMI, OVERWRITE_RIGHT);
2401 GENERATE(Token::SAR, NUMBER, SMI, SMI, NO_OVERWRITE);
2402 GENERATE(Token::SAR, NUMBER, SMI, SMI, OVERWRITE_RIGHT);
2403 GENERATE(Token::SAR, SMI, SMI, SMI, OVERWRITE_LEFT);
2404 GENERATE(Token::SAR, SMI, SMI, SMI, OVERWRITE_RIGHT);
2405 GENERATE(Token::SHL, INT32, SMI, INT32, NO_OVERWRITE);
2406 GENERATE(Token::SHL, INT32, SMI, INT32, OVERWRITE_RIGHT);
2407 GENERATE(Token::SHL, INT32, SMI, SMI, NO_OVERWRITE);
2408 GENERATE(Token::SHL, INT32, SMI, SMI, OVERWRITE_RIGHT);
2409 GENERATE(Token::SHL, NUMBER, SMI, SMI, OVERWRITE_RIGHT);
2410 GENERATE(Token::SHL, SMI, SMI, INT32, NO_OVERWRITE);
2411 GENERATE(Token::SHL, SMI, SMI, INT32, OVERWRITE_LEFT);
2412 GENERATE(Token::SHL, SMI, SMI, INT32, OVERWRITE_RIGHT);
2413 GENERATE(Token::SHL, SMI, SMI, SMI, NO_OVERWRITE);
2414 GENERATE(Token::SHL, SMI, SMI, SMI, OVERWRITE_LEFT);
2415 GENERATE(Token::SHL, SMI, SMI, SMI, OVERWRITE_RIGHT);
2416 GENERATE(Token::SHR, INT32, SMI, SMI, NO_OVERWRITE);
2417 GENERATE(Token::SHR, INT32, SMI, SMI, OVERWRITE_LEFT);
2418 GENERATE(Token::SHR, INT32, SMI, SMI, OVERWRITE_RIGHT);
2419 GENERATE(Token::SHR, NUMBER, SMI, SMI, NO_OVERWRITE);
2420 GENERATE(Token::SHR, NUMBER, SMI, SMI, OVERWRITE_LEFT);
2421 GENERATE(Token::SHR, NUMBER, SMI, INT32, OVERWRITE_RIGHT);
2422 GENERATE(Token::SHR, SMI, SMI, SMI, NO_OVERWRITE);
2423 GENERATE(Token::SHR, SMI, SMI, SMI, OVERWRITE_LEFT);
2424 GENERATE(Token::SHR, SMI, SMI, SMI, OVERWRITE_RIGHT);
2425 GENERATE(Token::SUB, INT32, INT32, INT32, NO_OVERWRITE);
2426 GENERATE(Token::SUB, INT32, INT32, INT32, OVERWRITE_LEFT);
2427 GENERATE(Token::SUB, INT32, NUMBER, NUMBER, NO_OVERWRITE);
2428 GENERATE(Token::SUB, INT32, NUMBER, NUMBER, OVERWRITE_RIGHT);
2429 GENERATE(Token::SUB, INT32, SMI, INT32, OVERWRITE_LEFT);
2430 GENERATE(Token::SUB, INT32, SMI, INT32, OVERWRITE_RIGHT);
2431 GENERATE(Token::SUB, NUMBER, INT32, NUMBER, NO_OVERWRITE);
2432 GENERATE(Token::SUB, NUMBER, INT32, NUMBER, OVERWRITE_LEFT);
2433 GENERATE(Token::SUB, NUMBER, NUMBER, NUMBER, NO_OVERWRITE);
2434 GENERATE(Token::SUB, NUMBER, NUMBER, NUMBER, OVERWRITE_LEFT);
2435 GENERATE(Token::SUB, NUMBER, NUMBER, NUMBER, OVERWRITE_RIGHT);
2436 GENERATE(Token::SUB, NUMBER, SMI, NUMBER, NO_OVERWRITE);
2437 GENERATE(Token::SUB, NUMBER, SMI, NUMBER, OVERWRITE_LEFT);
2438 GENERATE(Token::SUB, NUMBER, SMI, NUMBER, OVERWRITE_RIGHT);
2439 GENERATE(Token::SUB, SMI, INT32, INT32, NO_OVERWRITE);
2440 GENERATE(Token::SUB, SMI, NUMBER, NUMBER, NO_OVERWRITE);
2441 GENERATE(Token::SUB, SMI, NUMBER, NUMBER, OVERWRITE_LEFT);
2442 GENERATE(Token::SUB, SMI, NUMBER, NUMBER, OVERWRITE_RIGHT);
2443 GENERATE(Token::SUB, SMI, SMI, SMI, NO_OVERWRITE);
2444 GENERATE(Token::SUB, SMI, SMI, SMI, OVERWRITE_LEFT);
2445 GENERATE(Token::SUB, SMI, SMI, SMI, OVERWRITE_RIGHT);
2446 #undef GENERATE
2447 #define GENERATE(op, left_kind, fixed_right_arg_value, result_kind, mode) \
2448 do { \
2449 State state(isolate, op, mode); \
2450 state.left_kind_ = left_kind; \
2451 state.fixed_right_arg_.has_value = true; \
2452 state.fixed_right_arg_.value = fixed_right_arg_value; \
2453 state.right_kind_ = SMI; \
2454 state.result_kind_ = result_kind; \
2455 Generate(isolate, state); \
2456 } while (false)
2457 GENERATE(Token::MOD, SMI, 2, SMI, NO_OVERWRITE);
2458 GENERATE(Token::MOD, SMI, 4, SMI, NO_OVERWRITE);
2459 GENERATE(Token::MOD, SMI, 4, SMI, OVERWRITE_LEFT);
2460 GENERATE(Token::MOD, SMI, 8, SMI, NO_OVERWRITE);
2461 GENERATE(Token::MOD, SMI, 16, SMI, OVERWRITE_LEFT);
2462 GENERATE(Token::MOD, SMI, 32, SMI, NO_OVERWRITE);
2463 GENERATE(Token::MOD, SMI, 2048, SMI, NO_OVERWRITE);
2464 #undef GENERATE
2465 }
2466
2467
2468 Type* BinaryOpIC::State::GetResultType(Zone* zone) const {
2469 Kind result_kind = result_kind_;
2470 if (HasSideEffects()) {
2471 result_kind = NONE;
2472 } else if (result_kind == GENERIC && op_ == Token::ADD) {
2473 return Type::Union(Type::Number(zone), Type::String(zone), zone);
2474 } else if (result_kind == NUMBER && op_ == Token::SHR) {
2475 return Type::Unsigned32(zone);
2476 }
2477 DCHECK_NE(GENERIC, result_kind);
2478 return KindToType(result_kind, zone);
2479 }
2480
2481
2482 OStream& operator<<(OStream& os, const BinaryOpIC::State& s) {
2483 os << "(" << Token::Name(s.op_);
2484 if (s.mode_ == OVERWRITE_LEFT)
2485 os << "_ReuseLeft";
2486 else if (s.mode_ == OVERWRITE_RIGHT)
2487 os << "_ReuseRight";
2488 if (s.CouldCreateAllocationMementos()) os << "_CreateAllocationMementos";
2489 os << ":" << BinaryOpIC::State::KindToString(s.left_kind_) << "*";
2490 if (s.fixed_right_arg_.has_value) {
2491 os << s.fixed_right_arg_.value;
2492 } else {
2493 os << BinaryOpIC::State::KindToString(s.right_kind_);
2494 }
2495 return os << "->" << BinaryOpIC::State::KindToString(s.result_kind_) << ")";
2496 }
2497
2498
2499 void BinaryOpIC::State::Update(Handle<Object> left,
2500 Handle<Object> right,
2501 Handle<Object> result) {
2502 ExtraICState old_extra_ic_state = GetExtraICState();
2503
2504 left_kind_ = UpdateKind(left, left_kind_);
2505 right_kind_ = UpdateKind(right, right_kind_);
2506
2507 int32_t fixed_right_arg_value = 0;
2508 bool has_fixed_right_arg =
2509 op_ == Token::MOD &&
2510 right->ToInt32(&fixed_right_arg_value) &&
2511 fixed_right_arg_value > 0 &&
2512 IsPowerOf2(fixed_right_arg_value) &&
2513 FixedRightArgValueField::is_valid(WhichPowerOf2(fixed_right_arg_value)) &&
2514 (left_kind_ == SMI || left_kind_ == INT32) &&
2515 (result_kind_ == NONE || !fixed_right_arg_.has_value);
2516 fixed_right_arg_ = Maybe<int32_t>(has_fixed_right_arg,
2517 fixed_right_arg_value);
2518
2519 result_kind_ = UpdateKind(result, result_kind_);
2520
2521 if (!Token::IsTruncatingBinaryOp(op_)) {
2522 Kind input_kind = Max(left_kind_, right_kind_);
2523 if (result_kind_ < input_kind && input_kind <= NUMBER) {
2524 result_kind_ = input_kind;
2525 }
2526 }
2527
2528 // We don't want to distinguish INT32 and NUMBER for string add (because
2529 // NumberToString can't make use of this anyway).
2530 if (left_kind_ == STRING && right_kind_ == INT32) {
2531 DCHECK_EQ(STRING, result_kind_);
2532 DCHECK_EQ(Token::ADD, op_);
2533 right_kind_ = NUMBER;
2534 } else if (right_kind_ == STRING && left_kind_ == INT32) {
2535 DCHECK_EQ(STRING, result_kind_);
2536 DCHECK_EQ(Token::ADD, op_);
2537 left_kind_ = NUMBER;
2538 }
2539
2540 // Reset overwrite mode unless we can actually make use of it, or may be able
2541 // to make use of it at some point in the future.
2542 if ((mode_ == OVERWRITE_LEFT && left_kind_ > NUMBER) ||
2543 (mode_ == OVERWRITE_RIGHT && right_kind_ > NUMBER) ||
2544 result_kind_ > NUMBER) {
2545 mode_ = NO_OVERWRITE;
2546 }
2547
2548 if (old_extra_ic_state == GetExtraICState()) {
2549 // Tagged operations can lead to non-truncating HChanges
2550 if (left->IsUndefined() || left->IsBoolean()) {
2551 left_kind_ = GENERIC;
2552 } else {
2553 DCHECK(right->IsUndefined() || right->IsBoolean());
2554 right_kind_ = GENERIC;
2555 }
2556 }
2557 }
2558
2559
2560 BinaryOpIC::State::Kind BinaryOpIC::State::UpdateKind(Handle<Object> object,
2561 Kind kind) const {
2562 Kind new_kind = GENERIC;
2563 bool is_truncating = Token::IsTruncatingBinaryOp(op());
2564 if (object->IsBoolean() && is_truncating) {
2565 // Booleans will be automatically truncated by HChange.
2566 new_kind = INT32;
2567 } else if (object->IsUndefined()) {
2568 // Undefined will be automatically truncated by HChange.
2569 new_kind = is_truncating ? INT32 : NUMBER;
2570 } else if (object->IsSmi()) {
2571 new_kind = SMI;
2572 } else if (object->IsHeapNumber()) {
2573 double value = Handle<HeapNumber>::cast(object)->value();
2574 new_kind = IsInt32Double(value) ? INT32 : NUMBER;
2575 } else if (object->IsString() && op() == Token::ADD) {
2576 new_kind = STRING;
2577 }
2578 if (new_kind == INT32 && SmiValuesAre32Bits()) {
2579 new_kind = NUMBER;
2580 }
2581 if (kind != NONE &&
2582 ((new_kind <= NUMBER && kind > NUMBER) ||
2583 (new_kind > NUMBER && kind <= NUMBER))) {
2584 new_kind = GENERIC;
2585 }
2586 return Max(kind, new_kind);
2587 }
2588
2589
2590 // static
2591 const char* BinaryOpIC::State::KindToString(Kind kind) {
2592 switch (kind) {
2593 case NONE: return "None";
2594 case SMI: return "Smi";
2595 case INT32: return "Int32";
2596 case NUMBER: return "Number";
2597 case STRING: return "String";
2598 case GENERIC: return "Generic";
2599 }
2600 UNREACHABLE();
2601 return NULL;
2602 }
2603
2604
2605 // static
2606 Type* BinaryOpIC::State::KindToType(Kind kind, Zone* zone) {
2607 switch (kind) {
2608 case NONE: return Type::None(zone);
2609 case SMI: return Type::SignedSmall(zone);
2610 case INT32: return Type::Signed32(zone);
2611 case NUMBER: return Type::Number(zone);
2612 case STRING: return Type::String(zone);
2613 case GENERIC: return Type::Any(zone);
2614 }
2615 UNREACHABLE();
2616 return NULL;
2617 }
2618
2619
2620 MaybeHandle<Object> BinaryOpIC::Transition(
2621 Handle<AllocationSite> allocation_site,
2622 Handle<Object> left,
2623 Handle<Object> right) {
2624 State state(isolate(), target()->extra_ic_state());
2625
2626 // Compute the actual result using the builtin for the binary operation.
2627 Object* builtin = isolate()->js_builtins_object()->javascript_builtin(
2628 TokenToJSBuiltin(state.op()));
2629 Handle<JSFunction> function = handle(JSFunction::cast(builtin), isolate());
2630 Handle<Object> result;
2631 ASSIGN_RETURN_ON_EXCEPTION(
2632 isolate(),
2633 result,
2634 Execution::Call(isolate(), function, left, 1, &right),
2635 Object);
2636
2637 // Execution::Call can execute arbitrary JavaScript, hence potentially
2638 // update the state of this very IC, so we must update the stored state.
2639 UpdateTarget();
2640 // Compute the new state.
2641 State old_state(isolate(), target()->extra_ic_state());
2642 state.Update(left, right, result);
2643
2644 // Check if we have a string operation here.
2645 Handle<Code> target;
2646 if (!allocation_site.is_null() || state.ShouldCreateAllocationMementos()) {
2647 // Setup the allocation site on-demand.
2648 if (allocation_site.is_null()) {
2649 allocation_site = isolate()->factory()->NewAllocationSite();
2650 }
2651
2652 // Install the stub with an allocation site.
2653 BinaryOpICWithAllocationSiteStub stub(isolate(), state);
2654 target = stub.GetCodeCopyFromTemplate(allocation_site);
2655
2656 // Sanity check the trampoline stub.
2657 DCHECK_EQ(*allocation_site, target->FindFirstAllocationSite());
2658 } else {
2659 // Install the generic stub.
2660 BinaryOpICStub stub(isolate(), state);
2661 target = stub.GetCode();
2662
2663 // Sanity check the generic stub.
2664 DCHECK_EQ(NULL, target->FindFirstAllocationSite());
2665 }
2666 set_target(*target);
2667
2668 if (FLAG_trace_ic) {
2669 OFStream os(stdout);
2670 os << "[BinaryOpIC" << old_state << " => " << state << " @ "
2671 << static_cast<void*>(*target) << " <- ";
2672 JavaScriptFrame::PrintTop(isolate(), stdout, false, true);
2673 if (!allocation_site.is_null()) {
2674 os << " using allocation site " << static_cast<void*>(*allocation_site);
2675 }
2676 os << "]" << endl;
2677 }
2678
2679 // Patch the inlined smi code as necessary.
2680 if (!old_state.UseInlinedSmiCode() && state.UseInlinedSmiCode()) {
2681 PatchInlinedSmiCode(address(), ENABLE_INLINED_SMI_CHECK);
2682 } else if (old_state.UseInlinedSmiCode() && !state.UseInlinedSmiCode()) {
2683 PatchInlinedSmiCode(address(), DISABLE_INLINED_SMI_CHECK);
2684 }
2685
2686 return result;
2687 }
2688
2689
2690 RUNTIME_FUNCTION(BinaryOpIC_Miss) {
2691 TimerEventScope<TimerEventIcMiss> timer(isolate);
2692 HandleScope scope(isolate);
2693 DCHECK_EQ(2, args.length());
2694 Handle<Object> left = args.at<Object>(BinaryOpICStub::kLeft);
2695 Handle<Object> right = args.at<Object>(BinaryOpICStub::kRight);
2696 BinaryOpIC ic(isolate);
2697 Handle<Object> result;
2698 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2699 isolate,
2700 result,
2701 ic.Transition(Handle<AllocationSite>::null(), left, right));
2702 return *result;
2703 }
2704
2705
2706 RUNTIME_FUNCTION(BinaryOpIC_MissWithAllocationSite) {
2707 TimerEventScope<TimerEventIcMiss> timer(isolate);
2708 HandleScope scope(isolate);
2709 DCHECK_EQ(3, args.length());
2710 Handle<AllocationSite> allocation_site = args.at<AllocationSite>(
2711 BinaryOpWithAllocationSiteStub::kAllocationSite);
2712 Handle<Object> left = args.at<Object>(
2713 BinaryOpWithAllocationSiteStub::kLeft);
2714 Handle<Object> right = args.at<Object>(
2715 BinaryOpWithAllocationSiteStub::kRight);
2716 BinaryOpIC ic(isolate);
2717 Handle<Object> result;
2718 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
2719 isolate,
2720 result,
2721 ic.Transition(allocation_site, left, right));
2722 return *result;
2723 }
2724
2725
2726 Code* CompareIC::GetRawUninitialized(Isolate* isolate, Token::Value op) {
2727 ICCompareStub stub(isolate, op, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED);
2728 Code* code = NULL;
2729 CHECK(stub.FindCodeInCache(&code));
2730 return code;
2731 }
2732
2733
2734 Handle<Code> CompareIC::GetUninitialized(Isolate* isolate, Token::Value op) {
2735 ICCompareStub stub(isolate, op, UNINITIALIZED, UNINITIALIZED, UNINITIALIZED);
2736 return stub.GetCode();
2737 }
2738
2739
2740 const char* CompareIC::GetStateName(State state) {
2741 switch (state) {
2742 case UNINITIALIZED: return "UNINITIALIZED";
2743 case SMI: return "SMI";
2744 case NUMBER: return "NUMBER";
2745 case INTERNALIZED_STRING: return "INTERNALIZED_STRING";
2746 case STRING: return "STRING";
2747 case UNIQUE_NAME: return "UNIQUE_NAME";
2748 case OBJECT: return "OBJECT";
2749 case KNOWN_OBJECT: return "KNOWN_OBJECT";
2750 case GENERIC: return "GENERIC";
2751 }
2752 UNREACHABLE();
2753 return NULL;
2754 }
2755
2756
2757 Type* CompareIC::StateToType(
2758 Zone* zone,
2759 CompareIC::State state,
2760 Handle<Map> map) {
2761 switch (state) {
2762 case CompareIC::UNINITIALIZED: return Type::None(zone);
2763 case CompareIC::SMI: return Type::SignedSmall(zone);
2764 case CompareIC::NUMBER: return Type::Number(zone);
2765 case CompareIC::STRING: return Type::String(zone);
2766 case CompareIC::INTERNALIZED_STRING: return Type::InternalizedString(zone);
2767 case CompareIC::UNIQUE_NAME: return Type::UniqueName(zone);
2768 case CompareIC::OBJECT: return Type::Receiver(zone);
2769 case CompareIC::KNOWN_OBJECT:
2770 return map.is_null() ? Type::Receiver(zone) : Type::Class(map, zone);
2771 case CompareIC::GENERIC: return Type::Any(zone);
2772 }
2773 UNREACHABLE();
2774 return NULL;
2775 }
2776
2777
2778 void CompareIC::StubInfoToType(uint32_t stub_key, Type** left_type,
2779 Type** right_type, Type** overall_type,
2780 Handle<Map> map, Zone* zone) {
2781 State left_state, right_state, handler_state;
2782 ICCompareStub::DecodeKey(stub_key, &left_state, &right_state, &handler_state,
2783 NULL);
2784 *left_type = StateToType(zone, left_state);
2785 *right_type = StateToType(zone, right_state);
2786 *overall_type = StateToType(zone, handler_state, map);
2787 }
2788
2789
2790 CompareIC::State CompareIC::NewInputState(State old_state,
2791 Handle<Object> value) {
2792 switch (old_state) {
2793 case UNINITIALIZED:
2794 if (value->IsSmi()) return SMI;
2795 if (value->IsHeapNumber()) return NUMBER;
2796 if (value->IsInternalizedString()) return INTERNALIZED_STRING;
2797 if (value->IsString()) return STRING;
2798 if (value->IsSymbol()) return UNIQUE_NAME;
2799 if (value->IsJSObject()) return OBJECT;
2800 break;
2801 case SMI:
2802 if (value->IsSmi()) return SMI;
2803 if (value->IsHeapNumber()) return NUMBER;
2804 break;
2805 case NUMBER:
2806 if (value->IsNumber()) return NUMBER;
2807 break;
2808 case INTERNALIZED_STRING:
2809 if (value->IsInternalizedString()) return INTERNALIZED_STRING;
2810 if (value->IsString()) return STRING;
2811 if (value->IsSymbol()) return UNIQUE_NAME;
2812 break;
2813 case STRING:
2814 if (value->IsString()) return STRING;
2815 break;
2816 case UNIQUE_NAME:
2817 if (value->IsUniqueName()) return UNIQUE_NAME;
2818 break;
2819 case OBJECT:
2820 if (value->IsJSObject()) return OBJECT;
2821 break;
2822 case GENERIC:
2823 break;
2824 case KNOWN_OBJECT:
2825 UNREACHABLE();
2826 break;
2827 }
2828 return GENERIC;
2829 }
2830
2831
2832 CompareIC::State CompareIC::TargetState(State old_state,
2833 State old_left,
2834 State old_right,
2835 bool has_inlined_smi_code,
2836 Handle<Object> x,
2837 Handle<Object> y) {
2838 switch (old_state) {
2839 case UNINITIALIZED:
2840 if (x->IsSmi() && y->IsSmi()) return SMI;
2841 if (x->IsNumber() && y->IsNumber()) return NUMBER;
2842 if (Token::IsOrderedRelationalCompareOp(op_)) {
2843 // Ordered comparisons treat undefined as NaN, so the
2844 // NUMBER stub will do the right thing.
2845 if ((x->IsNumber() && y->IsUndefined()) ||
2846 (y->IsNumber() && x->IsUndefined())) {
2847 return NUMBER;
2848 }
2849 }
2850 if (x->IsInternalizedString() && y->IsInternalizedString()) {
2851 // We compare internalized strings as plain ones if we need to determine
2852 // the order in a non-equality compare.
2853 return Token::IsEqualityOp(op_) ? INTERNALIZED_STRING : STRING;
2854 }
2855 if (x->IsString() && y->IsString()) return STRING;
2856 if (!Token::IsEqualityOp(op_)) return GENERIC;
2857 if (x->IsUniqueName() && y->IsUniqueName()) return UNIQUE_NAME;
2858 if (x->IsJSObject() && y->IsJSObject()) {
2859 if (Handle<JSObject>::cast(x)->map() ==
2860 Handle<JSObject>::cast(y)->map()) {
2861 return KNOWN_OBJECT;
2862 } else {
2863 return OBJECT;
2864 }
2865 }
2866 return GENERIC;
2867 case SMI:
2868 return x->IsNumber() && y->IsNumber() ? NUMBER : GENERIC;
2869 case INTERNALIZED_STRING:
2870 DCHECK(Token::IsEqualityOp(op_));
2871 if (x->IsString() && y->IsString()) return STRING;
2872 if (x->IsUniqueName() && y->IsUniqueName()) return UNIQUE_NAME;
2873 return GENERIC;
2874 case NUMBER:
2875 // If the failure was due to one side changing from smi to heap number,
2876 // then keep the state (if other changed at the same time, we will get
2877 // a second miss and then go to generic).
2878 if (old_left == SMI && x->IsHeapNumber()) return NUMBER;
2879 if (old_right == SMI && y->IsHeapNumber()) return NUMBER;
2880 return GENERIC;
2881 case KNOWN_OBJECT:
2882 DCHECK(Token::IsEqualityOp(op_));
2883 if (x->IsJSObject() && y->IsJSObject()) return OBJECT;
2884 return GENERIC;
2885 case STRING:
2886 case UNIQUE_NAME:
2887 case OBJECT:
2888 case GENERIC:
2889 return GENERIC;
2890 }
2891 UNREACHABLE();
2892 return GENERIC; // Make the compiler happy.
2893 }
2894
2895
2896 Code* CompareIC::UpdateCaches(Handle<Object> x, Handle<Object> y) {
2897 HandleScope scope(isolate());
2898 State previous_left, previous_right, previous_state;
2899 ICCompareStub::DecodeKey(target()->stub_key(), &previous_left,
2900 &previous_right, &previous_state, NULL);
2901 State new_left = NewInputState(previous_left, x);
2902 State new_right = NewInputState(previous_right, y);
2903 State state = TargetState(previous_state, previous_left, previous_right,
2904 HasInlinedSmiCode(address()), x, y);
2905 ICCompareStub stub(isolate(), op_, new_left, new_right, state);
2906 if (state == KNOWN_OBJECT) {
2907 stub.set_known_map(
2908 Handle<Map>(Handle<JSObject>::cast(x)->map(), isolate()));
2909 }
2910 Handle<Code> new_target = stub.GetCode();
2911 set_target(*new_target);
2912
2913 if (FLAG_trace_ic) {
2914 PrintF("[CompareIC in ");
2915 JavaScriptFrame::PrintTop(isolate(), stdout, false, true);
2916 PrintF(" ((%s+%s=%s)->(%s+%s=%s))#%s @ %p]\n",
2917 GetStateName(previous_left),
2918 GetStateName(previous_right),
2919 GetStateName(previous_state),
2920 GetStateName(new_left),
2921 GetStateName(new_right),
2922 GetStateName(state),
2923 Token::Name(op_),
2924 static_cast<void*>(*stub.GetCode()));
2925 }
2926
2927 // Activate inlined smi code.
2928 if (previous_state == UNINITIALIZED) {
2929 PatchInlinedSmiCode(address(), ENABLE_INLINED_SMI_CHECK);
2930 }
2931
2932 return *new_target;
2933 }
2934
2935
2936 // Used from ICCompareStub::GenerateMiss in code-stubs-<arch>.cc.
2937 RUNTIME_FUNCTION(CompareIC_Miss) {
2938 TimerEventScope<TimerEventIcMiss> timer(isolate);
2939 HandleScope scope(isolate);
2940 DCHECK(args.length() == 3);
2941 CompareIC ic(isolate, static_cast<Token::Value>(args.smi_at(2)));
2942 return ic.UpdateCaches(args.at<Object>(0), args.at<Object>(1));
2943 }
2944
2945
2946 void CompareNilIC::Clear(Address address,
2947 Code* target,
2948 ConstantPoolArray* constant_pool) {
2949 if (IsCleared(target)) return;
2950 ExtraICState state = target->extra_ic_state();
2951
2952 CompareNilICStub stub(target->GetIsolate(),
2953 state,
2954 HydrogenCodeStub::UNINITIALIZED);
2955 stub.ClearState();
2956
2957 Code* code = NULL;
2958 CHECK(stub.FindCodeInCache(&code));
2959
2960 SetTargetAtAddress(address, code, constant_pool);
2961 }
2962
2963
2964 Handle<Object> CompareNilIC::DoCompareNilSlow(Isolate* isolate,
2965 NilValue nil,
2966 Handle<Object> object) {
2967 if (object->IsNull() || object->IsUndefined()) {
2968 return handle(Smi::FromInt(true), isolate);
2969 }
2970 return handle(Smi::FromInt(object->IsUndetectableObject()), isolate);
2971 }
2972
2973
2974 Handle<Object> CompareNilIC::CompareNil(Handle<Object> object) {
2975 ExtraICState extra_ic_state = target()->extra_ic_state();
2976
2977 CompareNilICStub stub(isolate(), extra_ic_state);
2978
2979 // Extract the current supported types from the patched IC and calculate what
2980 // types must be supported as a result of the miss.
2981 bool already_monomorphic = stub.IsMonomorphic();
2982
2983 stub.UpdateStatus(object);
2984
2985 NilValue nil = stub.GetNilValue();
2986
2987 // Find or create the specialized stub to support the new set of types.
2988 Handle<Code> code;
2989 if (stub.IsMonomorphic()) {
2990 Handle<Map> monomorphic_map(already_monomorphic && FirstTargetMap() != NULL
2991 ? FirstTargetMap()
2992 : HeapObject::cast(*object)->map());
2993 code = PropertyICCompiler::ComputeCompareNil(monomorphic_map, &stub);
2994 } else {
2995 code = stub.GetCode();
2996 }
2997 set_target(*code);
2998 return DoCompareNilSlow(isolate(), nil, object);
2999 }
3000
3001
3002 RUNTIME_FUNCTION(CompareNilIC_Miss) {
3003 TimerEventScope<TimerEventIcMiss> timer(isolate);
3004 HandleScope scope(isolate);
3005 Handle<Object> object = args.at<Object>(0);
3006 CompareNilIC ic(isolate);
3007 return *ic.CompareNil(object);
3008 }
3009
3010
3011 RUNTIME_FUNCTION(Unreachable) {
3012 UNREACHABLE();
3013 CHECK(false);
3014 return isolate->heap()->undefined_value();
3015 }
3016
3017
3018 Builtins::JavaScript BinaryOpIC::TokenToJSBuiltin(Token::Value op) {
3019 switch (op) {
3020 default:
3021 UNREACHABLE();
3022 case Token::ADD:
3023 return Builtins::ADD;
3024 break;
3025 case Token::SUB:
3026 return Builtins::SUB;
3027 break;
3028 case Token::MUL:
3029 return Builtins::MUL;
3030 break;
3031 case Token::DIV:
3032 return Builtins::DIV;
3033 break;
3034 case Token::MOD:
3035 return Builtins::MOD;
3036 break;
3037 case Token::BIT_OR:
3038 return Builtins::BIT_OR;
3039 break;
3040 case Token::BIT_AND:
3041 return Builtins::BIT_AND;
3042 break;
3043 case Token::BIT_XOR:
3044 return Builtins::BIT_XOR;
3045 break;
3046 case Token::SAR:
3047 return Builtins::SAR;
3048 break;
3049 case Token::SHR:
3050 return Builtins::SHR;
3051 break;
3052 case Token::SHL:
3053 return Builtins::SHL;
3054 break;
3055 }
3056 }
3057
3058
3059 Handle<Object> ToBooleanIC::ToBoolean(Handle<Object> object) {
3060 ToBooleanStub stub(isolate(), target()->extra_ic_state());
3061 bool to_boolean_value = stub.UpdateStatus(object);
3062 Handle<Code> code = stub.GetCode();
3063 set_target(*code);
3064 return handle(Smi::FromInt(to_boolean_value ? 1 : 0), isolate());
3065 }
3066
3067
3068 RUNTIME_FUNCTION(ToBooleanIC_Miss) {
3069 TimerEventScope<TimerEventIcMiss> timer(isolate);
3070 DCHECK(args.length() == 1);
3071 HandleScope scope(isolate);
3072 Handle<Object> object = args.at<Object>(0);
3073 ToBooleanIC ic(isolate);
3074 return *ic.ToBoolean(object);
3075 }
3076
3077
3078 static const Address IC_utilities[] = {
3079 #define ADDR(name) FUNCTION_ADDR(name),
3080 IC_UTIL_LIST(ADDR)
3081 NULL
3082 #undef ADDR
3083 };
3084
3085
3086 Address IC::AddressFromUtilityId(IC::UtilityId id) {
3087 return IC_utilities[id];
3088 }
3089
3090
3091 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ic.h ('k') | src/ic-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698