blob: 46fa8cc337139fa0089a14b2c79e7b22a6161ed8 [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_X64
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
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018void PropertyHandlerCompiler::PushVectorAndSlot(Register vector,
19 Register slot) {
20 MacroAssembler* masm = this->masm();
21 __ Push(vector);
22 __ Push(slot);
23}
24
25
26void PropertyHandlerCompiler::PopVectorAndSlot(Register vector, Register slot) {
27 MacroAssembler* masm = this->masm();
28 __ Pop(slot);
29 __ Pop(vector);
30}
31
32
33void PropertyHandlerCompiler::DiscardVectorAndSlot() {
34 MacroAssembler* masm = this->masm();
35 // Remove vector and slot.
36 __ addp(rsp, Immediate(2 * kPointerSize));
37}
38
39
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040void PropertyHandlerCompiler::GenerateDictionaryNegativeLookup(
41 MacroAssembler* masm, Label* miss_label, Register receiver,
42 Handle<Name> name, Register scratch0, Register scratch1) {
43 DCHECK(name->IsUniqueName());
44 DCHECK(!receiver.is(scratch0));
45 Counters* counters = masm->isolate()->counters();
46 __ IncrementCounter(counters->negative_lookups(), 1);
47 __ IncrementCounter(counters->negative_lookups_miss(), 1);
48
49 __ movp(scratch0, FieldOperand(receiver, HeapObject::kMapOffset));
50
51 const int kInterceptorOrAccessCheckNeededMask =
52 (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
53
54 // Bail out if the receiver has a named interceptor or requires access checks.
55 __ testb(FieldOperand(scratch0, Map::kBitFieldOffset),
56 Immediate(kInterceptorOrAccessCheckNeededMask));
57 __ j(not_zero, miss_label);
58
59 // Check that receiver is a JSObject.
60 __ CmpInstanceType(scratch0, FIRST_SPEC_OBJECT_TYPE);
61 __ j(below, miss_label);
62
63 // Load properties array.
64 Register properties = scratch0;
65 __ movp(properties, FieldOperand(receiver, JSObject::kPropertiesOffset));
66
67 // Check that the properties array is a dictionary.
68 __ CompareRoot(FieldOperand(properties, HeapObject::kMapOffset),
69 Heap::kHashTableMapRootIndex);
70 __ j(not_equal, miss_label);
71
72 Label done;
73 NameDictionaryLookupStub::GenerateNegativeLookup(masm, miss_label, &done,
74 properties, name, scratch1);
75 __ bind(&done);
76 __ DecrementCounter(counters->negative_lookups_miss(), 1);
77}
78
79
80void NamedLoadHandlerCompiler::GenerateDirectLoadGlobalFunctionPrototype(
Emily Bernierd0a1eb72015-03-24 16:35:39 -040081 MacroAssembler* masm, int index, Register result, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 const int offset = Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040083 __ movp(result, Operand(rsi, offset));
84 __ movp(result, FieldOperand(result, GlobalObject::kNativeContextOffset));
85 __ movp(result, Operand(result, Context::SlotOffset(index)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 // Load its initial map. The global functions all have initial maps.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040087 __ movp(result,
88 FieldOperand(result, JSFunction::kPrototypeOrInitialMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 // Load the prototype from the initial map.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040090 __ movp(result, FieldOperand(result, Map::kPrototypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091}
92
93
94void NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(
95 MacroAssembler* masm, Register receiver, Register result, Register scratch,
96 Label* miss_label) {
97 __ TryGetFunctionPrototype(receiver, result, miss_label);
98 if (!result.is(rax)) __ movp(rax, result);
99 __ ret(0);
100}
101
102
103static void PushInterceptorArguments(MacroAssembler* masm, Register receiver,
104 Register holder, Register name,
105 Handle<JSObject> holder_obj) {
106 STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsNameIndex == 0);
107 STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsInfoIndex == 1);
108 STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsThisIndex == 2);
109 STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsHolderIndex == 3);
110 STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsLength == 4);
111 __ Push(name);
112 Handle<InterceptorInfo> interceptor(holder_obj->GetNamedInterceptor());
113 DCHECK(!masm->isolate()->heap()->InNewSpace(*interceptor));
114 __ Move(kScratchRegister, interceptor);
115 __ Push(kScratchRegister);
116 __ Push(receiver);
117 __ Push(holder);
118}
119
120
121static void CompileCallLoadPropertyWithInterceptor(
122 MacroAssembler* masm, Register receiver, Register holder, Register name,
123 Handle<JSObject> holder_obj, IC::UtilityId id) {
124 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
125 __ CallExternalReference(ExternalReference(IC_Utility(id), masm->isolate()),
126 NamedLoadHandlerCompiler::kInterceptorArgsLength);
127}
128
129
130// Generate call to api function.
131void PropertyHandlerCompiler::GenerateFastApiCall(
132 MacroAssembler* masm, const CallOptimization& optimization,
133 Handle<Map> receiver_map, Register receiver, Register scratch_in,
134 bool is_store, int argc, Register* values) {
135 DCHECK(optimization.is_simple_api_call());
136
137 __ PopReturnAddressTo(scratch_in);
138 // receiver
139 __ Push(receiver);
140 // Write the arguments to stack frame.
141 for (int i = 0; i < argc; i++) {
142 Register arg = values[argc - 1 - i];
143 DCHECK(!receiver.is(arg));
144 DCHECK(!scratch_in.is(arg));
145 __ Push(arg);
146 }
147 __ PushReturnAddressFrom(scratch_in);
148 // Stack now matches JSFunction abi.
149
150 // Abi for CallApiFunctionStub.
151 Register callee = rax;
152 Register call_data = rbx;
153 Register holder = rcx;
154 Register api_function_address = rdx;
155 Register scratch = rdi; // scratch_in is no longer valid.
156
157 // Put holder in place.
158 CallOptimization::HolderLookup holder_lookup;
159 Handle<JSObject> api_holder =
160 optimization.LookupHolderOfExpectedType(receiver_map, &holder_lookup);
161 switch (holder_lookup) {
162 case CallOptimization::kHolderIsReceiver:
163 __ Move(holder, receiver);
164 break;
165 case CallOptimization::kHolderFound:
166 __ Move(holder, api_holder);
167 break;
168 case CallOptimization::kHolderNotFound:
169 UNREACHABLE();
170 break;
171 }
172
173 Isolate* isolate = masm->isolate();
174 Handle<JSFunction> function = optimization.constant_function();
175 Handle<CallHandlerInfo> api_call_info = optimization.api_call_info();
176 Handle<Object> call_data_obj(api_call_info->data(), isolate);
177
178 // Put callee in place.
179 __ Move(callee, function);
180
181 bool call_data_undefined = false;
182 // Put call_data in place.
183 if (isolate->heap()->InNewSpace(*call_data_obj)) {
184 __ Move(scratch, api_call_info);
185 __ movp(call_data, FieldOperand(scratch, CallHandlerInfo::kDataOffset));
186 } else if (call_data_obj->IsUndefined()) {
187 call_data_undefined = true;
188 __ LoadRoot(call_data, Heap::kUndefinedValueRootIndex);
189 } else {
190 __ Move(call_data, call_data_obj);
191 }
192
193 // Put api_function_address in place.
194 Address function_address = v8::ToCData<Address>(api_call_info->callback());
195 __ Move(api_function_address, function_address,
196 RelocInfo::EXTERNAL_REFERENCE);
197
198 // Jump to stub.
199 CallApiFunctionStub stub(isolate, is_store, call_data_undefined, argc);
200 __ TailCallStub(&stub);
201}
202
203
204void PropertyHandlerCompiler::GenerateCheckPropertyCell(
205 MacroAssembler* masm, Handle<JSGlobalObject> global, Handle<Name> name,
206 Register scratch, Label* miss) {
207 Handle<PropertyCell> cell = JSGlobalObject::EnsurePropertyCell(global, name);
208 DCHECK(cell->value()->IsTheHole());
209 __ Move(scratch, cell);
210 __ Cmp(FieldOperand(scratch, Cell::kValueOffset),
211 masm->isolate()->factory()->the_hole_value());
212 __ j(not_equal, miss);
213}
214
215
216void NamedStoreHandlerCompiler::GenerateStoreViaSetter(
217 MacroAssembler* masm, Handle<HeapType> type, Register receiver,
218 Handle<JSFunction> setter) {
219 // ----------- S t a t e -------------
220 // -- rsp[0] : return address
221 // -----------------------------------
222 {
223 FrameScope scope(masm, StackFrame::INTERNAL);
224
225 // Save value register, so we can restore it later.
226 __ Push(value());
227
228 if (!setter.is_null()) {
229 // Call the JavaScript setter with receiver and value on the stack.
230 if (IC::TypeToMap(*type, masm->isolate())->IsJSGlobalObjectMap()) {
231 // Swap in the global receiver.
232 __ movp(receiver,
233 FieldOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
234 }
235 __ Push(receiver);
236 __ Push(value());
237 ParameterCount actual(1);
238 ParameterCount expected(setter);
239 __ InvokeFunction(setter, expected, actual, CALL_FUNCTION,
240 NullCallWrapper());
241 } else {
242 // If we generate a global code snippet for deoptimization only, remember
243 // the place to continue after deoptimization.
244 masm->isolate()->heap()->SetSetterStubDeoptPCOffset(masm->pc_offset());
245 }
246
247 // We have to return the passed value, not the return value of the setter.
248 __ Pop(rax);
249
250 // Restore context register.
251 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
252 }
253 __ ret(0);
254}
255
256
257void NamedLoadHandlerCompiler::GenerateLoadViaGetter(
258 MacroAssembler* masm, Handle<HeapType> type, Register receiver,
259 Handle<JSFunction> getter) {
260 // ----------- S t a t e -------------
261 // -- rax : receiver
262 // -- rcx : name
263 // -- rsp[0] : return address
264 // -----------------------------------
265 {
266 FrameScope scope(masm, StackFrame::INTERNAL);
267
268 if (!getter.is_null()) {
269 // Call the JavaScript getter with the receiver on the stack.
270 if (IC::TypeToMap(*type, masm->isolate())->IsJSGlobalObjectMap()) {
271 // Swap in the global receiver.
272 __ movp(receiver,
273 FieldOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
274 }
275 __ Push(receiver);
276 ParameterCount actual(0);
277 ParameterCount expected(getter);
278 __ InvokeFunction(getter, expected, actual, CALL_FUNCTION,
279 NullCallWrapper());
280 } else {
281 // If we generate a global code snippet for deoptimization only, remember
282 // the place to continue after deoptimization.
283 masm->isolate()->heap()->SetGetterStubDeoptPCOffset(masm->pc_offset());
284 }
285
286 // Restore context register.
287 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
288 }
289 __ ret(0);
290}
291
292
293static void StoreIC_PushArgs(MacroAssembler* masm) {
294 Register receiver = StoreDescriptor::ReceiverRegister();
295 Register name = StoreDescriptor::NameRegister();
296 Register value = StoreDescriptor::ValueRegister();
297
298 DCHECK(!rbx.is(receiver) && !rbx.is(name) && !rbx.is(value));
299
300 __ PopReturnAddressTo(rbx);
301 __ Push(receiver);
302 __ Push(name);
303 __ Push(value);
304 __ PushReturnAddressFrom(rbx);
305}
306
307
308void NamedStoreHandlerCompiler::GenerateSlow(MacroAssembler* masm) {
309 // Return address is on the stack.
310 StoreIC_PushArgs(masm);
311
312 // Do tail-call to runtime routine.
313 ExternalReference ref(IC_Utility(IC::kStoreIC_Slow), masm->isolate());
314 __ TailCallExternalReference(ref, 3, 1);
315}
316
317
318void ElementHandlerCompiler::GenerateStoreSlow(MacroAssembler* masm) {
319 // Return address is on the stack.
320 StoreIC_PushArgs(masm);
321
322 // Do tail-call to runtime routine.
323 ExternalReference ref(IC_Utility(IC::kKeyedStoreIC_Slow), masm->isolate());
324 __ TailCallExternalReference(ref, 3, 1);
325}
326
327
328#undef __
329#define __ ACCESS_MASM((masm()))
330
331
332void NamedStoreHandlerCompiler::GenerateRestoreName(Label* label,
333 Handle<Name> name) {
334 if (!label->is_unused()) {
335 __ bind(label);
336 __ Move(this->name(), name);
337 }
338}
339
340
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400341void NamedStoreHandlerCompiler::GenerateRestoreName(Handle<Name> name) {
342 __ Move(this->name(), name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343}
344
345
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400346void NamedStoreHandlerCompiler::GenerateRestoreMap(Handle<Map> transition,
347 Register scratch,
348 Label* miss) {
349 Handle<WeakCell> cell = Map::WeakCellForMap(transition);
350 Register map_reg = StoreTransitionDescriptor::MapRegister();
351 DCHECK(!map_reg.is(scratch));
352 __ LoadWeakValue(map_reg, cell, miss);
353 if (transition->CanBeDeprecated()) {
354 __ movl(scratch, FieldOperand(map_reg, Map::kBitField3Offset));
355 __ andl(scratch, Immediate(Map::Deprecated::kMask));
356 __ j(not_zero, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400358}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400360
361void NamedStoreHandlerCompiler::GenerateConstantCheck(Register map_reg,
362 int descriptor,
363 Register value_reg,
364 Register scratch,
365 Label* miss_label) {
366 DCHECK(!map_reg.is(scratch));
367 DCHECK(!map_reg.is(value_reg));
368 DCHECK(!value_reg.is(scratch));
369 __ LoadInstanceDescriptors(map_reg, scratch);
370 __ movp(scratch,
371 FieldOperand(scratch, DescriptorArray::GetValueOffset(descriptor)));
372 __ cmpp(value_reg, scratch);
373 __ j(not_equal, miss_label);
374}
375
376
377void NamedStoreHandlerCompiler::GenerateFieldTypeChecks(HeapType* field_type,
378 Register value_reg,
379 Label* miss_label) {
380 __ JumpIfSmi(value_reg, miss_label);
381 HeapType::Iterator<Map> it = field_type->Classes();
382 if (!it.Done()) {
383 Label do_store;
384 while (true) {
385 __ CompareMap(value_reg, it.Current());
386 it.Advance();
387 if (it.Done()) {
388 __ j(not_equal, miss_label);
389 break;
390 }
391 __ j(equal, &do_store, Label::kNear);
392 }
393 __ bind(&do_store);
394 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395}
396
397
398Register PropertyHandlerCompiler::CheckPrototypes(
399 Register object_reg, Register holder_reg, Register scratch1,
400 Register scratch2, Handle<Name> name, Label* miss,
401 PrototypeCheckType check) {
402 Handle<Map> receiver_map(IC::TypeToMap(*type(), isolate()));
403
404 // Make sure there's no overlap between holder and object registers.
405 DCHECK(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
406 DCHECK(!scratch2.is(object_reg) && !scratch2.is(holder_reg) &&
407 !scratch2.is(scratch1));
408
409 // Keep track of the current object in register reg. On the first
410 // iteration, reg is an alias for object_reg, on later iterations,
411 // it is an alias for holder_reg.
412 Register reg = object_reg;
413 int depth = 0;
414
415 Handle<JSObject> current = Handle<JSObject>::null();
416 if (type()->IsConstant()) {
417 current = Handle<JSObject>::cast(type()->AsConstant()->Value());
418 }
419 Handle<JSObject> prototype = Handle<JSObject>::null();
420 Handle<Map> current_map = receiver_map;
421 Handle<Map> holder_map(holder()->map());
422 // Traverse the prototype chain and check the maps in the prototype chain for
423 // fast and global objects or do negative lookup for normal objects.
424 while (!current_map.is_identical_to(holder_map)) {
425 ++depth;
426
427 // Only global objects and objects that do not require access
428 // checks are allowed in stubs.
429 DCHECK(current_map->IsJSGlobalProxyMap() ||
430 !current_map->is_access_check_needed());
431
432 prototype = handle(JSObject::cast(current_map->prototype()));
433 if (current_map->is_dictionary_map() &&
434 !current_map->IsJSGlobalObjectMap()) {
435 DCHECK(!current_map->IsJSGlobalProxyMap()); // Proxy maps are fast.
436 if (!name->IsUniqueName()) {
437 DCHECK(name->IsString());
438 name = factory()->InternalizeString(Handle<String>::cast(name));
439 }
440 DCHECK(current.is_null() ||
441 current->property_dictionary()->FindEntry(name) ==
442 NameDictionary::kNotFound);
443
444 GenerateDictionaryNegativeLookup(masm(), miss, reg, name, scratch1,
445 scratch2);
446
447 __ movp(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
448 reg = holder_reg; // From now on the object will be in holder_reg.
449 __ movp(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
450 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400451 Register map_reg = scratch1;
452 __ movp(map_reg, FieldOperand(reg, HeapObject::kMapOffset));
453
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 if (depth != 1 || check == CHECK_ALL_MAPS) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400455 Handle<WeakCell> cell = Map::WeakCellForMap(current_map);
456 __ CmpWeakValue(map_reg, cell, scratch2);
457 __ j(not_equal, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 }
459
460 // Check access rights to the global object. This has to happen after
461 // the map check so that we know that the object is actually a global
462 // object.
463 // This allows us to install generated handlers for accesses to the
464 // global proxy (as opposed to using slow ICs). See corresponding code
465 // in LookupForRead().
466 if (current_map->IsJSGlobalProxyMap()) {
467 __ CheckAccessGlobalProxy(reg, scratch2, miss);
468 } else if (current_map->IsJSGlobalObjectMap()) {
469 GenerateCheckPropertyCell(masm(), Handle<JSGlobalObject>::cast(current),
470 name, scratch2, miss);
471 }
472 reg = holder_reg; // From now on the object will be in holder_reg.
473
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400474 __ movp(reg, FieldOperand(map_reg, Map::kPrototypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475 }
476
477 // Go to the next object in the prototype chain.
478 current = prototype;
479 current_map = handle(current->map());
480 }
481
482 // Log the check depth.
483 LOG(isolate(), IntEvent("check-maps-depth", depth + 1));
484
485 if (depth != 0 || check == CHECK_ALL_MAPS) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400486 __ movp(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
487 Handle<WeakCell> cell = Map::WeakCellForMap(current_map);
488 __ CmpWeakValue(scratch1, cell, scratch2);
489 __ j(not_equal, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 }
491
492 // Perform security check for access to the global object.
493 DCHECK(current_map->IsJSGlobalProxyMap() ||
494 !current_map->is_access_check_needed());
495 if (current_map->IsJSGlobalProxyMap()) {
496 __ CheckAccessGlobalProxy(reg, scratch1, miss);
497 }
498
499 // Return the register containing the holder.
500 return reg;
501}
502
503
504void NamedLoadHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
505 if (!miss->is_unused()) {
506 Label success;
507 __ jmp(&success);
508 __ bind(miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400509 if (IC::ICUseVector(kind())) {
510 DCHECK(kind() == Code::LOAD_IC);
511 PopVectorAndSlot();
512 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000513 TailCallBuiltin(masm(), MissBuiltin(kind()));
514 __ bind(&success);
515 }
516}
517
518
519void NamedStoreHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
520 if (!miss->is_unused()) {
521 Label success;
522 __ jmp(&success);
523 GenerateRestoreName(miss, name);
524 TailCallBuiltin(masm(), MissBuiltin(kind()));
525 __ bind(&success);
526 }
527}
528
529
530void NamedLoadHandlerCompiler::GenerateLoadCallback(
531 Register reg, Handle<ExecutableAccessorInfo> callback) {
532 // Insert additional parameters into the stack frame above return address.
533 DCHECK(!scratch4().is(reg));
534 __ PopReturnAddressTo(scratch4());
535
536 STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 0);
537 STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 1);
538 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 2);
539 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 3);
540 STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 4);
541 STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 5);
542 STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 6);
543 __ Push(receiver()); // receiver
544 if (heap()->InNewSpace(callback->data())) {
545 DCHECK(!scratch2().is(reg));
546 __ Move(scratch2(), callback);
547 __ Push(FieldOperand(scratch2(),
548 ExecutableAccessorInfo::kDataOffset)); // data
549 } else {
550 __ Push(Handle<Object>(callback->data(), isolate()));
551 }
552 DCHECK(!kScratchRegister.is(reg));
553 __ LoadRoot(kScratchRegister, Heap::kUndefinedValueRootIndex);
554 __ Push(kScratchRegister); // return value
555 __ Push(kScratchRegister); // return value default
556 __ PushAddress(ExternalReference::isolate_address(isolate()));
557 __ Push(reg); // holder
558 __ Push(name()); // name
559 // Save a pointer to where we pushed the arguments pointer. This will be
560 // passed as the const PropertyAccessorInfo& to the C++ callback.
561
562 __ PushReturnAddressFrom(scratch4());
563
564 // Abi for CallApiGetter
565 Register api_function_address = ApiGetterDescriptor::function_address();
566 Address getter_address = v8::ToCData<Address>(callback->getter());
567 __ Move(api_function_address, getter_address, RelocInfo::EXTERNAL_REFERENCE);
568
569 CallApiGetterStub stub(isolate());
570 __ TailCallStub(&stub);
571}
572
573
574void NamedLoadHandlerCompiler::GenerateLoadConstant(Handle<Object> value) {
575 // Return the constant value.
576 __ Move(rax, value);
577 __ ret(0);
578}
579
580
581void 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->state() == LookupIterator::ACCESSOR || must_perform_prototype_check);
600
601 // Save necessary data before invoking an interceptor.
602 // Requires a frame to make GC aware of pushed pointers.
603 {
604 FrameScope frame_scope(masm(), StackFrame::INTERNAL);
605
606 if (must_preserve_receiver_reg) {
607 __ Push(receiver());
608 }
609 __ Push(holder_reg);
610 __ Push(this->name());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400611 InterceptorVectorSlotPush(holder_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612
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 __ CompareRoot(rax, Heap::kNoInterceptorResultSentinelRootIndex);
624 __ j(equal, &interceptor_failed);
625 frame_scope.GenerateLeaveFrame();
626 __ ret(0);
627
628 __ bind(&interceptor_failed);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400629 InterceptorVectorSlotPop(holder_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630 __ Pop(this->name());
631 __ Pop(holder_reg);
632 if (must_preserve_receiver_reg) {
633 __ Pop(receiver());
634 }
635
636 // Leave the internal frame.
637 }
638
639 GenerateLoadPostInterceptor(it, holder_reg);
640}
641
642
643void NamedLoadHandlerCompiler::GenerateLoadInterceptor(Register holder_reg) {
644 // Call the runtime system to load the interceptor.
645 DCHECK(holder()->HasNamedInterceptor());
646 DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
647 __ PopReturnAddressTo(scratch2());
648 PushInterceptorArguments(masm(), receiver(), holder_reg, this->name(),
649 holder());
650 __ PushReturnAddressFrom(scratch2());
651
652 ExternalReference ref = ExternalReference(
653 IC_Utility(IC::kLoadPropertyWithInterceptor), isolate());
654 __ TailCallExternalReference(
655 ref, NamedLoadHandlerCompiler::kInterceptorArgsLength, 1);
656}
657
658
659Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback(
660 Handle<JSObject> object, Handle<Name> name,
661 Handle<ExecutableAccessorInfo> callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400662 Register holder_reg = Frontend(name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000663
664 __ PopReturnAddressTo(scratch1());
665 __ Push(receiver());
666 __ Push(holder_reg);
667 __ Push(callback); // callback info
668 __ Push(name);
669 __ Push(value());
670 __ PushReturnAddressFrom(scratch1());
671
672 // Do tail-call to the runtime system.
673 ExternalReference store_callback_property =
674 ExternalReference(IC_Utility(IC::kStoreCallbackProperty), isolate());
675 __ TailCallExternalReference(store_callback_property, 5, 1);
676
677 // Return the generated code.
678 return GetCode(kind(), Code::FAST, name);
679}
680
681
682Handle<Code> NamedStoreHandlerCompiler::CompileStoreInterceptor(
683 Handle<Name> name) {
684 __ PopReturnAddressTo(scratch1());
685 __ Push(receiver());
686 __ Push(this->name());
687 __ Push(value());
688 __ PushReturnAddressFrom(scratch1());
689
690 // Do tail-call to the runtime system.
691 ExternalReference store_ic_property = ExternalReference(
692 IC_Utility(IC::kStorePropertyWithInterceptor), isolate());
693 __ TailCallExternalReference(store_ic_property, 3, 1);
694
695 // Return the generated code.
696 return GetCode(kind(), Code::FAST, name);
697}
698
699
700Register NamedStoreHandlerCompiler::value() {
701 return StoreDescriptor::ValueRegister();
702}
703
704
705Handle<Code> NamedLoadHandlerCompiler::CompileLoadGlobal(
706 Handle<PropertyCell> cell, Handle<Name> name, bool is_configurable) {
707 Label miss;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400708 if (IC::ICUseVector(kind())) {
709 PushVectorAndSlot();
710 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000711 FrontendHeader(receiver(), name, &miss);
712
713 // Get the value from the cell.
714 Register result = StoreDescriptor::ValueRegister();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400715 Handle<WeakCell> weak_cell = factory()->NewWeakCell(cell);
716 __ LoadWeakValue(result, weak_cell, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 __ movp(result, FieldOperand(result, PropertyCell::kValueOffset));
718
719 // Check for deleted property if property can actually be deleted.
720 if (is_configurable) {
721 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
722 __ j(equal, &miss);
723 } else if (FLAG_debug_code) {
724 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
725 __ Check(not_equal, kDontDeleteCellsCannotContainTheHole);
726 }
727
728 Counters* counters = isolate()->counters();
729 __ IncrementCounter(counters->named_load_global_stub(), 1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400730 if (IC::ICUseVector(kind())) {
731 DiscardVectorAndSlot();
732 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 __ ret(0);
734
735 FrontendFooter(name, &miss);
736
737 // Return the generated code.
738 return GetCode(kind(), Code::NORMAL, name);
739}
740
741
742#undef __
743}
744} // namespace v8::internal
745
746#endif // V8_TARGET_ARCH_X64