94b48be274bdf64fad6603bf6072802fe723b72f
[platform/upstream/nodejs.git] / deps / v8 / src / ic / ia32 / handler-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/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   Handle<JSObject> prototype = Handle<JSObject>::null();
435   Handle<Map> current_map = receiver_map;
436   Handle<Map> holder_map(holder()->map());
437   // Traverse the prototype chain and check the maps in the prototype chain for
438   // fast and global objects or do negative lookup for normal objects.
439   while (!current_map.is_identical_to(holder_map)) {
440     ++depth;
441
442     // Only global objects and objects that do not require access
443     // checks are allowed in stubs.
444     DCHECK(current_map->IsJSGlobalProxyMap() ||
445            !current_map->is_access_check_needed());
446
447     prototype = handle(JSObject::cast(current_map->prototype()));
448     if (current_map->is_dictionary_map() &&
449         !current_map->IsJSGlobalObjectMap()) {
450       DCHECK(!current_map->IsJSGlobalProxyMap());  // Proxy maps are fast.
451       if (!name->IsUniqueName()) {
452         DCHECK(name->IsString());
453         name = factory()->InternalizeString(Handle<String>::cast(name));
454       }
455       DCHECK(current.is_null() ||
456              current->property_dictionary()->FindEntry(name) ==
457                  NameDictionary::kNotFound);
458
459       GenerateDictionaryNegativeLookup(masm(), miss, reg, name, scratch1,
460                                        scratch2);
461
462       __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
463       reg = holder_reg;  // From now on the object will be in holder_reg.
464       __ mov(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
465     } else {
466       Register map_reg = scratch1;
467       __ mov(map_reg, FieldOperand(reg, HeapObject::kMapOffset));
468       if (depth != 1 || check == CHECK_ALL_MAPS) {
469         Handle<WeakCell> cell = Map::WeakCellForMap(current_map);
470         __ CmpWeakValue(map_reg, cell, scratch2);
471         __ j(not_equal, miss);
472       }
473
474       // Check access rights to the global object.  This has to happen after
475       // the map check so that we know that the object is actually a global
476       // object.
477       // This allows us to install generated handlers for accesses to the
478       // global proxy (as opposed to using slow ICs). See corresponding code
479       // in LookupForRead().
480       if (current_map->IsJSGlobalProxyMap()) {
481         __ CheckAccessGlobalProxy(reg, map_reg, scratch2, miss);
482         // Restore map_reg.
483         __ mov(map_reg, FieldOperand(reg, HeapObject::kMapOffset));
484       } else if (current_map->IsJSGlobalObjectMap()) {
485         GenerateCheckPropertyCell(masm(), Handle<JSGlobalObject>::cast(current),
486                                   name, scratch2, miss);
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   // Log the check depth.
498   LOG(isolate(), IntEvent("check-maps-depth", depth + 1));
499
500   if (depth != 0 || check == CHECK_ALL_MAPS) {
501     // Check the holder map.
502     __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
503     Handle<WeakCell> cell = Map::WeakCellForMap(current_map);
504     __ CmpWeakValue(scratch1, cell, scratch2);
505     __ j(not_equal, miss);
506   }
507
508   // Perform security check for access to the global object.
509   DCHECK(current_map->IsJSGlobalProxyMap() ||
510          !current_map->is_access_check_needed());
511   if (current_map->IsJSGlobalProxyMap()) {
512     __ CheckAccessGlobalProxy(reg, scratch1, scratch2, miss);
513   }
514
515   // Return the register containing the holder.
516   return reg;
517 }
518
519
520 void NamedLoadHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
521   if (!miss->is_unused()) {
522     Label success;
523     __ jmp(&success);
524     __ bind(miss);
525     if (IC::ICUseVector(kind())) {
526       DCHECK(kind() == Code::LOAD_IC);
527       PopVectorAndSlot();
528     }
529     TailCallBuiltin(masm(), MissBuiltin(kind()));
530     __ bind(&success);
531   }
532 }
533
534
535 void NamedStoreHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
536   if (!miss->is_unused()) {
537     Label success;
538     __ jmp(&success);
539     GenerateRestoreName(miss, name);
540     TailCallBuiltin(masm(), MissBuiltin(kind()));
541     __ bind(&success);
542   }
543 }
544
545
546 void NamedLoadHandlerCompiler::GenerateLoadCallback(
547     Register reg, Handle<ExecutableAccessorInfo> callback) {
548   // Insert additional parameters into the stack frame above return address.
549   DCHECK(!scratch3().is(reg));
550   __ pop(scratch3());  // Get return address to place it below.
551
552   STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 0);
553   STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 1);
554   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 2);
555   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 3);
556   STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 4);
557   STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 5);
558   __ push(receiver());  // receiver
559   // Push data from ExecutableAccessorInfo.
560   Handle<Object> data(callback->data(), isolate());
561   if (data->IsUndefined() || data->IsSmi()) {
562     __ push(Immediate(data));
563   } else {
564     DCHECK(!scratch2().is(reg));
565     Handle<WeakCell> cell =
566         isolate()->factory()->NewWeakCell(Handle<HeapObject>::cast(data));
567     // The callback is alive if this instruction is executed,
568     // so the weak cell is not cleared and points to data.
569     __ GetWeakValue(scratch2(), cell);
570     __ push(scratch2());
571   }
572   __ push(Immediate(isolate()->factory()->undefined_value()));  // ReturnValue
573   // ReturnValue default value
574   __ push(Immediate(isolate()->factory()->undefined_value()));
575   __ push(Immediate(reinterpret_cast<int>(isolate())));
576   __ push(reg);  // holder
577
578   // Save a pointer to where we pushed the arguments. This will be
579   // passed as the const PropertyAccessorInfo& to the C++ callback.
580   __ push(esp);
581
582   __ push(name());  // name
583
584   __ push(scratch3());  // Restore return address.
585
586   // Abi for CallApiGetter
587   Register getter_address = ApiGetterDescriptor::function_address();
588   Address function_address = v8::ToCData<Address>(callback->getter());
589   __ mov(getter_address, Immediate(function_address));
590
591   CallApiGetterStub stub(isolate());
592   __ TailCallStub(&stub);
593 }
594
595
596 void NamedLoadHandlerCompiler::GenerateLoadConstant(Handle<Object> value) {
597   // Return the constant value.
598   __ LoadObject(eax, value);
599   __ ret(0);
600 }
601
602
603 void NamedLoadHandlerCompiler::GenerateLoadInterceptorWithFollowup(
604     LookupIterator* it, Register holder_reg) {
605   DCHECK(holder()->HasNamedInterceptor());
606   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
607
608   // Compile the interceptor call, followed by inline code to load the
609   // property from further up the prototype chain if the call fails.
610   // Check that the maps haven't changed.
611   DCHECK(holder_reg.is(receiver()) || holder_reg.is(scratch1()));
612
613   // Preserve the receiver register explicitly whenever it is different from the
614   // holder and it is needed should the interceptor return without any result.
615   // The ACCESSOR case needs the receiver to be passed into C++ code, the FIELD
616   // case might cause a miss during the prototype check.
617   bool must_perform_prototype_check =
618       !holder().is_identical_to(it->GetHolder<JSObject>());
619   bool must_preserve_receiver_reg =
620       !receiver().is(holder_reg) &&
621       (it->state() == LookupIterator::ACCESSOR || must_perform_prototype_check);
622
623   // Save necessary data before invoking an interceptor.
624   // Requires a frame to make GC aware of pushed pointers.
625   {
626     FrameScope frame_scope(masm(), StackFrame::INTERNAL);
627
628     if (must_preserve_receiver_reg) {
629       __ push(receiver());
630     }
631     __ push(holder_reg);
632     __ push(this->name());
633     InterceptorVectorSlotPush(holder_reg);
634     // Invoke an interceptor.  Note: map checks from receiver to
635     // interceptor's holder has been compiled before (see a caller
636     // of this method.)
637     CompileCallLoadPropertyWithInterceptor(
638         masm(), receiver(), holder_reg, this->name(), holder(),
639         IC::kLoadPropertyWithInterceptorOnly);
640
641     // Check if interceptor provided a value for property.  If it's
642     // the case, return immediately.
643     Label interceptor_failed;
644     __ cmp(eax, factory()->no_interceptor_result_sentinel());
645     __ j(equal, &interceptor_failed);
646     frame_scope.GenerateLeaveFrame();
647     __ ret(0);
648
649     // Clobber registers when generating debug-code to provoke errors.
650     __ bind(&interceptor_failed);
651     if (FLAG_debug_code) {
652       __ mov(receiver(), Immediate(bit_cast<int32_t>(kZapValue)));
653       __ mov(holder_reg, Immediate(bit_cast<int32_t>(kZapValue)));
654       __ mov(this->name(), Immediate(bit_cast<int32_t>(kZapValue)));
655     }
656
657     InterceptorVectorSlotPop(holder_reg);
658     __ pop(this->name());
659     __ pop(holder_reg);
660     if (must_preserve_receiver_reg) {
661       __ pop(receiver());
662     }
663
664     // Leave the internal frame.
665   }
666
667   GenerateLoadPostInterceptor(it, holder_reg);
668 }
669
670
671 void NamedLoadHandlerCompiler::GenerateLoadInterceptor(Register holder_reg) {
672   DCHECK(holder()->HasNamedInterceptor());
673   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
674   // Call the runtime system to load the interceptor.
675   __ pop(scratch2());  // save old return address
676   PushInterceptorArguments(masm(), receiver(), holder_reg, this->name(),
677                            holder());
678   __ push(scratch2());  // restore old return address
679
680   ExternalReference ref = ExternalReference(
681       IC_Utility(IC::kLoadPropertyWithInterceptor), isolate());
682   __ TailCallExternalReference(
683       ref, NamedLoadHandlerCompiler::kInterceptorArgsLength, 1);
684 }
685
686
687 Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback(
688     Handle<JSObject> object, Handle<Name> name,
689     Handle<ExecutableAccessorInfo> callback) {
690   Register holder_reg = Frontend(name);
691
692   __ pop(scratch1());  // remove the return address
693   __ push(receiver());
694   __ push(holder_reg);
695   // If the callback cannot leak, then push the callback directly,
696   // otherwise wrap it in a weak cell.
697   if (callback->data()->IsUndefined() || callback->data()->IsSmi()) {
698     __ Push(callback);
699   } else {
700     Handle<WeakCell> cell = isolate()->factory()->NewWeakCell(callback);
701     __ Push(cell);
702   }
703   __ Push(name);
704   __ push(value());
705   __ push(scratch1());  // restore return address
706
707   // Do tail-call to the runtime system.
708   ExternalReference store_callback_property =
709       ExternalReference(IC_Utility(IC::kStoreCallbackProperty), isolate());
710   __ TailCallExternalReference(store_callback_property, 5, 1);
711
712   // Return the generated code.
713   return GetCode(kind(), Code::FAST, name);
714 }
715
716
717 Handle<Code> NamedStoreHandlerCompiler::CompileStoreInterceptor(
718     Handle<Name> name) {
719   __ pop(scratch1());  // remove the return address
720   __ push(receiver());
721   __ push(this->name());
722   __ push(value());
723   __ push(scratch1());  // restore return address
724
725   // Do tail-call to the runtime system.
726   ExternalReference store_ic_property = ExternalReference(
727       IC_Utility(IC::kStorePropertyWithInterceptor), isolate());
728   __ TailCallExternalReference(store_ic_property, 3, 1);
729
730   // Return the generated code.
731   return GetCode(kind(), Code::FAST, name);
732 }
733
734
735 Register NamedStoreHandlerCompiler::value() {
736   return StoreDescriptor::ValueRegister();
737 }
738
739
740 Handle<Code> NamedLoadHandlerCompiler::CompileLoadGlobal(
741     Handle<PropertyCell> cell, Handle<Name> name, bool is_configurable) {
742   Label miss;
743   if (IC::ICUseVector(kind())) {
744     PushVectorAndSlot();
745   }
746   FrontendHeader(receiver(), name, &miss);
747   // Get the value from the cell.
748   Register result = StoreDescriptor::ValueRegister();
749   Handle<WeakCell> weak_cell = factory()->NewWeakCell(cell);
750   __ LoadWeakValue(result, weak_cell, &miss);
751   __ mov(result, FieldOperand(result, PropertyCell::kValueOffset));
752
753   // Check for deleted property if property can actually be deleted.
754   if (is_configurable) {
755     __ cmp(result, factory()->the_hole_value());
756     __ j(equal, &miss);
757   } else if (FLAG_debug_code) {
758     __ cmp(result, factory()->the_hole_value());
759     __ Check(not_equal, kDontDeleteCellsCannotContainTheHole);
760   }
761
762   Counters* counters = isolate()->counters();
763   __ IncrementCounter(counters->named_load_global_stub(), 1);
764   // The code above already loads the result into the return register.
765   if (IC::ICUseVector(kind())) {
766     DiscardVectorAndSlot();
767   }
768   __ ret(0);
769
770   FrontendFooter(name, &miss);
771
772   // Return the generated code.
773   return GetCode(kind(), Code::NORMAL, name);
774 }
775
776
777 #undef __
778 }
779 }  // namespace v8::internal
780
781 #endif  // V8_TARGET_ARCH_IA32