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