Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / v8 / src / arm64 / stub-cache-arm64.cc
1 // Copyright 2013 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_ARM64
8
9 #include "src/codegen.h"
10 #include "src/ic-inl.h"
11 #include "src/stub-cache.h"
12
13 namespace v8 {
14 namespace internal {
15
16
17 #define __ ACCESS_MASM(masm)
18
19
20 void PropertyHandlerCompiler::GenerateDictionaryNegativeLookup(
21     MacroAssembler* masm, Label* miss_label, Register receiver,
22     Handle<Name> name, Register scratch0, Register scratch1) {
23   DCHECK(!AreAliased(receiver, scratch0, scratch1));
24   DCHECK(name->IsUniqueName());
25   Counters* counters = masm->isolate()->counters();
26   __ IncrementCounter(counters->negative_lookups(), 1, scratch0, scratch1);
27   __ IncrementCounter(counters->negative_lookups_miss(), 1, scratch0, scratch1);
28
29   Label done;
30
31   const int kInterceptorOrAccessCheckNeededMask =
32       (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
33
34   // Bail out if the receiver has a named interceptor or requires access checks.
35   Register map = scratch1;
36   __ Ldr(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
37   __ Ldrb(scratch0, FieldMemOperand(map, Map::kBitFieldOffset));
38   __ Tst(scratch0, kInterceptorOrAccessCheckNeededMask);
39   __ B(ne, miss_label);
40
41   // Check that receiver is a JSObject.
42   __ Ldrb(scratch0, FieldMemOperand(map, Map::kInstanceTypeOffset));
43   __ Cmp(scratch0, FIRST_SPEC_OBJECT_TYPE);
44   __ B(lt, miss_label);
45
46   // Load properties array.
47   Register properties = scratch0;
48   __ Ldr(properties, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
49   // Check that the properties array is a dictionary.
50   __ Ldr(map, FieldMemOperand(properties, HeapObject::kMapOffset));
51   __ JumpIfNotRoot(map, Heap::kHashTableMapRootIndex, miss_label);
52
53   NameDictionaryLookupStub::GenerateNegativeLookup(masm,
54                                                      miss_label,
55                                                      &done,
56                                                      receiver,
57                                                      properties,
58                                                      name,
59                                                      scratch1);
60   __ Bind(&done);
61   __ DecrementCounter(counters->negative_lookups_miss(), 1, scratch0, scratch1);
62 }
63
64
65 // Probe primary or secondary table.
66 // If the entry is found in the cache, the generated code jump to the first
67 // instruction of the stub in the cache.
68 // If there is a miss the code fall trough.
69 //
70 // 'receiver', 'name' and 'offset' registers are preserved on miss.
71 static void ProbeTable(Isolate* isolate,
72                        MacroAssembler* masm,
73                        Code::Flags flags,
74                        StubCache::Table table,
75                        Register receiver,
76                        Register name,
77                        Register offset,
78                        Register scratch,
79                        Register scratch2,
80                        Register scratch3) {
81   // Some code below relies on the fact that the Entry struct contains
82   // 3 pointers (name, code, map).
83   STATIC_ASSERT(sizeof(StubCache::Entry) == (3 * kPointerSize));
84
85   ExternalReference key_offset(isolate->stub_cache()->key_reference(table));
86   ExternalReference value_offset(isolate->stub_cache()->value_reference(table));
87   ExternalReference map_offset(isolate->stub_cache()->map_reference(table));
88
89   uintptr_t key_off_addr = reinterpret_cast<uintptr_t>(key_offset.address());
90   uintptr_t value_off_addr =
91       reinterpret_cast<uintptr_t>(value_offset.address());
92   uintptr_t map_off_addr = reinterpret_cast<uintptr_t>(map_offset.address());
93
94   Label miss;
95
96   DCHECK(!AreAliased(name, offset, scratch, scratch2, scratch3));
97
98   // Multiply by 3 because there are 3 fields per entry.
99   __ Add(scratch3, offset, Operand(offset, LSL, 1));
100
101   // Calculate the base address of the entry.
102   __ Mov(scratch, key_offset);
103   __ Add(scratch, scratch, Operand(scratch3, LSL, kPointerSizeLog2));
104
105   // Check that the key in the entry matches the name.
106   __ Ldr(scratch2, MemOperand(scratch));
107   __ Cmp(name, scratch2);
108   __ B(ne, &miss);
109
110   // Check the map matches.
111   __ Ldr(scratch2, MemOperand(scratch, map_off_addr - key_off_addr));
112   __ Ldr(scratch3, FieldMemOperand(receiver, HeapObject::kMapOffset));
113   __ Cmp(scratch2, scratch3);
114   __ B(ne, &miss);
115
116   // Get the code entry from the cache.
117   __ Ldr(scratch, MemOperand(scratch, value_off_addr - key_off_addr));
118
119   // Check that the flags match what we're looking for.
120   __ Ldr(scratch2.W(), FieldMemOperand(scratch, Code::kFlagsOffset));
121   __ Bic(scratch2.W(), scratch2.W(), Code::kFlagsNotUsedInLookup);
122   __ Cmp(scratch2.W(), flags);
123   __ B(ne, &miss);
124
125 #ifdef DEBUG
126   if (FLAG_test_secondary_stub_cache && table == StubCache::kPrimary) {
127     __ B(&miss);
128   } else if (FLAG_test_primary_stub_cache && table == StubCache::kSecondary) {
129     __ B(&miss);
130   }
131 #endif
132
133   // Jump to the first instruction in the code stub.
134   __ Add(scratch, scratch, Code::kHeaderSize - kHeapObjectTag);
135   __ Br(scratch);
136
137   // Miss: fall through.
138   __ Bind(&miss);
139 }
140
141
142 void StubCache::GenerateProbe(MacroAssembler* masm,
143                               Code::Flags flags,
144                               Register receiver,
145                               Register name,
146                               Register scratch,
147                               Register extra,
148                               Register extra2,
149                               Register extra3) {
150   Isolate* isolate = masm->isolate();
151   Label miss;
152
153   // Make sure the flags does not name a specific type.
154   DCHECK(Code::ExtractTypeFromFlags(flags) == 0);
155
156   // Make sure that there are no register conflicts.
157   DCHECK(!AreAliased(receiver, name, scratch, extra, extra2, extra3));
158
159   // Make sure extra and extra2 registers are valid.
160   DCHECK(!extra.is(no_reg));
161   DCHECK(!extra2.is(no_reg));
162   DCHECK(!extra3.is(no_reg));
163
164   Counters* counters = masm->isolate()->counters();
165   __ IncrementCounter(counters->megamorphic_stub_cache_probes(), 1,
166                       extra2, extra3);
167
168   // Check that the receiver isn't a smi.
169   __ JumpIfSmi(receiver, &miss);
170
171   // Compute the hash for primary table.
172   __ Ldr(scratch, FieldMemOperand(name, Name::kHashFieldOffset));
173   __ Ldr(extra, FieldMemOperand(receiver, HeapObject::kMapOffset));
174   __ Add(scratch, scratch, extra);
175   __ Eor(scratch, scratch, flags);
176   // We shift out the last two bits because they are not part of the hash.
177   __ Ubfx(scratch, scratch, kCacheIndexShift,
178           CountTrailingZeros(kPrimaryTableSize, 64));
179
180   // Probe the primary table.
181   ProbeTable(isolate, masm, flags, kPrimary, receiver, name,
182       scratch, extra, extra2, extra3);
183
184   // Primary miss: Compute hash for secondary table.
185   __ Sub(scratch, scratch, Operand(name, LSR, kCacheIndexShift));
186   __ Add(scratch, scratch, flags >> kCacheIndexShift);
187   __ And(scratch, scratch, kSecondaryTableSize - 1);
188
189   // Probe the secondary table.
190   ProbeTable(isolate, masm, flags, kSecondary, receiver, name,
191       scratch, extra, extra2, extra3);
192
193   // Cache miss: Fall-through and let caller handle the miss by
194   // entering the runtime system.
195   __ Bind(&miss);
196   __ IncrementCounter(counters->megamorphic_stub_cache_misses(), 1,
197                       extra2, extra3);
198 }
199
200
201 void NamedLoadHandlerCompiler::GenerateDirectLoadGlobalFunctionPrototype(
202     MacroAssembler* masm, int index, Register prototype, Label* miss) {
203   Isolate* isolate = masm->isolate();
204   // Get the global function with the given index.
205   Handle<JSFunction> function(
206       JSFunction::cast(isolate->native_context()->get(index)));
207
208   // Check we're still in the same context.
209   Register scratch = prototype;
210   __ Ldr(scratch, GlobalObjectMemOperand());
211   __ Ldr(scratch, FieldMemOperand(scratch, GlobalObject::kNativeContextOffset));
212   __ Ldr(scratch, ContextMemOperand(scratch, index));
213   __ Cmp(scratch, Operand(function));
214   __ B(ne, miss);
215
216   // Load its initial map. The global functions all have initial maps.
217   __ Mov(prototype, Operand(Handle<Map>(function->initial_map())));
218   // Load the prototype from the initial map.
219   __ Ldr(prototype, FieldMemOperand(prototype, Map::kPrototypeOffset));
220 }
221
222
223 void NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(
224     MacroAssembler* masm, Register receiver, Register scratch1,
225     Register scratch2, Label* miss_label) {
226   __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
227   // TryGetFunctionPrototype can't put the result directly in x0 because the
228   // 3 inputs registers can't alias and we call this function from
229   // LoadIC::GenerateFunctionPrototype, where receiver is x0. So we explicitly
230   // move the result in x0.
231   __ Mov(x0, scratch1);
232   __ Ret();
233 }
234
235
236 // Generate code to check that a global property cell is empty. Create
237 // the property cell at compilation time if no cell exists for the
238 // property.
239 void PropertyHandlerCompiler::GenerateCheckPropertyCell(
240     MacroAssembler* masm, Handle<JSGlobalObject> global, Handle<Name> name,
241     Register scratch, Label* miss) {
242   Handle<Cell> cell = JSGlobalObject::EnsurePropertyCell(global, name);
243   DCHECK(cell->value()->IsTheHole());
244   __ Mov(scratch, Operand(cell));
245   __ Ldr(scratch, FieldMemOperand(scratch, Cell::kValueOffset));
246   __ JumpIfNotRoot(scratch, Heap::kTheHoleValueRootIndex, miss);
247 }
248
249
250 static void PushInterceptorArguments(MacroAssembler* masm, Register receiver,
251                                      Register holder, Register name,
252                                      Handle<JSObject> holder_obj) {
253   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsNameIndex == 0);
254   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsInfoIndex == 1);
255   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsThisIndex == 2);
256   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsHolderIndex == 3);
257   STATIC_ASSERT(NamedLoadHandlerCompiler::kInterceptorArgsLength == 4);
258
259   __ Push(name);
260   Handle<InterceptorInfo> interceptor(holder_obj->GetNamedInterceptor());
261   DCHECK(!masm->isolate()->heap()->InNewSpace(*interceptor));
262   Register scratch = name;
263   __ Mov(scratch, Operand(interceptor));
264   __ Push(scratch, receiver, holder);
265 }
266
267
268 static void CompileCallLoadPropertyWithInterceptor(
269     MacroAssembler* masm, Register receiver, Register holder, Register name,
270     Handle<JSObject> holder_obj, IC::UtilityId id) {
271   PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
272
273   __ CallExternalReference(ExternalReference(IC_Utility(id), masm->isolate()),
274                            NamedLoadHandlerCompiler::kInterceptorArgsLength);
275 }
276
277
278 // Generate call to api function.
279 void PropertyHandlerCompiler::GenerateFastApiCall(
280     MacroAssembler* masm, const CallOptimization& optimization,
281     Handle<Map> receiver_map, Register receiver, Register scratch,
282     bool is_store, int argc, Register* values) {
283   DCHECK(!AreAliased(receiver, scratch));
284
285   MacroAssembler::PushPopQueue queue(masm);
286   queue.Queue(receiver);
287   // Write the arguments to the stack frame.
288   for (int i = 0; i < argc; i++) {
289     Register arg = values[argc - 1 - i];
290     DCHECK(!AreAliased(receiver, scratch, arg));
291     queue.Queue(arg);
292   }
293   queue.PushQueued();
294
295   DCHECK(optimization.is_simple_api_call());
296
297   // Abi for CallApiFunctionStub.
298   Register callee = x0;
299   Register call_data = x4;
300   Register holder = x2;
301   Register api_function_address = x1;
302
303   // Put holder in place.
304   CallOptimization::HolderLookup holder_lookup;
305   Handle<JSObject> api_holder =
306       optimization.LookupHolderOfExpectedType(receiver_map, &holder_lookup);
307   switch (holder_lookup) {
308     case CallOptimization::kHolderIsReceiver:
309       __ Mov(holder, receiver);
310       break;
311     case CallOptimization::kHolderFound:
312       __ LoadObject(holder, api_holder);
313       break;
314     case CallOptimization::kHolderNotFound:
315       UNREACHABLE();
316       break;
317   }
318
319   Isolate* isolate = masm->isolate();
320   Handle<JSFunction> function = optimization.constant_function();
321   Handle<CallHandlerInfo> api_call_info = optimization.api_call_info();
322   Handle<Object> call_data_obj(api_call_info->data(), isolate);
323
324   // Put callee in place.
325   __ LoadObject(callee, function);
326
327   bool call_data_undefined = false;
328   // Put call_data in place.
329   if (isolate->heap()->InNewSpace(*call_data_obj)) {
330     __ LoadObject(call_data, api_call_info);
331     __ Ldr(call_data, FieldMemOperand(call_data, CallHandlerInfo::kDataOffset));
332   } else if (call_data_obj->IsUndefined()) {
333     call_data_undefined = true;
334     __ LoadRoot(call_data, Heap::kUndefinedValueRootIndex);
335   } else {
336     __ LoadObject(call_data, call_data_obj);
337   }
338
339   // Put api_function_address in place.
340   Address function_address = v8::ToCData<Address>(api_call_info->callback());
341   ApiFunction fun(function_address);
342   ExternalReference ref = ExternalReference(
343       &fun, ExternalReference::DIRECT_API_CALL, masm->isolate());
344   __ Mov(api_function_address, ref);
345
346   // Jump to stub.
347   CallApiFunctionStub stub(isolate, is_store, call_data_undefined, argc);
348   __ TailCallStub(&stub);
349 }
350
351
352 void PropertyAccessCompiler::GenerateTailCall(MacroAssembler* masm,
353                                               Handle<Code> code) {
354   __ Jump(code, RelocInfo::CODE_TARGET);
355 }
356
357
358 #undef __
359 #define __ ACCESS_MASM(masm())
360
361
362 void NamedStoreHandlerCompiler::GenerateRestoreName(Label* label,
363                                                     Handle<Name> name) {
364   if (!label->is_unused()) {
365     __ Bind(label);
366     __ Mov(this->name(), Operand(name));
367   }
368 }
369
370
371 // Generate StoreTransition code, value is passed in x0 register.
372 // When leaving generated code after success, the receiver_reg and storage_reg
373 // may be clobbered. Upon branch to miss_label, the receiver and name registers
374 // have their original values.
375 void NamedStoreHandlerCompiler::GenerateStoreTransition(
376     Handle<Map> transition, Handle<Name> name, Register receiver_reg,
377     Register storage_reg, Register value_reg, Register scratch1,
378     Register scratch2, Register scratch3, Label* miss_label, Label* slow) {
379   Label exit;
380
381   DCHECK(!AreAliased(receiver_reg, storage_reg, value_reg,
382                      scratch1, scratch2, scratch3));
383
384   // We don't need scratch3.
385   scratch3 = NoReg;
386
387   int descriptor = transition->LastAdded();
388   DescriptorArray* descriptors = transition->instance_descriptors();
389   PropertyDetails details = descriptors->GetDetails(descriptor);
390   Representation representation = details.representation();
391   DCHECK(!representation.IsNone());
392
393   if (details.type() == CONSTANT) {
394     Handle<Object> constant(descriptors->GetValue(descriptor), isolate());
395     __ LoadObject(scratch1, constant);
396     __ Cmp(value_reg, scratch1);
397     __ B(ne, miss_label);
398   } else if (representation.IsSmi()) {
399     __ JumpIfNotSmi(value_reg, miss_label);
400   } else if (representation.IsHeapObject()) {
401     __ JumpIfSmi(value_reg, miss_label);
402     HeapType* field_type = descriptors->GetFieldType(descriptor);
403     HeapType::Iterator<Map> it = field_type->Classes();
404     if (!it.Done()) {
405       __ Ldr(scratch1, FieldMemOperand(value_reg, HeapObject::kMapOffset));
406       Label do_store;
407       while (true) {
408         __ CompareMap(scratch1, it.Current());
409         it.Advance();
410         if (it.Done()) {
411           __ B(ne, miss_label);
412           break;
413         }
414         __ B(eq, &do_store);
415       }
416       __ Bind(&do_store);
417     }
418   } else if (representation.IsDouble()) {
419     UseScratchRegisterScope temps(masm());
420     DoubleRegister temp_double = temps.AcquireD();
421     __ SmiUntagToDouble(temp_double, value_reg, kSpeculativeUntag);
422
423     Label do_store;
424     __ JumpIfSmi(value_reg, &do_store);
425
426     __ CheckMap(value_reg, scratch1, Heap::kHeapNumberMapRootIndex,
427                 miss_label, DONT_DO_SMI_CHECK);
428     __ Ldr(temp_double, FieldMemOperand(value_reg, HeapNumber::kValueOffset));
429
430     __ Bind(&do_store);
431     __ AllocateHeapNumber(storage_reg, slow, scratch1, scratch2, temp_double,
432                           NoReg, MUTABLE);
433   }
434
435   // Stub never generated for objects that require access checks.
436   DCHECK(!transition->is_access_check_needed());
437
438   // Perform map transition for the receiver if necessary.
439   if (details.type() == FIELD &&
440       Map::cast(transition->GetBackPointer())->unused_property_fields() == 0) {
441     // The properties must be extended before we can store the value.
442     // We jump to a runtime call that extends the properties array.
443     __ Mov(scratch1, Operand(transition));
444     __ Push(receiver_reg, scratch1, value_reg);
445     __ TailCallExternalReference(
446         ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage),
447                           isolate()),
448         3, 1);
449     return;
450   }
451
452   // Update the map of the object.
453   __ Mov(scratch1, Operand(transition));
454   __ Str(scratch1, FieldMemOperand(receiver_reg, HeapObject::kMapOffset));
455
456   // Update the write barrier for the map field.
457   __ RecordWriteField(receiver_reg,
458                       HeapObject::kMapOffset,
459                       scratch1,
460                       scratch2,
461                       kLRHasNotBeenSaved,
462                       kDontSaveFPRegs,
463                       OMIT_REMEMBERED_SET,
464                       OMIT_SMI_CHECK);
465
466   if (details.type() == CONSTANT) {
467     DCHECK(value_reg.is(x0));
468     __ Ret();
469     return;
470   }
471
472   int index = transition->instance_descriptors()->GetFieldIndex(
473       transition->LastAdded());
474
475   // Adjust for the number of properties stored in the object. Even in the
476   // face of a transition we can use the old map here because the size of the
477   // object and the number of in-object properties is not going to change.
478   index -= transition->inobject_properties();
479
480   // TODO(verwaest): Share this code as a code stub.
481   SmiCheck smi_check = representation.IsTagged()
482       ? INLINE_SMI_CHECK : OMIT_SMI_CHECK;
483   Register prop_reg = representation.IsDouble() ? storage_reg : value_reg;
484   if (index < 0) {
485     // Set the property straight into the object.
486     int offset = transition->instance_size() + (index * kPointerSize);
487     __ Str(prop_reg, FieldMemOperand(receiver_reg, offset));
488
489     if (!representation.IsSmi()) {
490       // Update the write barrier for the array address.
491       if (!representation.IsDouble()) {
492         __ Mov(storage_reg, value_reg);
493       }
494       __ RecordWriteField(receiver_reg,
495                           offset,
496                           storage_reg,
497                           scratch1,
498                           kLRHasNotBeenSaved,
499                           kDontSaveFPRegs,
500                           EMIT_REMEMBERED_SET,
501                           smi_check);
502     }
503   } else {
504     // Write to the properties array.
505     int offset = index * kPointerSize + FixedArray::kHeaderSize;
506     // Get the properties array
507     __ Ldr(scratch1,
508            FieldMemOperand(receiver_reg, JSObject::kPropertiesOffset));
509     __ Str(prop_reg, FieldMemOperand(scratch1, offset));
510
511     if (!representation.IsSmi()) {
512       // Update the write barrier for the array address.
513       if (!representation.IsDouble()) {
514         __ Mov(storage_reg, value_reg);
515       }
516       __ RecordWriteField(scratch1,
517                           offset,
518                           storage_reg,
519                           receiver_reg,
520                           kLRHasNotBeenSaved,
521                           kDontSaveFPRegs,
522                           EMIT_REMEMBERED_SET,
523                           smi_check);
524     }
525   }
526
527   __ Bind(&exit);
528   // Return the value (register x0).
529   DCHECK(value_reg.is(x0));
530   __ Ret();
531 }
532
533
534 void NamedStoreHandlerCompiler::GenerateStoreField(LookupResult* lookup,
535                                                    Register value_reg,
536                                                    Label* miss_label) {
537   DCHECK(lookup->representation().IsHeapObject());
538   __ JumpIfSmi(value_reg, miss_label);
539   HeapType::Iterator<Map> it = lookup->GetFieldType()->Classes();
540   __ Ldr(scratch1(), FieldMemOperand(value_reg, HeapObject::kMapOffset));
541   Label do_store;
542   while (true) {
543     __ CompareMap(scratch1(), it.Current());
544     it.Advance();
545     if (it.Done()) {
546       __ B(ne, miss_label);
547       break;
548     }
549     __ B(eq, &do_store);
550   }
551   __ Bind(&do_store);
552
553   StoreFieldStub stub(isolate(), lookup->GetFieldIndex(),
554                       lookup->representation());
555   GenerateTailCall(masm(), stub.GetCode());
556 }
557
558
559 Register PropertyHandlerCompiler::CheckPrototypes(
560     Register object_reg, Register holder_reg, Register scratch1,
561     Register scratch2, Handle<Name> name, Label* miss,
562     PrototypeCheckType check) {
563   Handle<Map> receiver_map(IC::TypeToMap(*type(), isolate()));
564
565   // object_reg and holder_reg registers can alias.
566   DCHECK(!AreAliased(object_reg, scratch1, scratch2));
567   DCHECK(!AreAliased(holder_reg, scratch1, scratch2));
568
569   // Keep track of the current object in register reg.
570   Register reg = object_reg;
571   int depth = 0;
572
573   Handle<JSObject> current = Handle<JSObject>::null();
574   if (type()->IsConstant()) {
575     current = Handle<JSObject>::cast(type()->AsConstant()->Value());
576   }
577   Handle<JSObject> prototype = Handle<JSObject>::null();
578   Handle<Map> current_map = receiver_map;
579   Handle<Map> holder_map(holder()->map());
580   // Traverse the prototype chain and check the maps in the prototype chain for
581   // fast and global objects or do negative lookup for normal objects.
582   while (!current_map.is_identical_to(holder_map)) {
583     ++depth;
584
585     // Only global objects and objects that do not require access
586     // checks are allowed in stubs.
587     DCHECK(current_map->IsJSGlobalProxyMap() ||
588            !current_map->is_access_check_needed());
589
590     prototype = handle(JSObject::cast(current_map->prototype()));
591     if (current_map->is_dictionary_map() &&
592         !current_map->IsJSGlobalObjectMap()) {
593       DCHECK(!current_map->IsJSGlobalProxyMap());  // Proxy maps are fast.
594       if (!name->IsUniqueName()) {
595         DCHECK(name->IsString());
596         name = factory()->InternalizeString(Handle<String>::cast(name));
597       }
598       DCHECK(current.is_null() ||
599              (current->property_dictionary()->FindEntry(name) ==
600               NameDictionary::kNotFound));
601
602       GenerateDictionaryNegativeLookup(masm(), miss, reg, name,
603                                        scratch1, scratch2);
604
605       __ Ldr(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
606       reg = holder_reg;  // From now on the object will be in holder_reg.
607       __ Ldr(reg, FieldMemOperand(scratch1, Map::kPrototypeOffset));
608     } else {
609       // Two possible reasons for loading the prototype from the map:
610       // (1) Can't store references to new space in code.
611       // (2) Handler is shared for all receivers with the same prototype
612       //     map (but not necessarily the same prototype instance).
613       bool load_prototype_from_map =
614           heap()->InNewSpace(*prototype) || depth == 1;
615       Register map_reg = scratch1;
616       __ Ldr(map_reg, FieldMemOperand(reg, HeapObject::kMapOffset));
617
618       if (depth != 1 || check == CHECK_ALL_MAPS) {
619         __ CheckMap(map_reg, current_map, miss, DONT_DO_SMI_CHECK);
620       }
621
622       // Check access rights to the global object.  This has to happen after
623       // the map check so that we know that the object is actually a global
624       // object.
625       // This allows us to install generated handlers for accesses to the
626       // global proxy (as opposed to using slow ICs). See corresponding code
627       // in LookupForRead().
628       if (current_map->IsJSGlobalProxyMap()) {
629         UseScratchRegisterScope temps(masm());
630         __ CheckAccessGlobalProxy(reg, scratch2, temps.AcquireX(), miss);
631       } else if (current_map->IsJSGlobalObjectMap()) {
632         GenerateCheckPropertyCell(
633             masm(), Handle<JSGlobalObject>::cast(current), name,
634             scratch2, miss);
635       }
636
637       reg = holder_reg;  // From now on the object will be in holder_reg.
638
639       if (load_prototype_from_map) {
640         __ Ldr(reg, FieldMemOperand(map_reg, Map::kPrototypeOffset));
641       } else {
642         __ Mov(reg, Operand(prototype));
643       }
644     }
645
646     // Go to the next object in the prototype chain.
647     current = prototype;
648     current_map = handle(current->map());
649   }
650
651   // Log the check depth.
652   LOG(isolate(), IntEvent("check-maps-depth", depth + 1));
653
654   // Check the holder map.
655   if (depth != 0 || check == CHECK_ALL_MAPS) {
656     // Check the holder map.
657     __ CheckMap(reg, scratch1, current_map, miss, DONT_DO_SMI_CHECK);
658   }
659
660   // Perform security check for access to the global object.
661   DCHECK(current_map->IsJSGlobalProxyMap() ||
662          !current_map->is_access_check_needed());
663   if (current_map->IsJSGlobalProxyMap()) {
664     __ CheckAccessGlobalProxy(reg, scratch1, scratch2, miss);
665   }
666
667   // Return the register containing the holder.
668   return reg;
669 }
670
671
672 void NamedLoadHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
673   if (!miss->is_unused()) {
674     Label success;
675     __ B(&success);
676
677     __ Bind(miss);
678     TailCallBuiltin(masm(), MissBuiltin(kind()));
679
680     __ Bind(&success);
681   }
682 }
683
684
685 void NamedStoreHandlerCompiler::FrontendFooter(Handle<Name> name, Label* miss) {
686   if (!miss->is_unused()) {
687     Label success;
688     __ B(&success);
689
690     GenerateRestoreName(miss, name);
691     TailCallBuiltin(masm(), MissBuiltin(kind()));
692
693     __ Bind(&success);
694   }
695 }
696
697
698 void NamedLoadHandlerCompiler::GenerateLoadConstant(Handle<Object> value) {
699   // Return the constant value.
700   __ LoadObject(x0, value);
701   __ Ret();
702 }
703
704
705 void NamedLoadHandlerCompiler::GenerateLoadCallback(
706     Register reg, Handle<ExecutableAccessorInfo> callback) {
707   DCHECK(!AreAliased(scratch2(), scratch3(), scratch4(), reg));
708
709   // Build ExecutableAccessorInfo::args_ list on the stack and push property
710   // name below the exit frame to make GC aware of them and store pointers to
711   // them.
712   STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 0);
713   STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 1);
714   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 2);
715   STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 3);
716   STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 4);
717   STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 5);
718   STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 6);
719
720   __ Push(receiver());
721
722   if (heap()->InNewSpace(callback->data())) {
723     __ Mov(scratch3(), Operand(callback));
724     __ Ldr(scratch3(), FieldMemOperand(scratch3(),
725                                        ExecutableAccessorInfo::kDataOffset));
726   } else {
727     __ Mov(scratch3(), Operand(Handle<Object>(callback->data(), isolate())));
728   }
729   __ LoadRoot(scratch4(), Heap::kUndefinedValueRootIndex);
730   __ Mov(scratch2(), Operand(ExternalReference::isolate_address(isolate())));
731   __ Push(scratch3(), scratch4(), scratch4(), scratch2(), reg, name());
732
733   Register args_addr = scratch2();
734   __ Add(args_addr, __ StackPointer(), kPointerSize);
735
736   // Stack at this point:
737   //              sp[40] callback data
738   //              sp[32] undefined
739   //              sp[24] undefined
740   //              sp[16] isolate
741   // args_addr -> sp[8]  reg
742   //              sp[0]  name
743
744   // Abi for CallApiGetter.
745   Register getter_address_reg = x2;
746
747   // Set up the call.
748   Address getter_address = v8::ToCData<Address>(callback->getter());
749   ApiFunction fun(getter_address);
750   ExternalReference::Type type = ExternalReference::DIRECT_GETTER_CALL;
751   ExternalReference ref = ExternalReference(&fun, type, isolate());
752   __ Mov(getter_address_reg, ref);
753
754   CallApiGetterStub stub(isolate());
755   __ TailCallStub(&stub);
756 }
757
758
759 void NamedLoadHandlerCompiler::GenerateLoadInterceptor(Register holder_reg,
760                                                        LookupResult* lookup,
761                                                        Handle<Name> name) {
762   DCHECK(!AreAliased(receiver(), this->name(),
763                      scratch1(), scratch2(), scratch3()));
764   DCHECK(holder()->HasNamedInterceptor());
765   DCHECK(!holder()->GetNamedInterceptor()->getter()->IsUndefined());
766
767   // So far the most popular follow ups for interceptor loads are FIELD
768   // and CALLBACKS, so inline only them, other cases may be added later.
769   bool compile_followup_inline = false;
770   if (lookup->IsFound() && lookup->IsCacheable()) {
771     if (lookup->IsField()) {
772       compile_followup_inline = true;
773     } else if (lookup->type() == CALLBACKS &&
774                lookup->GetCallbackObject()->IsExecutableAccessorInfo()) {
775       Handle<ExecutableAccessorInfo> callback(
776           ExecutableAccessorInfo::cast(lookup->GetCallbackObject()));
777       compile_followup_inline =
778           callback->getter() != NULL &&
779           ExecutableAccessorInfo::IsCompatibleReceiverType(isolate(), callback,
780                                                            type());
781     }
782   }
783
784   if (compile_followup_inline) {
785     // Compile the interceptor call, followed by inline code to load the
786     // property from further up the prototype chain if the call fails.
787     // Check that the maps haven't changed.
788     DCHECK(holder_reg.is(receiver()) || holder_reg.is(scratch1()));
789
790     // Preserve the receiver register explicitly whenever it is different from
791     // the holder and it is needed should the interceptor return without any
792     // result. The CALLBACKS case needs the receiver to be passed into C++ code,
793     // the FIELD case might cause a miss during the prototype check.
794     bool must_perfrom_prototype_check = *holder() != lookup->holder();
795     bool must_preserve_receiver_reg = !receiver().Is(holder_reg) &&
796         (lookup->type() == CALLBACKS || must_perfrom_prototype_check);
797
798     // Save necessary data before invoking an interceptor.
799     // Requires a frame to make GC aware of pushed pointers.
800     {
801       FrameScope frame_scope(masm(), StackFrame::INTERNAL);
802       if (must_preserve_receiver_reg) {
803         __ Push(receiver(), holder_reg, this->name());
804       } else {
805         __ Push(holder_reg, this->name());
806       }
807       // Invoke an interceptor.  Note: map checks from receiver to
808       // interceptor's holder has been compiled before (see a caller
809       // of this method.)
810       CompileCallLoadPropertyWithInterceptor(
811           masm(), receiver(), holder_reg, this->name(), holder(),
812           IC::kLoadPropertyWithInterceptorOnly);
813
814       // Check if interceptor provided a value for property.  If it's
815       // the case, return immediately.
816       Label interceptor_failed;
817       __ JumpIfRoot(x0,
818                     Heap::kNoInterceptorResultSentinelRootIndex,
819                     &interceptor_failed);
820       frame_scope.GenerateLeaveFrame();
821       __ Ret();
822
823       __ Bind(&interceptor_failed);
824       if (must_preserve_receiver_reg) {
825         __ Pop(this->name(), holder_reg, receiver());
826       } else {
827         __ Pop(this->name(), holder_reg);
828       }
829       // Leave the internal frame.
830     }
831     GenerateLoadPostInterceptor(holder_reg, name, lookup);
832   } else {  // !compile_followup_inline
833     // Call the runtime system to load the interceptor.
834     // Check that the maps haven't changed.
835     PushInterceptorArguments(masm(), receiver(), holder_reg, this->name(),
836                              holder());
837
838     ExternalReference ref =
839         ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptor),
840                           isolate());
841     __ TailCallExternalReference(
842         ref, NamedLoadHandlerCompiler::kInterceptorArgsLength, 1);
843   }
844 }
845
846
847 Handle<Code> NamedStoreHandlerCompiler::CompileStoreCallback(
848     Handle<JSObject> object, Handle<Name> name,
849     Handle<ExecutableAccessorInfo> callback) {
850   ASM_LOCATION("NamedStoreHandlerCompiler::CompileStoreCallback");
851   Register holder_reg = Frontend(receiver(), name);
852
853   // Stub never generated for non-global objects that require access checks.
854   DCHECK(holder()->IsJSGlobalProxy() || !holder()->IsAccessCheckNeeded());
855
856   // receiver() and holder_reg can alias.
857   DCHECK(!AreAliased(receiver(), scratch1(), scratch2(), value()));
858   DCHECK(!AreAliased(holder_reg, scratch1(), scratch2(), value()));
859   __ Mov(scratch1(), Operand(callback));
860   __ Mov(scratch2(), Operand(name));
861   __ Push(receiver(), holder_reg, scratch1(), scratch2(), value());
862
863   // Do tail-call to the runtime system.
864   ExternalReference store_callback_property =
865       ExternalReference(IC_Utility(IC::kStoreCallbackProperty), isolate());
866   __ TailCallExternalReference(store_callback_property, 5, 1);
867
868   // Return the generated code.
869   return GetCode(kind(), Code::FAST, name);
870 }
871
872
873 #undef __
874 #define __ ACCESS_MASM(masm)
875
876
877 void NamedStoreHandlerCompiler::GenerateStoreViaSetter(
878     MacroAssembler* masm, Handle<HeapType> type, Register receiver,
879     Handle<JSFunction> setter) {
880   // ----------- S t a t e -------------
881   //  -- lr    : return address
882   // -----------------------------------
883   Label miss;
884
885   {
886     FrameScope scope(masm, StackFrame::INTERNAL);
887
888     // Save value register, so we can restore it later.
889     __ Push(value());
890
891     if (!setter.is_null()) {
892       // Call the JavaScript setter with receiver and value on the stack.
893       if (IC::TypeToMap(*type, masm->isolate())->IsJSGlobalObjectMap()) {
894         // Swap in the global receiver.
895         __ Ldr(receiver,
896                FieldMemOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
897       }
898       __ Push(receiver, value());
899       ParameterCount actual(1);
900       ParameterCount expected(setter);
901       __ InvokeFunction(setter, expected, actual,
902                         CALL_FUNCTION, NullCallWrapper());
903     } else {
904       // If we generate a global code snippet for deoptimization only, remember
905       // the place to continue after deoptimization.
906       masm->isolate()->heap()->SetSetterStubDeoptPCOffset(masm->pc_offset());
907     }
908
909     // We have to return the passed value, not the return value of the setter.
910     __ Pop(x0);
911
912     // Restore context register.
913     __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
914   }
915   __ Ret();
916 }
917
918
919 #undef __
920 #define __ ACCESS_MASM(masm())
921
922
923 Handle<Code> NamedStoreHandlerCompiler::CompileStoreInterceptor(
924     Handle<Name> name) {
925   Label miss;
926
927   ASM_LOCATION("NamedStoreHandlerCompiler::CompileStoreInterceptor");
928
929   __ Push(receiver(), this->name(), value());
930
931   // Do tail-call to the runtime system.
932   ExternalReference store_ic_property = ExternalReference(
933       IC_Utility(IC::kStorePropertyWithInterceptor), isolate());
934   __ TailCallExternalReference(store_ic_property, 3, 1);
935
936   // Return the generated code.
937   return GetCode(kind(), Code::FAST, name);
938 }
939
940
941 // TODO(all): The so-called scratch registers are significant in some cases. For
942 // example, PropertyAccessCompiler::keyed_store_calling_convention()[3] (x3) is
943 // actually
944 // used for KeyedStoreCompiler::transition_map(). We should verify which
945 // registers are actually scratch registers, and which are important. For now,
946 // we use the same assignments as ARM to remain on the safe side.
947
948 Register* PropertyAccessCompiler::load_calling_convention() {
949   // receiver, name, scratch1, scratch2, scratch3, scratch4.
950   Register receiver = LoadIC::ReceiverRegister();
951   Register name = LoadIC::NameRegister();
952   static Register registers[] = { receiver, name, x3, x0, x4, x5 };
953   return registers;
954 }
955
956
957 Register* PropertyAccessCompiler::store_calling_convention() {
958   // receiver, value, scratch1, scratch2, scratch3.
959   Register receiver = StoreIC::ReceiverRegister();
960   Register name = StoreIC::NameRegister();
961   DCHECK(x3.is(KeyedStoreIC::MapRegister()));
962   static Register registers[] = { receiver, name, x3, x4, x5 };
963   return registers;
964 }
965
966
967 Register NamedStoreHandlerCompiler::value() { return StoreIC::ValueRegister(); }
968
969
970 #undef __
971 #define __ ACCESS_MASM(masm)
972
973 void NamedLoadHandlerCompiler::GenerateLoadViaGetter(
974     MacroAssembler* masm, Handle<HeapType> type, Register receiver,
975     Handle<JSFunction> getter) {
976   {
977     FrameScope scope(masm, StackFrame::INTERNAL);
978
979     if (!getter.is_null()) {
980       // Call the JavaScript getter with the receiver on the stack.
981       if (IC::TypeToMap(*type, masm->isolate())->IsJSGlobalObjectMap()) {
982         // Swap in the global receiver.
983         __ Ldr(receiver,
984                FieldMemOperand(receiver, JSGlobalObject::kGlobalProxyOffset));
985       }
986       __ Push(receiver);
987       ParameterCount actual(0);
988       ParameterCount expected(getter);
989       __ InvokeFunction(getter, expected, actual,
990                         CALL_FUNCTION, NullCallWrapper());
991     } else {
992       // If we generate a global code snippet for deoptimization only, remember
993       // the place to continue after deoptimization.
994       masm->isolate()->heap()->SetGetterStubDeoptPCOffset(masm->pc_offset());
995     }
996
997     // Restore context register.
998     __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
999   }
1000   __ Ret();
1001 }
1002
1003
1004 #undef __
1005 #define __ ACCESS_MASM(masm())
1006
1007
1008 Handle<Code> NamedLoadHandlerCompiler::CompileLoadGlobal(
1009     Handle<PropertyCell> cell, Handle<Name> name, bool is_configurable) {
1010   Label miss;
1011   FrontendHeader(receiver(), name, &miss);
1012
1013   // Get the value from the cell.
1014   Register result = StoreIC::ValueRegister();
1015   __ Mov(result, Operand(cell));
1016   __ Ldr(result, FieldMemOperand(result, Cell::kValueOffset));
1017
1018   // Check for deleted property if property can actually be deleted.
1019   if (is_configurable) {
1020     __ JumpIfRoot(result, Heap::kTheHoleValueRootIndex, &miss);
1021   }
1022
1023   Counters* counters = isolate()->counters();
1024   __ IncrementCounter(counters->named_load_global_stub(), 1, x1, x3);
1025   __ Ret();
1026
1027   FrontendFooter(name, &miss);
1028
1029   // Return the generated code.
1030   return GetCode(kind(), Code::NORMAL, name);
1031 }
1032
1033
1034 Handle<Code> PropertyICCompiler::CompilePolymorphic(TypeHandleList* types,
1035                                                     CodeHandleList* handlers,
1036                                                     Handle<Name> name,
1037                                                     Code::StubType type,
1038                                                     IcCheckType check) {
1039   Label miss;
1040
1041   if (check == PROPERTY &&
1042       (kind() == Code::KEYED_LOAD_IC || kind() == Code::KEYED_STORE_IC)) {
1043     // In case we are compiling an IC for dictionary loads and stores, just
1044     // check whether the name is unique.
1045     if (name.is_identical_to(isolate()->factory()->normal_ic_symbol())) {
1046       __ JumpIfNotUniqueName(this->name(), &miss);
1047     } else {
1048       __ CompareAndBranch(this->name(), Operand(name), ne, &miss);
1049     }
1050   }
1051
1052   Label number_case;
1053   Label* smi_target = IncludesNumberType(types) ? &number_case : &miss;
1054   __ JumpIfSmi(receiver(), smi_target);
1055
1056   // Polymorphic keyed stores may use the map register
1057   Register map_reg = scratch1();
1058   DCHECK(kind() != Code::KEYED_STORE_IC ||
1059          map_reg.is(KeyedStoreIC::MapRegister()));
1060   __ Ldr(map_reg, FieldMemOperand(receiver(), HeapObject::kMapOffset));
1061   int receiver_count = types->length();
1062   int number_of_handled_maps = 0;
1063   for (int current = 0; current < receiver_count; ++current) {
1064     Handle<HeapType> type = types->at(current);
1065     Handle<Map> map = IC::TypeToMap(*type, isolate());
1066     if (!map->is_deprecated()) {
1067       number_of_handled_maps++;
1068       Label try_next;
1069       __ Cmp(map_reg, Operand(map));
1070       __ B(ne, &try_next);
1071       if (type->Is(HeapType::Number())) {
1072         DCHECK(!number_case.is_unused());
1073         __ Bind(&number_case);
1074       }
1075       __ Jump(handlers->at(current), RelocInfo::CODE_TARGET);
1076       __ Bind(&try_next);
1077     }
1078   }
1079   DCHECK(number_of_handled_maps != 0);
1080
1081   __ Bind(&miss);
1082   TailCallBuiltin(masm(), MissBuiltin(kind()));
1083
1084   // Return the generated code.
1085   InlineCacheState state =
1086       (number_of_handled_maps > 1) ? POLYMORPHIC : MONOMORPHIC;
1087   return GetCode(kind(), type, name, state);
1088 }
1089
1090
1091 Handle<Code> PropertyICCompiler::CompileKeyedStorePolymorphic(
1092     MapHandleList* receiver_maps, CodeHandleList* handler_stubs,
1093     MapHandleList* transitioned_maps) {
1094   Label miss;
1095
1096   ASM_LOCATION("PropertyICCompiler::CompileStorePolymorphic");
1097
1098   __ JumpIfSmi(receiver(), &miss);
1099
1100   int receiver_count = receiver_maps->length();
1101   __ Ldr(scratch1(), FieldMemOperand(receiver(), HeapObject::kMapOffset));
1102   for (int i = 0; i < receiver_count; i++) {
1103     __ Cmp(scratch1(), Operand(receiver_maps->at(i)));
1104
1105     Label skip;
1106     __ B(&skip, ne);
1107     if (!transitioned_maps->at(i).is_null()) {
1108       // This argument is used by the handler stub. For example, see
1109       // ElementsTransitionGenerator::GenerateMapChangeElementsTransition.
1110       __ Mov(transition_map(), Operand(transitioned_maps->at(i)));
1111     }
1112     __ Jump(handler_stubs->at(i), RelocInfo::CODE_TARGET);
1113     __ Bind(&skip);
1114   }
1115
1116   __ Bind(&miss);
1117   TailCallBuiltin(masm(), MissBuiltin(kind()));
1118
1119   return GetCode(kind(), Code::NORMAL, factory()->empty_string(), POLYMORPHIC);
1120 }
1121
1122
1123 #undef __
1124 #define __ ACCESS_MASM(masm)
1125
1126 void ElementHandlerCompiler::GenerateLoadDictionaryElement(
1127     MacroAssembler* masm) {
1128   // The return address is in lr.
1129   Label slow, miss;
1130
1131   Register result = x0;
1132   Register key = LoadIC::NameRegister();
1133   Register receiver = LoadIC::ReceiverRegister();
1134   DCHECK(receiver.is(x1));
1135   DCHECK(key.is(x2));
1136
1137   __ JumpIfNotSmi(key, &miss);
1138   __ Ldr(x4, FieldMemOperand(receiver, JSObject::kElementsOffset));
1139   __ LoadFromNumberDictionary(&slow, x4, key, result, x7, x3, x5, x6);
1140   __ Ret();
1141
1142   __ Bind(&slow);
1143   __ IncrementCounter(
1144       masm->isolate()->counters()->keyed_load_external_array_slow(), 1, x4, x3);
1145   TailCallBuiltin(masm, Builtins::kKeyedLoadIC_Slow);
1146
1147   // Miss case, call the runtime.
1148   __ Bind(&miss);
1149   TailCallBuiltin(masm, Builtins::kKeyedLoadIC_Miss);
1150 }
1151
1152
1153 } }  // namespace v8::internal
1154
1155 #endif  // V8_TARGET_ARCH_ARM64