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