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