deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / ic / x87 / handler-compiler-x87.cc
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 #if V8_TARGET_ARCH_X87
8
9 #include "src/ic/call-optimization.h"
10 #include "src/ic/handler-compiler.h"
11 #include "src/ic/ic.h"
12
13 namespace v8 {
14 namespace internal {
15
16 #define __ ACCESS_MASM(masm)
17
18
19 void NamedLoadHandlerCompiler::GenerateLoadViaGetter(
20     MacroAssembler* masm, Handle<Map> map, Register receiver, Register holder,
21     int accessor_index, int expected_arguments, Register scratch) {
22   {
23     FrameScope scope(masm, StackFrame::INTERNAL);
24
25     if (accessor_index >= 0) {
26       DCHECK(!holder.is(scratch));
27       DCHECK(!receiver.is(scratch));
28       // Call the JavaScript getter with the receiver on the stack.
29       if (map->IsJSGlobalObjectMap()) {
30         // Swap in the global receiver.
31         __ mov(scratch,
32                FieldOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
33         receiver = scratch;
34       }
35       __ push(receiver);
36       ParameterCount actual(0);
37       ParameterCount expected(expected_arguments);
38       __ LoadAccessor(edi, holder, accessor_index, ACCESSOR_GETTER);
39       __ InvokeFunction(edi, expected, actual, CALL_FUNCTION,
40                         NullCallWrapper());
41     } else {
42       // If we generate a global code snippet for deoptimization only, remember
43       // the place to continue after deoptimization.
44       masm->isolate()->heap()->SetGetterStubDeoptPCOffset(masm->pc_offset());
45     }
46
47     // Restore context register.
48     __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
49   }
50   __ ret(0);
51 }
52
53
54 void PropertyHandlerCompiler::PushVectorAndSlot(Register vector,
55                                                 Register slot) {
56   MacroAssembler* masm = this->masm();
57   __ push(vector);
58   __ push(slot);
59 }
60
61
62 void PropertyHandlerCompiler::PopVectorAndSlot(Register vector, Register slot) {
63   MacroAssembler* masm = this->masm();
64   __ pop(slot);
65   __ pop(vector);
66 }
67
68
69 void PropertyHandlerCompiler::DiscardVectorAndSlot() {
70   MacroAssembler* masm = this->masm();
71   // Remove vector and slot.
72   __ add(esp, Immediate(2 * kPointerSize));
73 }
74
75
76 void PropertyHandlerCompiler::GenerateDictionaryNegativeLookup(
77     MacroAssembler* masm, Label* miss_label, Register receiver,
78     Handle<Name> name, Register scratch0, Register scratch1) {
79   DCHECK(name->IsUniqueName());
80   DCHECK(!receiver.is(scratch0));
81   Counters* counters = masm->isolate()->counters();
82   __ IncrementCounter(counters->negative_lookups(), 1);
83   __ IncrementCounter(counters->negative_lookups_miss(), 1);
84
85   __ mov(scratch0, FieldOperand(receiver, HeapObject::kMapOffset));
86
87   const int kInterceptorOrAccessCheckNeededMask =
88       (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
89
90   // Bail out if the receiver has a named interceptor or requires access checks.
91   __ test_b(FieldOperand(scratch0, Map::kBitFieldOffset),
92             kInterceptorOrAccessCheckNeededMask);
93   __ j(not_zero, miss_label);
94
95   // Check that receiver is a JSObject.
96   __ CmpInstanceType(scratch0, FIRST_SPEC_OBJECT_TYPE);
97   __ j(below, miss_label);
98
99   // Load properties array.
100   Register properties = scratch0;
101   __ mov(properties, FieldOperand(receiver, JSObject::kPropertiesOffset));
102
103   // Check that the properties array is a dictionary.
104   __ cmp(FieldOperand(properties, HeapObject::kMapOffset),
105          Immediate(masm->isolate()->factory()->hash_table_map()));
106   __ j(not_equal, miss_label);
107
108   Label done;
109   NameDictionaryLookupStub::GenerateNegativeLookup(masm, miss_label, &done,
110                                                    properties, name, scratch1);
111   __ bind(&done);
112   __ DecrementCounter(counters->negative_lookups_miss(), 1);
113 }
114
115
116 void NamedLoadHandlerCompiler::GenerateDirectLoadGlobalFunctionPrototype(
117     MacroAssembler* masm, int index, Register result, Label* miss) {
118   const int offset = Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX);
119   __ mov(result, Operand(esi, offset));
120   __ mov(result, FieldOperand(result, GlobalObject::kNativeContextOffset));
121   __ mov(result, Operand(result, Context::SlotOffset(index)));
122   // Load its initial map. The global functions all have initial maps.
123   __ mov(result,
124          FieldOperand(result, JSFunction::kPrototypeOrInitialMapOffset));
125   // Load the prototype from the initial map.
126   __ mov(result, FieldOperand(result, Map::kPrototypeOffset));
127 }
128
129
130 void NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(
131     MacroAssembler* masm, Register receiver, Register scratch1,
132     Register scratch2, Label* miss_label) {
133   DCHECK(!FLAG_vector_ics);
134   __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
135   __ mov(eax, scratch1);
136   __ ret(0);
137 }
138
139
140 // Generate call to api function.
141 // This function uses push() to generate smaller, faster code than
142 // the version above. It is an optimization that should will be removed
143 // when api call ICs are generated in hydrogen.
144 void PropertyHandlerCompiler::GenerateApiAccessorCall(
145     MacroAssembler* masm, const CallOptimization& optimization,
146     Handle<Map> receiver_map, Register receiver, Register scratch,
147     bool is_store, Register store_parameter, Register accessor_holder,
148     int accessor_index) {
149   DCHECK(!accessor_holder.is(scratch));
150   // Copy return value.
151   __ pop(scratch);
152   // receiver
153   __ push(receiver);
154   // Write the arguments to stack frame.
155   if (is_store) {
156     DCHECK(!receiver.is(store_parameter));
157     DCHECK(!scratch.is(store_parameter));
158     __ push(store_parameter);
159   }
160   __ push(scratch);
161   // Stack now matches JSFunction abi.
162   DCHECK(optimization.is_simple_api_call());
163
164   // Abi for CallApiFunctionStub.
165   Register callee = edi;
166   Register data = ebx;
167   Register holder = ecx;
168   Register api_function_address = edx;
169   scratch = no_reg;
170
171   // Put callee in place.
172   __ LoadAccessor(callee, accessor_holder, accessor_index,
173                   is_store ? ACCESSOR_SETTER : ACCESSOR_GETTER);
174
175   // Put holder in place.
176   CallOptimization::HolderLookup holder_lookup;
177   int holder_depth = 0;
178   optimization.LookupHolderOfExpectedType(receiver_map, &holder_lookup,
179                                           &holder_depth);
180   switch (holder_lookup) {
181     case CallOptimization::kHolderIsReceiver:
182       __ Move(holder, receiver);
183       break;
184     case CallOptimization::kHolderFound:
185       __ mov(holder, FieldOperand(receiver, HeapObject::kMapOffset));
186       __ mov(holder, FieldOperand(holder, Map::kPrototypeOffset));
187       for (int i = 1; i < holder_depth; i++) {
188         __ mov(holder, FieldOperand(holder, HeapObject::kMapOffset));
189         __ mov(holder, FieldOperand(holder, Map::kPrototypeOffset));
190       }
191       break;
192     case CallOptimization::kHolderNotFound:
193       UNREACHABLE();
194       break;
195   }
196
197   Isolate* isolate = masm->isolate();
198   Handle<CallHandlerInfo> api_call_info = optimization.api_call_info();
199   bool call_data_undefined = false;
200   // Put call data in place.
201   if (api_call_info->data()->IsUndefined()) {
202     call_data_undefined = true;
203     __ mov(data, Immediate(isolate->factory()->undefined_value()));
204   } else {
205     __ mov(data, FieldOperand(callee, JSFunction::kSharedFunctionInfoOffset));
206     __ mov(data, FieldOperand(data, SharedFunctionInfo::kFunctionDataOffset));
207     __ mov(data, FieldOperand(data, FunctionTemplateInfo::kCallCodeOffset));
208     __ mov(data, FieldOperand(data, CallHandlerInfo::kDataOffset));
209   }
210
211   // Put api_function_address in place.
212   Address function_address = v8::ToCData<Address>(api_call_info->callback());
213   __ mov(api_function_address, Immediate(function_address));
214
215   // Jump to stub.
216   CallApiAccessorStub stub(isolate, is_store, call_data_undefined);
217   __ TailCallStub(&stub);
218 }
219
220
221 // Generate code to check that a global property cell is empty. Create
222 // the property cell at compilation time if no cell exists for the
223 // property.
224 void PropertyHandlerCompiler::GenerateCheckPropertyCell(
225     MacroAssembler* masm, Handle<JSGlobalObject> global, Handle<Name> name,
226     Register scratch, Label* miss) {
227   Handle<PropertyCell> cell = JSGlobalObject::EnsurePropertyCell(global, name);
228   DCHECK(cell->value()->IsTheHole());
229   Factory* factory = masm->isolate()->factory();
230   Handle<WeakCell> weak_cell = factory->NewWeakCell(cell);
231   __ LoadWeakValue(scratch, weak_cell, miss);
232   __ cmp(FieldOperand(scratch, PropertyCell::kValueOffset),
233          Immediate(factory->the_hole_value()));
234   __ j(not_equal, miss);
235 }
236
237
238 void NamedStoreHandlerCompiler::GenerateStoreViaSetter(
239     MacroAssembler* masm, Handle<Map> map, Register receiver, Register holder,
240     int accessor_index, int expected_arguments, Register scratch) {
241   // ----------- S t a t e -------------
242   //  -- esp[0] : return address
243   // -----------------------------------
244   {
245     FrameScope scope(masm, StackFrame::INTERNAL);
246
247     // Save value register, so we can restore it later.
248     __ push(value());
249
250     if (accessor_index >= 0) {
251       DCHECK(!holder.is(scratch));
252       DCHECK(!receiver.is(scratch));
253       DCHECK(!value().is(scratch));
254       // Call the JavaScript setter with receiver and value on the stack.
255       if (map->IsJSGlobalObjectMap()) {
256         __ mov(scratch,
257                FieldOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
258         receiver = scratch;
259       }
260       __ push(receiver);
261       __ push(value());
262       ParameterCount actual(1);
263       ParameterCount expected(expected_arguments);
264       __ LoadAccessor(edi, holder, accessor_index, ACCESSOR_SETTER);
265       __ InvokeFunction(edi, expected, actual, CALL_FUNCTION,
266                         NullCallWrapper());
267     } else {
268       // If we generate a global code snippet for deoptimization only, remember
269       // the place to continue after deoptimization.
270       masm->isolate()->heap()->SetSetterStubDeoptPCOffset(masm->pc_offset());
271     }
272
273     // We have to return the passed value, not the return value of the setter.
274     __ pop(eax);
275
276     // Restore context register.
277     __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
278   }
279   __ ret(0);
280 }
281
282
283 static void PushInterceptorArguments(MacroAssembler* masm, Register receiver,
284                                      Register holder, Register name,
285                                      Handle<JSObject> holder_obj) {
286   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsNameIndex == 0);
287   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsThisIndex == 1);
288   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsHolderIndex == 2);
289   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsLength == 3);
290   __ push(name);
291   __ push(receiver);
292   __ push(holder);
293 }
294
295
296 static void CompileCallLoadPropertyWithInterceptor(
297     MacroAssembler* masm, Register receiver, Register holder, Register name,
298     Handle<JSObject> holder_obj, IC::UtilityId id) {
299   PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
300   __ CallExternalReference(ExternalReference(IC_Utility(id), masm->isolate()),
301                            NamedLoadHandlerCompiler::kInterceptorArgsLength);
302 }
303
304
305 static void StoreIC_PushArgs(MacroAssembler* masm) {
306   Register receiver = StoreDescriptor::ReceiverRegister();
307   Register name = StoreDescriptor::NameRegister();
308   Register value = StoreDescriptor::ValueRegister();
309
310   DCHECK(!ebx.is(receiver) && !ebx.is(name) && !ebx.is(value));
311
312   __ pop(ebx);
313   __ push(receiver);
314   __ push(name);
315   __ push(value);
316   __ push(ebx);
317 }
318
319
320 void NamedStoreHandlerCompiler::GenerateSlow(MacroAssembler* masm) {
321   // Return address is on the stack.
322   StoreIC_PushArgs(masm);
323
324   // Do tail-call to runtime routine.
325   ExternalReference ref(IC_Utility(IC::kStoreIC_Slow), masm->isolate());
326   __ TailCallExternalReference(ref, 3, 1);
327 }
328
329
330 void ElementHandlerCompiler::GenerateStoreSlow(MacroAssembler* masm) {
331   // Return address is on the stack.
332   StoreIC_PushArgs(masm);
333
334   // Do tail-call to runtime routine.
335   ExternalReference ref(IC_Utility(IC::kKeyedStoreIC_Slow), masm->isolate());
336   __ TailCallExternalReference(ref, 3, 1);
337 }
338
339
340 #undef __
341 #define __ ACCESS_MASM(masm())
342
343
344 void NamedStoreHandlerCompiler::GenerateRestoreName(Label* label,
345                                                     Handle<Name> name) {
346   if (!label->is_unused()) {
347     __ bind(label);
348     __ mov(this->name(), Immediate(name));
349   }
350 }
351
352
353 void NamedStoreHandlerCompiler::GenerateRestoreName(Handle<Name> name) {
354   __ mov(this->name(), Immediate(name));
355 }
356
357
358 void NamedStoreHandlerCompiler::GenerateRestoreMap(Handle<Map> transition,
359                                                    Register scratch,
360                                                    Label* miss) {
361   Handle<WeakCell> cell = Map::WeakCellForMap(transition);
362   Register map_reg = StoreTransitionDescriptor::MapRegister();
363   DCHECK(!map_reg.is(scratch));
364   __ LoadWeakValue(map_reg, cell, miss);
365   if (transition->CanBeDeprecated()) {
366     __ mov(scratch, FieldOperand(map_reg, Map::kBitField3Offset));
367     __ and_(scratch, Immediate(Map::Deprecated::kMask));
368     __ j(not_zero, miss);
369   }
370 }
371
372
373 void NamedStoreHandlerCompiler::GenerateConstantCheck(Register map_reg,
374                                                       int descriptor,
375                                                       Register value_reg,
376                                                       Register scratch,
377                                                       Label* miss_label) {
378   DCHECK(!map_reg.is(scratch));
379   DCHECK(!map_reg.is(value_reg));
380   DCHECK(!value_reg.is(scratch));
381   __ LoadInstanceDescriptors(map_reg, scratch);
382   __ mov(scratch,
383          FieldOperand(scratch, DescriptorArray::GetValueOffset(descriptor)));
384   __ cmp(value_reg, scratch);
385   __ j(not_equal, miss_label);
386 }
387
388
389 void NamedStoreHandlerCompiler::GenerateFieldTypeChecks(HeapType* field_type,
390                                                         Register value_reg,
391                                                         Label* miss_label) {
392   Register map_reg = scratch1();
393   Register scratch = scratch2();
394   DCHECK(!value_reg.is(map_reg));
395   DCHECK(!value_reg.is(scratch));
396   __ JumpIfSmi(value_reg, miss_label);
397   HeapType::Iterator<Map> it = field_type->Classes();
398   if (!it.Done()) {
399     Label do_store;
400     __ mov(map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
401     while (true) {
402       __ CmpWeakValue(map_reg, Map::WeakCellForMap(it.Current()), scratch);
403       it.Advance();
404       if (it.Done()) {
405         __ j(not_equal, miss_label);
406         break;
407       }
408       __ j(equal, &do_store, Label::kNear);
409     }
410     __ bind(&do_store);
411   }
412 }
413
414
415 Register PropertyHandlerCompiler::CheckPrototypes(
416     Register object_reg, Register holder_reg, Register scratch1,
417     Register scratch2, Handle<Name> name, Label* miss,
418     PrototypeCheckType check) {
419   Handle<Map> receiver_map = map();
420
421   // Make sure there's no overlap between holder and object registers.
422   DCHECK(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
423   DCHECK(!scratch2.is(object_reg) && !scratch2.is(holder_reg) &&
424          !scratch2.is(scratch1));
425
426   // Keep track of the current object in register reg.
427   Register reg = object_reg;
428   int depth = 0;
429
430   Handle<JSObject> current = Handle<JSObject>::null();
431   if (receiver_map->IsJSGlobalObjectMap()) {
432     current = isolate()->global_object();
433   }
434
435   // Check access rights to the global object.  This has to happen after
436   // the map check so that we know that the object is actually a global
437   // object.
438   // This allows us to install generated handlers for accesses to the
439   // global proxy (as opposed to using slow ICs). See corresponding code
440   // in LookupForRead().
441   if (receiver_map->IsJSGlobalProxyMap()) {
442     __ CheckAccessGlobalProxy(reg, scratch1, scratch2, miss);
443   }
444
445   Handle<JSObject> prototype = Handle<JSObject>::null();
446   Handle<Map> current_map = receiver_map;
447   Handle<Map> holder_map(holder()->map());
448   // Traverse the prototype chain and check the maps in the prototype chain for
449   // fast and global objects or do negative lookup for normal objects.
450   while (!current_map.is_identical_to(holder_map)) {
451     ++depth;
452
453     // Only global objects and objects that do not require access
454     // checks are allowed in stubs.
455     DCHECK(current_map->IsJSGlobalProxyMap() ||
456            !current_map->is_access_check_needed());
457
458     prototype = handle(JSObject::cast(current_map->prototype()));
459     if (current_map->is_dictionary_map() &&
460         !current_map->IsJSGlobalObjectMap()) {
461       DCHECK(!current_map->IsJSGlobalProxyMap());  // Proxy maps are fast.
462       if (!name->IsUniqueName()) {
463         DCHECK(name->IsString());
464         name = factory()->InternalizeString(Handle<String>::cast(name));
465       }
466       DCHECK(current.is_null() ||
467              current->property_dictionary()->FindEntry(name) ==
468                  NameDictionary::kNotFound);
469
470       GenerateDictionaryNegativeLookup(masm(), miss, reg, name, scratch1,
471                                        scratch2);
472
473       __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
474       reg = holder_reg;  // From now on the object will be in holder_reg.
475       __ mov(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
476     } else {
477       Register map_reg = scratch1;
478       __ mov(map_reg, FieldOperand(reg, HeapObject::kMapOffset));
479       if (current_map->IsJSGlobalObjectMap()) {
480         GenerateCheckPropertyCell(masm(), Handle<JSGlobalObject>::cast(current),
481                                   name, scratch2, miss);
482       } else if (depth != 1 || check == CHECK_ALL_MAPS) {
483         Handle<WeakCell> cell = Map::WeakCellForMap(current_map);
484         __ CmpWeakValue(map_reg, cell, scratch2);
485         __ j(not_equal, miss);
486       }
487
488       reg = holder_reg;  // From now on the object will be in holder_reg.
489       __ mov(reg, FieldOperand(map_reg, Map::kPrototypeOffset));
490     }
491
492     // Go to the next object in the prototype chain.
493     current = prototype;
494     current_map = handle(current->map());
495   }
496
497   DCHECK(!current_map->IsJSGlobalProxyMap());
498
499   // Log the check depth.
500   LOG(isolate(), IntEvent("check-maps-depth", depth + 1));
501
502   if (depth != 0 || check == CHECK_ALL_MAPS) {
503     // Check the holder map.
504     __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
505     Handle<WeakCell> cell = Map::WeakCellForMap(current_map);
506     __ CmpWeakValue(scratch1, cell, scratch2);
507     __ j(not_equal, miss);
508   }
509
510   // Return the register containing the holder.
511   return reg;
512 }
513
514
515 void NamedLoadHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
516   if (!miss->is_unused()) {
517     Label success;
518     __ jmp(&success);
519     __ bind(miss);
520     if (IC::ICUseVector(kind())) {
521       DCHECK(kind() == Code::LOAD_IC);
522       PopVectorAndSlot();
523     }
524     TailCallBuiltin(masm(), MissBuiltin(kind()));
525     __ bind(&success);
526   }
527 }
528
529
530 void NamedStoreHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
531   if (!miss->is_unused()) {
532     Label success;
533     __ jmp(&success);
534     GenerateRestoreName(miss, name);
535     TailCallBuiltin(masm(), MissBuiltin(kind()));
536     __ bind(&success);
537   }
538 }
539
540
541 void NamedLoadHandlerCompiler::GenerateLoadCallback(
542     Register reg, Handle<ExecutableAccessorInfo> callback) {
543   // Insert additional parameters into the stack frame above return address.
544   DCHECK(!scratch3().is(reg));
545   __ pop(scratch3());  // Get return address to place it below.
546
547   STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 0);
548   STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 1);
549   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 2);
550   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 3);
551   STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 4);
552   STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 5);
553   __ push(receiver());  // receiver
554   // Push data from ExecutableAccessorInfo.
555   Handle<Object> data(callback->data(), isolate());
556   if (data->IsUndefined() || data->IsSmi()) {
557     __ push(Immediate(data));
558   } else {
559     DCHECK(!scratch2().is(reg));
560     Handle<WeakCell> cell =
561         isolate()->factory()->NewWeakCell(Handle<HeapObject>::cast(data));
562     // The callback is alive if this instruction is executed,
563     // so the weak cell is not cleared and points to data.
564     __ GetWeakValue(scratch2(), cell);
565     __ push(scratch2());
566   }
567   __ push(Immediate(isolate()->factory()->undefined_value()));  // ReturnValue
568   // ReturnValue default value
569   __ push(Immediate(isolate()->factory()->undefined_value()));
570   __ push(Immediate(reinterpret_cast<int>(isolate())));
571   __ push(reg);  // holder
572
573   // Save a pointer to where we pushed the arguments. This will be
574   // passed as the const PropertyAccessorInfo& to the C++ callback.
575   __ push(esp);
576
577   __ push(name());  // name
578
579   __ push(scratch3());  // Restore return address.
580
581   // Abi for CallApiGetter
582   Register getter_address = ApiGetterDescriptor::function_address();
583   Address function_address = v8::ToCData<Address>(callback->getter());
584   __ mov(getter_address, Immediate(function_address));
585
586   CallApiGetterStub stub(isolate());
587   __ TailCallStub(&stub);
588 }
589
590
591 void NamedLoadHandlerCompiler::GenerateLoadConstant(Handle<Object> value) {
592   // Return the constant value.
593   __ LoadObject(eax, value);
594   __ ret(0);
595 }
596
597
598 void NamedLoadHandlerCompiler::GenerateLoadInterceptorWithFollowup(
599     LookupIterator* it, Register holder_reg) {
600   DCHECK(holder()->HasNamedInterceptor());
601   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
602
603   // Compile the interceptor call, followed by inline code to load the
604   // property from further up the prototype chain if the call fails.
605   // Check that the maps haven't changed.
606   DCHECK(holder_reg.is(receiver()) || holder_reg.is(scratch1()));
607
608   // Preserve the receiver register explicitly whenever it is different from the
609   // holder and it is needed should the interceptor return without any result.
610   // The ACCESSOR case needs the receiver to be passed into C++ code, the FIELD
611   // case might cause a miss during the prototype check.
612   bool must_perform_prototype_check =
613       !holder().is_identical_to(it->GetHolder<JSObject>());
614   bool must_preserve_receiver_reg =
615       !receiver().is(holder_reg) &&
616       (it->state() == LookupIterator::ACCESSOR || must_perform_prototype_check);
617
618   // Save necessary data before invoking an interceptor.
619   // Requires a frame to make GC aware of pushed pointers.
620   {
621     FrameScope frame_scope(masm(), StackFrame::INTERNAL);
622
623     if (must_preserve_receiver_reg) {
624       __ push(receiver());
625     }
626     __ push(holder_reg);
627     __ push(this->name());
628     InterceptorVectorSlotPush(holder_reg);
629     // Invoke an interceptor.  Note: map checks from receiver to
630     // interceptor's holder has been compiled before (see a caller
631     // of this method.)
632     CompileCallLoadPropertyWithInterceptor(
633         masm(), receiver(), holder_reg, this->name(), holder(),
634         IC::kLoadPropertyWithInterceptorOnly);
635
636     // Check if interceptor provided a value for property.  If it's
637     // the case, return immediately.
638     Label interceptor_failed;
639     __ cmp(eax, factory()->no_interceptor_result_sentinel());
640     __ j(equal, &interceptor_failed);
641     frame_scope.GenerateLeaveFrame();
642     __ ret(0);
643
644     // Clobber registers when generating debug-code to provoke errors.
645     __ bind(&interceptor_failed);
646     if (FLAG_debug_code) {
647       __ mov(receiver(), Immediate(bit_cast<int32_t>(kZapValue)));
648       __ mov(holder_reg, Immediate(bit_cast<int32_t>(kZapValue)));
649       __ mov(this->name(), Immediate(bit_cast<int32_t>(kZapValue)));
650     }
651
652     InterceptorVectorSlotPop(holder_reg);
653     __ pop(this->name());
654     __ pop(holder_reg);
655     if (must_preserve_receiver_reg) {
656       __ pop(receiver());
657     }
658
659     // Leave the internal frame.
660   }
661
662   GenerateLoadPostInterceptor(it, holder_reg);
663 }
664
665
666 void NamedLoadHandlerCompiler::GenerateLoadInterceptor(Register holder_reg) {
667   DCHECK(holder()->HasNamedInterceptor());
668   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
669   // Call the runtime system to load the interceptor.
670   __ pop(scratch2());  // save old return address
671   PushInterceptorArguments(masm(), receiver(), holder_reg, this->name(),
672                            holder());
673   __ push(scratch2());  // restore old return address
674
675   ExternalReference ref = ExternalReference(
676       IC_Utility(IC::kLoadPropertyWithInterceptor), isolate());
677   __ TailCallExternalReference(
678       ref, NamedLoadHandlerCompiler::kInterceptorArgsLength, 1);
679 }
680
681
682 Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback(
683     Handle<JSObject> object, Handle<Name> name,
684     Handle<ExecutableAccessorInfo> callback) {
685   Register holder_reg = Frontend(name);
686
687   __ pop(scratch1());  // remove the return address
688   __ push(receiver());
689   __ push(holder_reg);
690   // If the callback cannot leak, then push the callback directly,
691   // otherwise wrap it in a weak cell.
692   if (callback->data()->IsUndefined() || callback->data()->IsSmi()) {
693     __ Push(callback);
694   } else {
695     Handle<WeakCell> cell = isolate()->factory()->NewWeakCell(callback);
696     __ Push(cell);
697   }
698   __ Push(name);
699   __ push(value());
700   __ push(scratch1());  // restore return address
701
702   // Do tail-call to the runtime system.
703   ExternalReference store_callback_property =
704       ExternalReference(IC_Utility(IC::kStoreCallbackProperty), isolate());
705   __ TailCallExternalReference(store_callback_property, 5, 1);
706
707   // Return the generated code.
708   return GetCode(kind(), Code::FAST, name);
709 }
710
711
712 Handle<Code> NamedStoreHandlerCompiler::CompileStoreInterceptor(
713     Handle<Name> name) {
714   __ pop(scratch1());  // remove the return address
715   __ push(receiver());
716   __ push(this->name());
717   __ push(value());
718   __ push(scratch1());  // restore return address
719
720   // Do tail-call to the runtime system.
721   ExternalReference store_ic_property = ExternalReference(
722       IC_Utility(IC::kStorePropertyWithInterceptor), isolate());
723   __ TailCallExternalReference(store_ic_property, 3, 1);
724
725   // Return the generated code.
726   return GetCode(kind(), Code::FAST, name);
727 }
728
729
730 Register NamedStoreHandlerCompiler::value() {
731   return StoreDescriptor::ValueRegister();
732 }
733
734
735 Handle<Code> NamedLoadHandlerCompiler::CompileLoadGlobal(
736     Handle<PropertyCell> cell, Handle<Name> name, bool is_configurable) {
737   Label miss;
738   if (IC::ICUseVector(kind())) {
739     PushVectorAndSlot();
740   }
741   FrontendHeader(receiver(), name, &miss);
742   // Get the value from the cell.
743   Register result = StoreDescriptor::ValueRegister();
744   Handle<WeakCell> weak_cell = factory()->NewWeakCell(cell);
745   __ LoadWeakValue(result, weak_cell, &miss);
746   __ mov(result, FieldOperand(result, PropertyCell::kValueOffset));
747
748   // Check for deleted property if property can actually be deleted.
749   if (is_configurable) {
750     __ cmp(result, factory()->the_hole_value());
751     __ j(equal, &miss);
752   } else if (FLAG_debug_code) {
753     __ cmp(result, factory()->the_hole_value());
754     __ Check(not_equal, kDontDeleteCellsCannotContainTheHole);
755   }
756
757   Counters* counters = isolate()->counters();
758   __ IncrementCounter(counters->named_load_global_stub(), 1);
759   // The code above already loads the result into the return register.
760   if (IC::ICUseVector(kind())) {
761     DiscardVectorAndSlot();
762   }
763   __ ret(0);
764
765   FrontendFooter(name, &miss);
766
767   // Return the generated code.
768   return GetCode(kind(), Code::NORMAL, name);
769 }
770
771
772 #undef __
773 }
774 }  // namespace v8::internal
775
776 #endif  // V8_TARGET_ARCH_X87