Rewrite StoreIC handling using the LookupIterator. Continued from patch 494153002
[platform/upstream/v8.git] / src / ic / x64 / ic-compiler-x64.cc
1 // Copyright 2014 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 #if V8_TARGET_ARCH_X64
8
9 #include "src/ic/ic-compiler.h"
10
11 namespace v8 {
12 namespace internal {
13
14 #define __ ACCESS_MASM(masm)
15
16
17 void PropertyHandlerCompiler::GenerateDictionaryNegativeLookup(
18     MacroAssembler* masm, Label* miss_label, Register receiver,
19     Handle<Name> name, Register scratch0, Register scratch1) {
20   DCHECK(name->IsUniqueName());
21   DCHECK(!receiver.is(scratch0));
22   Counters* counters = masm->isolate()->counters();
23   __ IncrementCounter(counters->negative_lookups(), 1);
24   __ IncrementCounter(counters->negative_lookups_miss(), 1);
25
26   __ movp(scratch0, FieldOperand(receiver, HeapObject::kMapOffset));
27
28   const int kInterceptorOrAccessCheckNeededMask =
29       (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
30
31   // Bail out if the receiver has a named interceptor or requires access checks.
32   __ testb(FieldOperand(scratch0, Map::kBitFieldOffset),
33            Immediate(kInterceptorOrAccessCheckNeededMask));
34   __ j(not_zero, miss_label);
35
36   // Check that receiver is a JSObject.
37   __ CmpInstanceType(scratch0, FIRST_SPEC_OBJECT_TYPE);
38   __ j(below, miss_label);
39
40   // Load properties array.
41   Register properties = scratch0;
42   __ movp(properties, FieldOperand(receiver, JSObject::kPropertiesOffset));
43
44   // Check that the properties array is a dictionary.
45   __ CompareRoot(FieldOperand(properties, HeapObject::kMapOffset),
46                  Heap::kHashTableMapRootIndex);
47   __ j(not_equal, miss_label);
48
49   Label done;
50   NameDictionaryLookupStub::GenerateNegativeLookup(masm, miss_label, &done,
51                                                    properties, name, scratch1);
52   __ bind(&done);
53   __ DecrementCounter(counters->negative_lookups_miss(), 1);
54 }
55
56
57 void NamedLoadHandlerCompiler::GenerateDirectLoadGlobalFunctionPrototype(
58     MacroAssembler* masm, int index, Register prototype, Label* miss) {
59   Isolate* isolate = masm->isolate();
60   // Get the global function with the given index.
61   Handle<JSFunction> function(
62       JSFunction::cast(isolate->native_context()->get(index)));
63
64   // Check we're still in the same context.
65   Register scratch = prototype;
66   const int offset = Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX);
67   __ movp(scratch, Operand(rsi, offset));
68   __ movp(scratch, FieldOperand(scratch, GlobalObject::kNativeContextOffset));
69   __ Cmp(Operand(scratch, Context::SlotOffset(index)), function);
70   __ j(not_equal, miss);
71
72   // Load its initial map. The global functions all have initial maps.
73   __ Move(prototype, Handle<Map>(function->initial_map()));
74   // Load the prototype from the initial map.
75   __ movp(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
76 }
77
78
79 void NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(
80     MacroAssembler* masm, Register receiver, Register result, Register scratch,
81     Label* miss_label) {
82   __ TryGetFunctionPrototype(receiver, result, miss_label);
83   if (!result.is(rax)) __ movp(rax, result);
84   __ ret(0);
85 }
86
87
88 static void PushInterceptorArguments(MacroAssembler* masm, Register receiver,
89                                      Register holder, Register name,
90                                      Handle<JSObject> holder_obj) {
91   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsNameIndex == 0);
92   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsInfoIndex == 1);
93   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsThisIndex == 2);
94   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsHolderIndex == 3);
95   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsLength == 4);
96   __ Push(name);
97   Handle<InterceptorInfo> interceptor(holder_obj->GetNamedInterceptor());
98   DCHECK(!masm->isolate()->heap()->InNewSpace(*interceptor));
99   __ Move(kScratchRegister, interceptor);
100   __ Push(kScratchRegister);
101   __ Push(receiver);
102   __ Push(holder);
103 }
104
105
106 static void CompileCallLoadPropertyWithInterceptor(
107     MacroAssembler* masm, Register receiver, Register holder, Register name,
108     Handle<JSObject> holder_obj, IC::UtilityId id) {
109   PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
110   __ CallExternalReference(ExternalReference(IC_Utility(id), masm->isolate()),
111                            NamedLoadHandlerCompiler::kInterceptorArgsLength);
112 }
113
114
115 // Generate call to api function.
116 void PropertyHandlerCompiler::GenerateFastApiCall(
117     MacroAssembler* masm, const CallOptimization& optimization,
118     Handle<Map> receiver_map, Register receiver, Register scratch_in,
119     bool is_store, int argc, Register* values) {
120   DCHECK(optimization.is_simple_api_call());
121
122   __ PopReturnAddressTo(scratch_in);
123   // receiver
124   __ Push(receiver);
125   // Write the arguments to stack frame.
126   for (int i = 0; i < argc; i++) {
127     Register arg = values[argc - 1 - i];
128     DCHECK(!receiver.is(arg));
129     DCHECK(!scratch_in.is(arg));
130     __ Push(arg);
131   }
132   __ PushReturnAddressFrom(scratch_in);
133   // Stack now matches JSFunction abi.
134
135   // Abi for CallApiFunctionStub.
136   Register callee = rax;
137   Register call_data = rbx;
138   Register holder = rcx;
139   Register api_function_address = rdx;
140   Register scratch = rdi;  // scratch_in is no longer valid.
141
142   // Put holder in place.
143   CallOptimization::HolderLookup holder_lookup;
144   Handle<JSObject> api_holder =
145       optimization.LookupHolderOfExpectedType(receiver_map, &holder_lookup);
146   switch (holder_lookup) {
147     case CallOptimization::kHolderIsReceiver:
148       __ Move(holder, receiver);
149       break;
150     case CallOptimization::kHolderFound:
151       __ Move(holder, api_holder);
152       break;
153     case CallOptimization::kHolderNotFound:
154       UNREACHABLE();
155       break;
156   }
157
158   Isolate* isolate = masm->isolate();
159   Handle<JSFunction> function = optimization.constant_function();
160   Handle<CallHandlerInfo> api_call_info = optimization.api_call_info();
161   Handle<Object> call_data_obj(api_call_info->data(), isolate);
162
163   // Put callee in place.
164   __ Move(callee, function);
165
166   bool call_data_undefined = false;
167   // Put call_data in place.
168   if (isolate->heap()->InNewSpace(*call_data_obj)) {
169     __ Move(scratch, api_call_info);
170     __ movp(call_data, FieldOperand(scratch, CallHandlerInfo::kDataOffset));
171   } else if (call_data_obj->IsUndefined()) {
172     call_data_undefined = true;
173     __ LoadRoot(call_data, Heap::kUndefinedValueRootIndex);
174   } else {
175     __ Move(call_data, call_data_obj);
176   }
177
178   // Put api_function_address in place.
179   Address function_address = v8::ToCData<Address>(api_call_info->callback());
180   __ Move(api_function_address, function_address,
181           RelocInfo::EXTERNAL_REFERENCE);
182
183   // Jump to stub.
184   CallApiFunctionStub stub(isolate, is_store, call_data_undefined, argc);
185   __ TailCallStub(&stub);
186 }
187
188
189 void PropertyHandlerCompiler::GenerateCheckPropertyCell(
190     MacroAssembler* masm, Handle<JSGlobalObject> global, Handle<Name> name,
191     Register scratch, Label* miss) {
192   Handle<PropertyCell> cell = JSGlobalObject::EnsurePropertyCell(global, name);
193   DCHECK(cell->value()->IsTheHole());
194   __ Move(scratch, cell);
195   __ Cmp(FieldOperand(scratch, Cell::kValueOffset),
196          masm->isolate()->factory()->the_hole_value());
197   __ j(not_equal, miss);
198 }
199
200
201 void PropertyAccessCompiler::GenerateTailCall(MacroAssembler* masm,
202                                               Handle<Code> code) {
203   __ jmp(code, RelocInfo::CODE_TARGET);
204 }
205
206
207 #undef __
208 #define __ ACCESS_MASM((masm()))
209
210
211 void NamedStoreHandlerCompiler::GenerateRestoreName(Label* label,
212                                                     Handle<Name> name) {
213   if (!label->is_unused()) {
214     __ bind(label);
215     __ Move(this->name(), name);
216   }
217 }
218
219
220 // Receiver_reg is preserved on jumps to miss_label, but may be destroyed if
221 // store is successful.
222 void NamedStoreHandlerCompiler::GenerateStoreTransition(
223     Handle<Map> transition, Handle<Name> name, Register receiver_reg,
224     Register storage_reg, Register value_reg, Register scratch1,
225     Register scratch2, Register unused, Label* miss_label, Label* slow) {
226   int descriptor = transition->LastAdded();
227   DescriptorArray* descriptors = transition->instance_descriptors();
228   PropertyDetails details = descriptors->GetDetails(descriptor);
229   Representation representation = details.representation();
230   DCHECK(!representation.IsNone());
231
232   if (details.type() == CONSTANT) {
233     Handle<Object> constant(descriptors->GetValue(descriptor), isolate());
234     __ Cmp(value_reg, constant);
235     __ j(not_equal, miss_label);
236   } else if (representation.IsSmi()) {
237     __ JumpIfNotSmi(value_reg, miss_label);
238   } else if (representation.IsHeapObject()) {
239     __ JumpIfSmi(value_reg, miss_label);
240     HeapType* field_type = descriptors->GetFieldType(descriptor);
241     HeapType::Iterator<Map> it = field_type->Classes();
242     if (!it.Done()) {
243       Label do_store;
244       while (true) {
245         __ CompareMap(value_reg, it.Current());
246         it.Advance();
247         if (it.Done()) {
248           __ j(not_equal, miss_label);
249           break;
250         }
251         __ j(equal, &do_store, Label::kNear);
252       }
253       __ bind(&do_store);
254     }
255   } else if (representation.IsDouble()) {
256     Label do_store, heap_number;
257     __ AllocateHeapNumber(storage_reg, scratch1, slow, MUTABLE);
258
259     __ JumpIfNotSmi(value_reg, &heap_number);
260     __ SmiToInteger32(scratch1, value_reg);
261     __ Cvtlsi2sd(xmm0, scratch1);
262     __ jmp(&do_store);
263
264     __ bind(&heap_number);
265     __ CheckMap(value_reg, isolate()->factory()->heap_number_map(), miss_label,
266                 DONT_DO_SMI_CHECK);
267     __ movsd(xmm0, FieldOperand(value_reg, HeapNumber::kValueOffset));
268
269     __ bind(&do_store);
270     __ movsd(FieldOperand(storage_reg, HeapNumber::kValueOffset), xmm0);
271   }
272
273   // Stub never generated for objects that require access checks.
274   DCHECK(!transition->is_access_check_needed());
275
276   // Perform map transition for the receiver if necessary.
277   if (details.type() == FIELD &&
278       Map::cast(transition->GetBackPointer())->unused_property_fields() == 0) {
279     // The properties must be extended before we can store the value.
280     // We jump to a runtime call that extends the properties array.
281     __ PopReturnAddressTo(scratch1);
282     __ Push(receiver_reg);
283     __ Push(transition);
284     __ Push(value_reg);
285     __ PushReturnAddressFrom(scratch1);
286     __ TailCallExternalReference(
287         ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage),
288                           isolate()),
289         3, 1);
290     return;
291   }
292
293   // Update the map of the object.
294   __ Move(scratch1, transition);
295   __ movp(FieldOperand(receiver_reg, HeapObject::kMapOffset), scratch1);
296
297   // Update the write barrier for the map field.
298   __ RecordWriteField(receiver_reg, HeapObject::kMapOffset, scratch1, scratch2,
299                       kDontSaveFPRegs, OMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
300
301   if (details.type() == CONSTANT) {
302     DCHECK(value_reg.is(rax));
303     __ ret(0);
304     return;
305   }
306
307   int index = transition->instance_descriptors()->GetFieldIndex(
308       transition->LastAdded());
309
310   // Adjust for the number of properties stored in the object. Even in the
311   // face of a transition we can use the old map here because the size of the
312   // object and the number of in-object properties is not going to change.
313   index -= transition->inobject_properties();
314
315   // TODO(verwaest): Share this code as a code stub.
316   SmiCheck smi_check =
317       representation.IsTagged() ? INLINE_SMI_CHECK : OMIT_SMI_CHECK;
318   if (index < 0) {
319     // Set the property straight into the object.
320     int offset = transition->instance_size() + (index * kPointerSize);
321     if (representation.IsDouble()) {
322       __ movp(FieldOperand(receiver_reg, offset), storage_reg);
323     } else {
324       __ movp(FieldOperand(receiver_reg, offset), value_reg);
325     }
326
327     if (!representation.IsSmi()) {
328       // Update the write barrier for the array address.
329       if (!representation.IsDouble()) {
330         __ movp(storage_reg, value_reg);
331       }
332       __ RecordWriteField(receiver_reg, offset, storage_reg, scratch1,
333                           kDontSaveFPRegs, EMIT_REMEMBERED_SET, smi_check);
334     }
335   } else {
336     // Write to the properties array.
337     int offset = index * kPointerSize + FixedArray::kHeaderSize;
338     // Get the properties array (optimistically).
339     __ movp(scratch1, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
340     if (representation.IsDouble()) {
341       __ movp(FieldOperand(scratch1, offset), storage_reg);
342     } else {
343       __ movp(FieldOperand(scratch1, offset), value_reg);
344     }
345
346     if (!representation.IsSmi()) {
347       // Update the write barrier for the array address.
348       if (!representation.IsDouble()) {
349         __ movp(storage_reg, value_reg);
350       }
351       __ RecordWriteField(scratch1, offset, storage_reg, receiver_reg,
352                           kDontSaveFPRegs, EMIT_REMEMBERED_SET, smi_check);
353     }
354   }
355
356   // Return the value (register rax).
357   DCHECK(value_reg.is(rax));
358   __ ret(0);
359 }
360
361
362 void NamedStoreHandlerCompiler::GenerateStoreField(LookupIterator* lookup,
363                                                    Register value_reg,
364                                                    Label* miss_label) {
365   DCHECK(lookup->representation().IsHeapObject());
366   __ JumpIfSmi(value_reg, miss_label);
367   HeapType::Iterator<Map> it = lookup->GetFieldType()->Classes();
368   Label do_store;
369   while (true) {
370     __ CompareMap(value_reg, it.Current());
371     it.Advance();
372     if (it.Done()) {
373       __ j(not_equal, miss_label);
374       break;
375     }
376     __ j(equal, &do_store, Label::kNear);
377   }
378   __ bind(&do_store);
379
380   StoreFieldStub stub(isolate(), lookup->GetFieldIndex(),
381                       lookup->representation());
382   GenerateTailCall(masm(), stub.GetCode());
383 }
384
385
386 Register PropertyHandlerCompiler::CheckPrototypes(
387     Register object_reg, Register holder_reg, Register scratch1,
388     Register scratch2, Handle<Name> name, Label* miss,
389     PrototypeCheckType check) {
390   Handle<Map> receiver_map(IC::TypeToMap(*type(), isolate()));
391
392   // Make sure there's no overlap between holder and object registers.
393   DCHECK(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
394   DCHECK(!scratch2.is(object_reg) && !scratch2.is(holder_reg) &&
395          !scratch2.is(scratch1));
396
397   // Keep track of the current object in register reg.  On the first
398   // iteration, reg is an alias for object_reg, on later iterations,
399   // it is an alias for holder_reg.
400   Register reg = object_reg;
401   int depth = 0;
402
403   Handle<JSObject> current = Handle<JSObject>::null();
404   if (type()->IsConstant()) {
405     current = Handle<JSObject>::cast(type()->AsConstant()->Value());
406   }
407   Handle<JSObject> prototype = Handle<JSObject>::null();
408   Handle<Map> current_map = receiver_map;
409   Handle<Map> holder_map(holder()->map());
410   // Traverse the prototype chain and check the maps in the prototype chain for
411   // fast and global objects or do negative lookup for normal objects.
412   while (!current_map.is_identical_to(holder_map)) {
413     ++depth;
414
415     // Only global objects and objects that do not require access
416     // checks are allowed in stubs.
417     DCHECK(current_map->IsJSGlobalProxyMap() ||
418            !current_map->is_access_check_needed());
419
420     prototype = handle(JSObject::cast(current_map->prototype()));
421     if (current_map->is_dictionary_map() &&
422         !current_map->IsJSGlobalObjectMap()) {
423       DCHECK(!current_map->IsJSGlobalProxyMap());  // Proxy maps are fast.
424       if (!name->IsUniqueName()) {
425         DCHECK(name->IsString());
426         name = factory()->InternalizeString(Handle<String>::cast(name));
427       }
428       DCHECK(current.is_null() ||
429              current->property_dictionary()->FindEntry(name) ==
430                  NameDictionary::kNotFound);
431
432       GenerateDictionaryNegativeLookup(masm(), miss, reg, name, scratch1,
433                                        scratch2);
434
435       __ movp(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
436       reg = holder_reg;  // From now on the object will be in holder_reg.
437       __ movp(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
438     } else {
439       bool in_new_space = heap()->InNewSpace(*prototype);
440       // Two possible reasons for loading the prototype from the map:
441       // (1) Can't store references to new space in code.
442       // (2) Handler is shared for all receivers with the same prototype
443       //     map (but not necessarily the same prototype instance).
444       bool load_prototype_from_map = in_new_space || depth == 1;
445       if (load_prototype_from_map) {
446         // Save the map in scratch1 for later.
447         __ movp(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
448       }
449       if (depth != 1 || check == CHECK_ALL_MAPS) {
450         __ CheckMap(reg, current_map, miss, DONT_DO_SMI_CHECK);
451       }
452
453       // Check access rights to the global object.  This has to happen after
454       // the map check so that we know that the object is actually a global
455       // object.
456       // This allows us to install generated handlers for accesses to the
457       // global proxy (as opposed to using slow ICs). See corresponding code
458       // in LookupForRead().
459       if (current_map->IsJSGlobalProxyMap()) {
460         __ CheckAccessGlobalProxy(reg, scratch2, miss);
461       } else if (current_map->IsJSGlobalObjectMap()) {
462         GenerateCheckPropertyCell(masm(), Handle<JSGlobalObject>::cast(current),
463                                   name, scratch2, miss);
464       }
465       reg = holder_reg;  // From now on the object will be in holder_reg.
466
467       if (load_prototype_from_map) {
468         __ movp(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
469       } else {
470         __ Move(reg, prototype);
471       }
472     }
473
474     // Go to the next object in the prototype chain.
475     current = prototype;
476     current_map = handle(current->map());
477   }
478
479   // Log the check depth.
480   LOG(isolate(), IntEvent("check-maps-depth", depth + 1));
481
482   if (depth != 0 || check == CHECK_ALL_MAPS) {
483     // Check the holder map.
484     __ CheckMap(reg, current_map, miss, DONT_DO_SMI_CHECK);
485   }
486
487   // Perform security check for access to the global object.
488   DCHECK(current_map->IsJSGlobalProxyMap() ||
489          !current_map->is_access_check_needed());
490   if (current_map->IsJSGlobalProxyMap()) {
491     __ CheckAccessGlobalProxy(reg, scratch1, miss);
492   }
493
494   // Return the register containing the holder.
495   return reg;
496 }
497
498
499 void NamedLoadHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
500   if (!miss->is_unused()) {
501     Label success;
502     __ jmp(&success);
503     __ bind(miss);
504     TailCallBuiltin(masm(), MissBuiltin(kind()));
505     __ bind(&success);
506   }
507 }
508
509
510 void NamedStoreHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
511   if (!miss->is_unused()) {
512     Label success;
513     __ jmp(&success);
514     GenerateRestoreName(miss, name);
515     TailCallBuiltin(masm(), MissBuiltin(kind()));
516     __ bind(&success);
517   }
518 }
519
520
521 void NamedLoadHandlerCompiler::GenerateLoadCallback(
522     Register reg, Handle<ExecutableAccessorInfo> callback) {
523   // Insert additional parameters into the stack frame above return address.
524   DCHECK(!scratch4().is(reg));
525   __ PopReturnAddressTo(scratch4());
526
527   STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 0);
528   STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 1);
529   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 2);
530   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 3);
531   STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 4);
532   STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 5);
533   STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 6);
534   __ Push(receiver());  // receiver
535   if (heap()->InNewSpace(callback->data())) {
536     DCHECK(!scratch2().is(reg));
537     __ Move(scratch2(), callback);
538     __ Push(FieldOperand(scratch2(),
539                          ExecutableAccessorInfo::kDataOffset));  // data
540   } else {
541     __ Push(Handle<Object>(callback->data(), isolate()));
542   }
543   DCHECK(!kScratchRegister.is(reg));
544   __ LoadRoot(kScratchRegister, Heap::kUndefinedValueRootIndex);
545   __ Push(kScratchRegister);  // return value
546   __ Push(kScratchRegister);  // return value default
547   __ PushAddress(ExternalReference::isolate_address(isolate()));
548   __ Push(reg);     // holder
549   __ Push(name());  // name
550   // Save a pointer to where we pushed the arguments pointer.  This will be
551   // passed as the const PropertyAccessorInfo& to the C++ callback.
552
553   __ PushReturnAddressFrom(scratch4());
554
555   // Abi for CallApiGetter
556   Register api_function_address = r8;
557   Address getter_address = v8::ToCData<Address>(callback->getter());
558   __ Move(api_function_address, getter_address, RelocInfo::EXTERNAL_REFERENCE);
559
560   CallApiGetterStub stub(isolate());
561   __ TailCallStub(&stub);
562 }
563
564
565 void NamedLoadHandlerCompiler::GenerateLoadConstant(Handle<Object> value) {
566   // Return the constant value.
567   __ Move(rax, value);
568   __ ret(0);
569 }
570
571
572 void NamedLoadHandlerCompiler::GenerateLoadInterceptorWithFollowup(
573     LookupIterator* it, Register holder_reg) {
574   DCHECK(holder()->HasNamedInterceptor());
575   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
576
577   // Compile the interceptor call, followed by inline code to load the
578   // property from further up the prototype chain if the call fails.
579   // Check that the maps haven't changed.
580   DCHECK(holder_reg.is(receiver()) || holder_reg.is(scratch1()));
581
582   // Preserve the receiver register explicitly whenever it is different from the
583   // holder and it is needed should the interceptor return without any result.
584   // The ACCESSOR case needs the receiver to be passed into C++ code, the FIELD
585   // case might cause a miss during the prototype check.
586   bool must_perform_prototype_check =
587       !holder().is_identical_to(it->GetHolder<JSObject>());
588   bool must_preserve_receiver_reg =
589       !receiver().is(holder_reg) &&
590       (it->property_kind() == LookupIterator::ACCESSOR ||
591        must_perform_prototype_check);
592
593   // Save necessary data before invoking an interceptor.
594   // Requires a frame to make GC aware of pushed pointers.
595   {
596     FrameScope frame_scope(masm(), StackFrame::INTERNAL);
597
598     if (must_preserve_receiver_reg) {
599       __ Push(receiver());
600     }
601     __ Push(holder_reg);
602     __ Push(this->name());
603
604     // Invoke an interceptor.  Note: map checks from receiver to
605     // interceptor's holder has been compiled before (see a caller
606     // of this method.)
607     CompileCallLoadPropertyWithInterceptor(
608         masm(), receiver(), holder_reg, this->name(), holder(),
609         IC::kLoadPropertyWithInterceptorOnly);
610
611     // Check if interceptor provided a value for property.  If it's
612     // the case, return immediately.
613     Label interceptor_failed;
614     __ CompareRoot(rax, Heap::kNoInterceptorResultSentinelRootIndex);
615     __ j(equal, &interceptor_failed);
616     frame_scope.GenerateLeaveFrame();
617     __ ret(0);
618
619     __ bind(&interceptor_failed);
620     __ Pop(this->name());
621     __ Pop(holder_reg);
622     if (must_preserve_receiver_reg) {
623       __ Pop(receiver());
624     }
625
626     // Leave the internal frame.
627   }
628
629   GenerateLoadPostInterceptor(it, holder_reg);
630 }
631
632
633 void NamedLoadHandlerCompiler::GenerateLoadInterceptor(Register holder_reg) {
634   // Call the runtime system to load the interceptor.
635   DCHECK(holder()->HasNamedInterceptor());
636   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
637   __ PopReturnAddressTo(scratch2());
638   PushInterceptorArguments(masm(), receiver(), holder_reg, this->name(),
639                            holder());
640   __ PushReturnAddressFrom(scratch2());
641
642   ExternalReference ref = ExternalReference(
643       IC_Utility(IC::kLoadPropertyWithInterceptor), isolate());
644   __ TailCallExternalReference(
645       ref, NamedLoadHandlerCompiler::kInterceptorArgsLength, 1);
646 }
647
648
649 Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback(
650     Handle<JSObject> object, Handle<Name> name,
651     Handle<ExecutableAccessorInfo> callback) {
652   Register holder_reg = Frontend(receiver(), name);
653
654   __ PopReturnAddressTo(scratch1());
655   __ Push(receiver());
656   __ Push(holder_reg);
657   __ Push(callback);  // callback info
658   __ Push(name);
659   __ Push(value());
660   __ PushReturnAddressFrom(scratch1());
661
662   // Do tail-call to the runtime system.
663   ExternalReference store_callback_property =
664       ExternalReference(IC_Utility(IC::kStoreCallbackProperty), isolate());
665   __ TailCallExternalReference(store_callback_property, 5, 1);
666
667   // Return the generated code.
668   return GetCode(kind(), Code::FAST, name);
669 }
670
671
672 #undef __
673 #define __ ACCESS_MASM(masm)
674
675
676 void NamedStoreHandlerCompiler::GenerateStoreViaSetter(
677     MacroAssembler* masm, Handle<HeapType> type, Register receiver,
678     Handle<JSFunction> setter) {
679   // ----------- S t a t e -------------
680   //  -- rsp[0] : return address
681   // -----------------------------------
682   {
683     FrameScope scope(masm, StackFrame::INTERNAL);
684
685     // Save value register, so we can restore it later.
686     __ Push(value());
687
688     if (!setter.is_null()) {
689       // Call the JavaScript setter with receiver and value on the stack.
690       if (IC::TypeToMap(*type, masm->isolate())->IsJSGlobalObjectMap()) {
691         // Swap in the global receiver.
692         __ movp(receiver,
693                 FieldOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
694       }
695       __ Push(receiver);
696       __ Push(value());
697       ParameterCount actual(1);
698       ParameterCount expected(setter);
699       __ InvokeFunction(setter, expected, actual, CALL_FUNCTION,
700                         NullCallWrapper());
701     } else {
702       // If we generate a global code snippet for deoptimization only, remember
703       // the place to continue after deoptimization.
704       masm->isolate()->heap()->SetSetterStubDeoptPCOffset(masm->pc_offset());
705     }
706
707     // We have to return the passed value, not the return value of the setter.
708     __ Pop(rax);
709
710     // Restore context register.
711     __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
712   }
713   __ ret(0);
714 }
715
716
717 #undef __
718 #define __ ACCESS_MASM(masm())
719
720
721 Handle<Code> NamedStoreHandlerCompiler::CompileStoreInterceptor(
722     Handle<Name> name) {
723   __ PopReturnAddressTo(scratch1());
724   __ Push(receiver());
725   __ Push(this->name());
726   __ Push(value());
727   __ PushReturnAddressFrom(scratch1());
728
729   // Do tail-call to the runtime system.
730   ExternalReference store_ic_property = ExternalReference(
731       IC_Utility(IC::kStorePropertyWithInterceptor), isolate());
732   __ TailCallExternalReference(store_ic_property, 3, 1);
733
734   // Return the generated code.
735   return GetCode(kind(), Code::FAST, name);
736 }
737
738
739 Handle<Code> PropertyICCompiler::CompileKeyedStorePolymorphic(
740     MapHandleList* receiver_maps, CodeHandleList* handler_stubs,
741     MapHandleList* transitioned_maps) {
742   Label miss;
743   __ JumpIfSmi(receiver(), &miss, Label::kNear);
744
745   __ movp(scratch1(), FieldOperand(receiver(), HeapObject::kMapOffset));
746   int receiver_count = receiver_maps->length();
747   for (int i = 0; i < receiver_count; ++i) {
748     // Check map and tail call if there's a match
749     __ Cmp(scratch1(), receiver_maps->at(i));
750     if (transitioned_maps->at(i).is_null()) {
751       __ j(equal, handler_stubs->at(i), RelocInfo::CODE_TARGET);
752     } else {
753       Label next_map;
754       __ j(not_equal, &next_map, Label::kNear);
755       __ Move(transition_map(), transitioned_maps->at(i),
756               RelocInfo::EMBEDDED_OBJECT);
757       __ jmp(handler_stubs->at(i), RelocInfo::CODE_TARGET);
758       __ bind(&next_map);
759     }
760   }
761
762   __ bind(&miss);
763
764   TailCallBuiltin(masm(), MissBuiltin(kind()));
765
766   // Return the generated code.
767   return GetCode(kind(), Code::NORMAL, factory()->empty_string(), POLYMORPHIC);
768 }
769
770
771 Register* PropertyAccessCompiler::load_calling_convention() {
772   // receiver, name, scratch1, scratch2, scratch3, scratch4.
773   Register receiver = LoadIC::ReceiverRegister();
774   Register name = LoadIC::NameRegister();
775   static Register registers[] = {receiver, name, rax, rbx, rdi, r8};
776   return registers;
777 }
778
779
780 Register* PropertyAccessCompiler::store_calling_convention() {
781   // receiver, name, scratch1, scratch2, scratch3.
782   Register receiver = KeyedStoreIC::ReceiverRegister();
783   Register name = KeyedStoreIC::NameRegister();
784   DCHECK(rbx.is(KeyedStoreIC::MapRegister()));
785   static Register registers[] = {receiver, name, rbx, rdi, r8};
786   return registers;
787 }
788
789
790 Register NamedStoreHandlerCompiler::value() { return StoreIC::ValueRegister(); }
791
792
793 #undef __
794 #define __ ACCESS_MASM(masm)
795
796
797 void NamedLoadHandlerCompiler::GenerateLoadViaGetter(
798     MacroAssembler* masm, Handle<HeapType> type, Register receiver,
799     Handle<JSFunction> getter) {
800   // ----------- S t a t e -------------
801   //  -- rax    : receiver
802   //  -- rcx    : name
803   //  -- rsp[0] : return address
804   // -----------------------------------
805   {
806     FrameScope scope(masm, StackFrame::INTERNAL);
807
808     if (!getter.is_null()) {
809       // Call the JavaScript getter with the receiver on the stack.
810       if (IC::TypeToMap(*type, masm->isolate())->IsJSGlobalObjectMap()) {
811         // Swap in the global receiver.
812         __ movp(receiver,
813                 FieldOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
814       }
815       __ Push(receiver);
816       ParameterCount actual(0);
817       ParameterCount expected(getter);
818       __ InvokeFunction(getter, expected, actual, CALL_FUNCTION,
819                         NullCallWrapper());
820     } else {
821       // If we generate a global code snippet for deoptimization only, remember
822       // the place to continue after deoptimization.
823       masm->isolate()->heap()->SetGetterStubDeoptPCOffset(masm->pc_offset());
824     }
825
826     // Restore context register.
827     __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
828   }
829   __ ret(0);
830 }
831
832
833 #undef __
834 #define __ ACCESS_MASM(masm())
835
836
837 Handle<Code> NamedLoadHandlerCompiler::CompileLoadGlobal(
838     Handle<PropertyCell> cell, Handle<Name> name, bool is_configurable) {
839   Label miss;
840   FrontendHeader(receiver(), name, &miss);
841
842   // Get the value from the cell.
843   Register result = StoreIC::ValueRegister();
844   __ Move(result, cell);
845   __ movp(result, FieldOperand(result, PropertyCell::kValueOffset));
846
847   // Check for deleted property if property can actually be deleted.
848   if (is_configurable) {
849     __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
850     __ j(equal, &miss);
851   } else if (FLAG_debug_code) {
852     __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
853     __ Check(not_equal, kDontDeleteCellsCannotContainTheHole);
854   }
855
856   Counters* counters = isolate()->counters();
857   __ IncrementCounter(counters->named_load_global_stub(), 1);
858   __ ret(0);
859
860   FrontendFooter(name, &miss);
861
862   // Return the generated code.
863   return GetCode(kind(), Code::NORMAL, name);
864 }
865
866
867 Handle<Code> PropertyICCompiler::CompilePolymorphic(TypeHandleList* types,
868                                                     CodeHandleList* handlers,
869                                                     Handle<Name> name,
870                                                     Code::StubType type,
871                                                     IcCheckType check) {
872   Label miss;
873
874   if (check == PROPERTY &&
875       (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC)) {
876     // In case we are compiling an IC for dictionary loads and stores, just
877     // check whether the name is unique.
878     if (name.is_identical_to(isolate()->factory()->normal_ic_symbol())) {
879       __ JumpIfNotUniqueName(this->name(), &miss);
880     } else {
881       __ Cmp(this->name(), name);
882       __ j(not_equal, &miss);
883     }
884   }
885
886   Label number_case;
887   Label* smi_target = IncludesNumberType(types) ? &number_case : &miss;
888   __ JumpIfSmi(receiver(), smi_target);
889
890   // Polymorphic keyed stores may use the map register
891   Register map_reg = scratch1();
892   DCHECK(kind() != Code::KEYED_STORE_IC ||
893          map_reg.is(KeyedStoreIC::MapRegister()));
894   __ movp(map_reg, FieldOperand(receiver(), HeapObject::kMapOffset));
895   int receiver_count = types->length();
896   int number_of_handled_maps = 0;
897   for (int current = 0; current < receiver_count; ++current) {
898     Handle<HeapType> type = types->at(current);
899     Handle<Map> map = IC::TypeToMap(*type, isolate());
900     if (!map->is_deprecated()) {
901       number_of_handled_maps++;
902       // Check map and tail call if there's a match
903       __ Cmp(map_reg, map);
904       if (type->Is(HeapType::Number())) {
905         DCHECK(!number_case.is_unused());
906         __ bind(&number_case);
907       }
908       __ j(equal, handlers->at(current), RelocInfo::CODE_TARGET);
909     }
910   }
911   DCHECK(number_of_handled_maps > 0);
912
913   __ bind(&miss);
914   TailCallBuiltin(masm(), MissBuiltin(kind()));
915
916   // Return the generated code.
917   InlineCacheState state =
918       number_of_handled_maps > 1 ? POLYMORPHIC : MONOMORPHIC;
919   return GetCode(kind(), type, name, state);
920 }
921
922
923 #undef __
924 #define __ ACCESS_MASM(masm)
925
926
927 void ElementHandlerCompiler::GenerateLoadDictionaryElement(
928     MacroAssembler* masm) {
929   // ----------- S t a t e -------------
930   //  -- rcx    : key
931   //  -- rdx    : receiver
932   //  -- rsp[0] : return address
933   // -----------------------------------
934   DCHECK(rdx.is(LoadIC::ReceiverRegister()));
935   DCHECK(rcx.is(LoadIC::NameRegister()));
936   Label slow, miss;
937
938   // This stub is meant to be tail-jumped to, the receiver must already
939   // have been verified by the caller to not be a smi.
940
941   __ JumpIfNotSmi(rcx, &miss);
942   __ SmiToInteger32(rbx, rcx);
943   __ movp(rax, FieldOperand(rdx, JSObject::kElementsOffset));
944
945   // Check whether the elements is a number dictionary.
946   // rdx: receiver
947   // rcx: key
948   // rbx: key as untagged int32
949   // rax: elements
950   __ LoadFromNumberDictionary(&slow, rax, rcx, rbx, r9, rdi, rax);
951   __ ret(0);
952
953   __ bind(&slow);
954   // ----------- S t a t e -------------
955   //  -- rcx    : key
956   //  -- rdx    : receiver
957   //  -- rsp[0] : return address
958   // -----------------------------------
959   TailCallBuiltin(masm, Builtins::kKeyedLoadIC_Slow);
960
961   __ bind(&miss);
962   // ----------- S t a t e -------------
963   //  -- rcx    : key
964   //  -- rdx    : receiver
965   //  -- rsp[0] : return address
966   // -----------------------------------
967   TailCallBuiltin(masm, Builtins::kKeyedLoadIC_Miss);
968 }
969
970
971 #undef __
972 }
973 }  // namespace v8::internal
974
975 #endif  // V8_TARGET_ARCH_X64