deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / mips64 / macro-assembler-mips64.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 <limits.h>  // For LONG_MIN, LONG_MAX.
6
7 #include "src/v8.h"
8
9 #if V8_TARGET_ARCH_MIPS64
10
11 #include "src/base/division-by-constant.h"
12 #include "src/bootstrapper.h"
13 #include "src/codegen.h"
14 #include "src/cpu-profiler.h"
15 #include "src/debug.h"
16 #include "src/isolate-inl.h"
17 #include "src/runtime/runtime.h"
18
19 namespace v8 {
20 namespace internal {
21
22 MacroAssembler::MacroAssembler(Isolate* arg_isolate, void* buffer, int size)
23     : Assembler(arg_isolate, buffer, size),
24       generating_stub_(false),
25       has_frame_(false),
26       has_double_zero_reg_set_(false) {
27   if (isolate() != NULL) {
28     code_object_ = Handle<Object>(isolate()->heap()->undefined_value(),
29                                   isolate());
30   }
31 }
32
33
34 void MacroAssembler::Load(Register dst,
35                           const MemOperand& src,
36                           Representation r) {
37   DCHECK(!r.IsDouble());
38   if (r.IsInteger8()) {
39     lb(dst, src);
40   } else if (r.IsUInteger8()) {
41     lbu(dst, src);
42   } else if (r.IsInteger16()) {
43     lh(dst, src);
44   } else if (r.IsUInteger16()) {
45     lhu(dst, src);
46   } else if (r.IsInteger32()) {
47     lw(dst, src);
48   } else {
49     ld(dst, src);
50   }
51 }
52
53
54 void MacroAssembler::Store(Register src,
55                            const MemOperand& dst,
56                            Representation r) {
57   DCHECK(!r.IsDouble());
58   if (r.IsInteger8() || r.IsUInteger8()) {
59     sb(src, dst);
60   } else if (r.IsInteger16() || r.IsUInteger16()) {
61     sh(src, dst);
62   } else if (r.IsInteger32()) {
63     sw(src, dst);
64   } else {
65     if (r.IsHeapObject()) {
66       AssertNotSmi(src);
67     } else if (r.IsSmi()) {
68       AssertSmi(src);
69     }
70     sd(src, dst);
71   }
72 }
73
74
75 void MacroAssembler::LoadRoot(Register destination,
76                               Heap::RootListIndex index) {
77   ld(destination, MemOperand(s6, index << kPointerSizeLog2));
78 }
79
80
81 void MacroAssembler::LoadRoot(Register destination,
82                               Heap::RootListIndex index,
83                               Condition cond,
84                               Register src1, const Operand& src2) {
85   Branch(2, NegateCondition(cond), src1, src2);
86   ld(destination, MemOperand(s6, index << kPointerSizeLog2));
87 }
88
89
90 void MacroAssembler::StoreRoot(Register source,
91                                Heap::RootListIndex index) {
92   DCHECK(Heap::RootCanBeWrittenAfterInitialization(index));
93   sd(source, MemOperand(s6, index << kPointerSizeLog2));
94 }
95
96
97 void MacroAssembler::StoreRoot(Register source,
98                                Heap::RootListIndex index,
99                                Condition cond,
100                                Register src1, const Operand& src2) {
101   DCHECK(Heap::RootCanBeWrittenAfterInitialization(index));
102   Branch(2, NegateCondition(cond), src1, src2);
103   sd(source, MemOperand(s6, index << kPointerSizeLog2));
104 }
105
106
107 // Push and pop all registers that can hold pointers.
108 void MacroAssembler::PushSafepointRegisters() {
109   // Safepoints expect a block of kNumSafepointRegisters values on the
110   // stack, so adjust the stack for unsaved registers.
111   const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
112   DCHECK(num_unsaved >= 0);
113   if (num_unsaved > 0) {
114     Dsubu(sp, sp, Operand(num_unsaved * kPointerSize));
115   }
116   MultiPush(kSafepointSavedRegisters);
117 }
118
119
120 void MacroAssembler::PopSafepointRegisters() {
121   const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
122   MultiPop(kSafepointSavedRegisters);
123   if (num_unsaved > 0) {
124     Daddu(sp, sp, Operand(num_unsaved * kPointerSize));
125   }
126 }
127
128
129 void MacroAssembler::StoreToSafepointRegisterSlot(Register src, Register dst) {
130   sd(src, SafepointRegisterSlot(dst));
131 }
132
133
134 void MacroAssembler::LoadFromSafepointRegisterSlot(Register dst, Register src) {
135   ld(dst, SafepointRegisterSlot(src));
136 }
137
138
139 int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
140   // The registers are pushed starting with the highest encoding,
141   // which means that lowest encodings are closest to the stack pointer.
142   return kSafepointRegisterStackIndexMap[reg_code];
143 }
144
145
146 MemOperand MacroAssembler::SafepointRegisterSlot(Register reg) {
147   return MemOperand(sp, SafepointRegisterStackIndex(reg.code()) * kPointerSize);
148 }
149
150
151 MemOperand MacroAssembler::SafepointRegistersAndDoublesSlot(Register reg) {
152   UNIMPLEMENTED_MIPS();
153   // General purpose registers are pushed last on the stack.
154   int doubles_size = FPURegister::NumAllocatableRegisters() * kDoubleSize;
155   int register_offset = SafepointRegisterStackIndex(reg.code()) * kPointerSize;
156   return MemOperand(sp, doubles_size + register_offset);
157 }
158
159
160 void MacroAssembler::InNewSpace(Register object,
161                                 Register scratch,
162                                 Condition cc,
163                                 Label* branch) {
164   DCHECK(cc == eq || cc == ne);
165   And(scratch, object, Operand(ExternalReference::new_space_mask(isolate())));
166   Branch(branch, cc, scratch,
167          Operand(ExternalReference::new_space_start(isolate())));
168 }
169
170
171 void MacroAssembler::RecordWriteField(
172     Register object,
173     int offset,
174     Register value,
175     Register dst,
176     RAStatus ra_status,
177     SaveFPRegsMode save_fp,
178     RememberedSetAction remembered_set_action,
179     SmiCheck smi_check,
180     PointersToHereCheck pointers_to_here_check_for_value) {
181   DCHECK(!AreAliased(value, dst, t8, object));
182   // First, check if a write barrier is even needed. The tests below
183   // catch stores of Smis.
184   Label done;
185
186   // Skip barrier if writing a smi.
187   if (smi_check == INLINE_SMI_CHECK) {
188     JumpIfSmi(value, &done);
189   }
190
191   // Although the object register is tagged, the offset is relative to the start
192   // of the object, so so offset must be a multiple of kPointerSize.
193   DCHECK(IsAligned(offset, kPointerSize));
194
195   Daddu(dst, object, Operand(offset - kHeapObjectTag));
196   if (emit_debug_code()) {
197     Label ok;
198     And(t8, dst, Operand((1 << kPointerSizeLog2) - 1));
199     Branch(&ok, eq, t8, Operand(zero_reg));
200     stop("Unaligned cell in write barrier");
201     bind(&ok);
202   }
203
204   RecordWrite(object,
205               dst,
206               value,
207               ra_status,
208               save_fp,
209               remembered_set_action,
210               OMIT_SMI_CHECK,
211               pointers_to_here_check_for_value);
212
213   bind(&done);
214
215   // Clobber clobbered input registers when running with the debug-code flag
216   // turned on to provoke errors.
217   if (emit_debug_code()) {
218     li(value, Operand(bit_cast<int64_t>(kZapValue + 4)));
219     li(dst, Operand(bit_cast<int64_t>(kZapValue + 8)));
220   }
221 }
222
223
224 // Will clobber 4 registers: object, map, dst, ip.  The
225 // register 'object' contains a heap object pointer.
226 void MacroAssembler::RecordWriteForMap(Register object,
227                                        Register map,
228                                        Register dst,
229                                        RAStatus ra_status,
230                                        SaveFPRegsMode fp_mode) {
231   if (emit_debug_code()) {
232     DCHECK(!dst.is(at));
233     ld(dst, FieldMemOperand(map, HeapObject::kMapOffset));
234     Check(eq,
235           kWrongAddressOrValuePassedToRecordWrite,
236           dst,
237           Operand(isolate()->factory()->meta_map()));
238   }
239
240   if (!FLAG_incremental_marking) {
241     return;
242   }
243
244   if (emit_debug_code()) {
245     ld(at, FieldMemOperand(object, HeapObject::kMapOffset));
246     Check(eq,
247           kWrongAddressOrValuePassedToRecordWrite,
248           map,
249           Operand(at));
250   }
251
252   Label done;
253
254   // A single check of the map's pages interesting flag suffices, since it is
255   // only set during incremental collection, and then it's also guaranteed that
256   // the from object's page's interesting flag is also set.  This optimization
257   // relies on the fact that maps can never be in new space.
258   CheckPageFlag(map,
259                 map,  // Used as scratch.
260                 MemoryChunk::kPointersToHereAreInterestingMask,
261                 eq,
262                 &done);
263
264   Daddu(dst, object, Operand(HeapObject::kMapOffset - kHeapObjectTag));
265   if (emit_debug_code()) {
266     Label ok;
267     And(at, dst, Operand((1 << kPointerSizeLog2) - 1));
268     Branch(&ok, eq, at, Operand(zero_reg));
269     stop("Unaligned cell in write barrier");
270     bind(&ok);
271   }
272
273   // Record the actual write.
274   if (ra_status == kRAHasNotBeenSaved) {
275     push(ra);
276   }
277   RecordWriteStub stub(isolate(), object, map, dst, OMIT_REMEMBERED_SET,
278                        fp_mode);
279   CallStub(&stub);
280   if (ra_status == kRAHasNotBeenSaved) {
281     pop(ra);
282   }
283
284   bind(&done);
285
286   // Count number of write barriers in generated code.
287   isolate()->counters()->write_barriers_static()->Increment();
288   IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, at, dst);
289
290   // Clobber clobbered registers when running with the debug-code flag
291   // turned on to provoke errors.
292   if (emit_debug_code()) {
293     li(dst, Operand(bit_cast<int64_t>(kZapValue + 12)));
294     li(map, Operand(bit_cast<int64_t>(kZapValue + 16)));
295   }
296 }
297
298
299 // Will clobber 4 registers: object, address, scratch, ip.  The
300 // register 'object' contains a heap object pointer.  The heap object
301 // tag is shifted away.
302 void MacroAssembler::RecordWrite(
303     Register object,
304     Register address,
305     Register value,
306     RAStatus ra_status,
307     SaveFPRegsMode fp_mode,
308     RememberedSetAction remembered_set_action,
309     SmiCheck smi_check,
310     PointersToHereCheck pointers_to_here_check_for_value) {
311   DCHECK(!AreAliased(object, address, value, t8));
312   DCHECK(!AreAliased(object, address, value, t9));
313
314   if (emit_debug_code()) {
315     ld(at, MemOperand(address));
316     Assert(
317         eq, kWrongAddressOrValuePassedToRecordWrite, at, Operand(value));
318   }
319
320   if (remembered_set_action == OMIT_REMEMBERED_SET &&
321       !FLAG_incremental_marking) {
322     return;
323   }
324
325   // First, check if a write barrier is even needed. The tests below
326   // catch stores of smis and stores into the young generation.
327   Label done;
328
329   if (smi_check == INLINE_SMI_CHECK) {
330     DCHECK_EQ(0, kSmiTag);
331     JumpIfSmi(value, &done);
332   }
333
334   if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
335     CheckPageFlag(value,
336                   value,  // Used as scratch.
337                   MemoryChunk::kPointersToHereAreInterestingMask,
338                   eq,
339                   &done);
340   }
341   CheckPageFlag(object,
342                 value,  // Used as scratch.
343                 MemoryChunk::kPointersFromHereAreInterestingMask,
344                 eq,
345                 &done);
346
347   // Record the actual write.
348   if (ra_status == kRAHasNotBeenSaved) {
349     push(ra);
350   }
351   RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
352                        fp_mode);
353   CallStub(&stub);
354   if (ra_status == kRAHasNotBeenSaved) {
355     pop(ra);
356   }
357
358   bind(&done);
359
360   // Count number of write barriers in generated code.
361   isolate()->counters()->write_barriers_static()->Increment();
362   IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, at,
363                    value);
364
365   // Clobber clobbered registers when running with the debug-code flag
366   // turned on to provoke errors.
367   if (emit_debug_code()) {
368     li(address, Operand(bit_cast<int64_t>(kZapValue + 12)));
369     li(value, Operand(bit_cast<int64_t>(kZapValue + 16)));
370   }
371 }
372
373
374 void MacroAssembler::RememberedSetHelper(Register object,  // For debug tests.
375                                          Register address,
376                                          Register scratch,
377                                          SaveFPRegsMode fp_mode,
378                                          RememberedSetFinalAction and_then) {
379   Label done;
380   if (emit_debug_code()) {
381     Label ok;
382     JumpIfNotInNewSpace(object, scratch, &ok);
383     stop("Remembered set pointer is in new space");
384     bind(&ok);
385   }
386   // Load store buffer top.
387   ExternalReference store_buffer =
388       ExternalReference::store_buffer_top(isolate());
389   li(t8, Operand(store_buffer));
390   ld(scratch, MemOperand(t8));
391   // Store pointer to buffer and increment buffer top.
392   sd(address, MemOperand(scratch));
393   Daddu(scratch, scratch, kPointerSize);
394   // Write back new top of buffer.
395   sd(scratch, MemOperand(t8));
396   // Call stub on end of buffer.
397   // Check for end of buffer.
398   And(t8, scratch, Operand(StoreBuffer::kStoreBufferOverflowBit));
399   DCHECK(!scratch.is(t8));
400   if (and_then == kFallThroughAtEnd) {
401     Branch(&done, eq, t8, Operand(zero_reg));
402   } else {
403     DCHECK(and_then == kReturnAtEnd);
404     Ret(eq, t8, Operand(zero_reg));
405   }
406   push(ra);
407   StoreBufferOverflowStub store_buffer_overflow(isolate(), fp_mode);
408   CallStub(&store_buffer_overflow);
409   pop(ra);
410   bind(&done);
411   if (and_then == kReturnAtEnd) {
412     Ret();
413   }
414 }
415
416
417 // -----------------------------------------------------------------------------
418 // Allocation support.
419
420
421 void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
422                                             Register scratch,
423                                             Label* miss) {
424   Label same_contexts;
425
426   DCHECK(!holder_reg.is(scratch));
427   DCHECK(!holder_reg.is(at));
428   DCHECK(!scratch.is(at));
429
430   // Load current lexical context from the stack frame.
431   ld(scratch, MemOperand(fp, StandardFrameConstants::kContextOffset));
432   // In debug mode, make sure the lexical context is set.
433 #ifdef DEBUG
434   Check(ne, kWeShouldNotHaveAnEmptyLexicalContext,
435       scratch, Operand(zero_reg));
436 #endif
437
438   // Load the native context of the current context.
439   int offset =
440       Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize;
441   ld(scratch, FieldMemOperand(scratch, offset));
442   ld(scratch, FieldMemOperand(scratch, GlobalObject::kNativeContextOffset));
443
444   // Check the context is a native context.
445   if (emit_debug_code()) {
446     push(holder_reg);  // Temporarily save holder on the stack.
447     // Read the first word and compare to the native_context_map.
448     ld(holder_reg, FieldMemOperand(scratch, HeapObject::kMapOffset));
449     LoadRoot(at, Heap::kNativeContextMapRootIndex);
450     Check(eq, kJSGlobalObjectNativeContextShouldBeANativeContext,
451           holder_reg, Operand(at));
452     pop(holder_reg);  // Restore holder.
453   }
454
455   // Check if both contexts are the same.
456   ld(at, FieldMemOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
457   Branch(&same_contexts, eq, scratch, Operand(at));
458
459   // Check the context is a native context.
460   if (emit_debug_code()) {
461     push(holder_reg);  // Temporarily save holder on the stack.
462     mov(holder_reg, at);  // Move at to its holding place.
463     LoadRoot(at, Heap::kNullValueRootIndex);
464     Check(ne, kJSGlobalProxyContextShouldNotBeNull,
465           holder_reg, Operand(at));
466
467     ld(holder_reg, FieldMemOperand(holder_reg, HeapObject::kMapOffset));
468     LoadRoot(at, Heap::kNativeContextMapRootIndex);
469     Check(eq, kJSGlobalObjectNativeContextShouldBeANativeContext,
470           holder_reg, Operand(at));
471     // Restore at is not needed. at is reloaded below.
472     pop(holder_reg);  // Restore holder.
473     // Restore at to holder's context.
474     ld(at, FieldMemOperand(holder_reg, JSGlobalProxy::kNativeContextOffset));
475   }
476
477   // Check that the security token in the calling global object is
478   // compatible with the security token in the receiving global
479   // object.
480   int token_offset = Context::kHeaderSize +
481                      Context::SECURITY_TOKEN_INDEX * kPointerSize;
482
483   ld(scratch, FieldMemOperand(scratch, token_offset));
484   ld(at, FieldMemOperand(at, token_offset));
485   Branch(miss, ne, scratch, Operand(at));
486
487   bind(&same_contexts);
488 }
489
490
491 // Compute the hash code from the untagged key.  This must be kept in sync with
492 // ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
493 // code-stub-hydrogen.cc
494 void MacroAssembler::GetNumberHash(Register reg0, Register scratch) {
495   // First of all we assign the hash seed to scratch.
496   LoadRoot(scratch, Heap::kHashSeedRootIndex);
497   SmiUntag(scratch);
498
499   // Xor original key with a seed.
500   xor_(reg0, reg0, scratch);
501
502   // Compute the hash code from the untagged key.  This must be kept in sync
503   // with ComputeIntegerHash in utils.h.
504   //
505   // hash = ~hash + (hash << 15);
506   // The algorithm uses 32-bit integer values.
507   nor(scratch, reg0, zero_reg);
508   sll(at, reg0, 15);
509   addu(reg0, scratch, at);
510
511   // hash = hash ^ (hash >> 12);
512   srl(at, reg0, 12);
513   xor_(reg0, reg0, at);
514
515   // hash = hash + (hash << 2);
516   sll(at, reg0, 2);
517   addu(reg0, reg0, at);
518
519   // hash = hash ^ (hash >> 4);
520   srl(at, reg0, 4);
521   xor_(reg0, reg0, at);
522
523   // hash = hash * 2057;
524   sll(scratch, reg0, 11);
525   sll(at, reg0, 3);
526   addu(reg0, reg0, at);
527   addu(reg0, reg0, scratch);
528
529   // hash = hash ^ (hash >> 16);
530   srl(at, reg0, 16);
531   xor_(reg0, reg0, at);
532 }
533
534
535 void MacroAssembler::LoadFromNumberDictionary(Label* miss,
536                                               Register elements,
537                                               Register key,
538                                               Register result,
539                                               Register reg0,
540                                               Register reg1,
541                                               Register reg2) {
542   // Register use:
543   //
544   // elements - holds the slow-case elements of the receiver on entry.
545   //            Unchanged unless 'result' is the same register.
546   //
547   // key      - holds the smi key on entry.
548   //            Unchanged unless 'result' is the same register.
549   //
550   //
551   // result   - holds the result on exit if the load succeeded.
552   //            Allowed to be the same as 'key' or 'result'.
553   //            Unchanged on bailout so 'key' or 'result' can be used
554   //            in further computation.
555   //
556   // Scratch registers:
557   //
558   // reg0 - holds the untagged key on entry and holds the hash once computed.
559   //
560   // reg1 - Used to hold the capacity mask of the dictionary.
561   //
562   // reg2 - Used for the index into the dictionary.
563   // at   - Temporary (avoid MacroAssembler instructions also using 'at').
564   Label done;
565
566   GetNumberHash(reg0, reg1);
567
568   // Compute the capacity mask.
569   ld(reg1, FieldMemOperand(elements, SeededNumberDictionary::kCapacityOffset));
570   SmiUntag(reg1, reg1);
571   Dsubu(reg1, reg1, Operand(1));
572
573   // Generate an unrolled loop that performs a few probes before giving up.
574   for (int i = 0; i < kNumberDictionaryProbes; i++) {
575     // Use reg2 for index calculations and keep the hash intact in reg0.
576     mov(reg2, reg0);
577     // Compute the masked index: (hash + i + i * i) & mask.
578     if (i > 0) {
579       Daddu(reg2, reg2, Operand(SeededNumberDictionary::GetProbeOffset(i)));
580     }
581     and_(reg2, reg2, reg1);
582
583     // Scale the index by multiplying by the element size.
584     DCHECK(SeededNumberDictionary::kEntrySize == 3);
585     dsll(at, reg2, 1);  // 2x.
586     daddu(reg2, reg2, at);  // reg2 = reg2 * 3.
587
588     // Check if the key is identical to the name.
589     dsll(at, reg2, kPointerSizeLog2);
590     daddu(reg2, elements, at);
591
592     ld(at, FieldMemOperand(reg2, SeededNumberDictionary::kElementsStartOffset));
593     if (i != kNumberDictionaryProbes - 1) {
594       Branch(&done, eq, key, Operand(at));
595     } else {
596       Branch(miss, ne, key, Operand(at));
597     }
598   }
599
600   bind(&done);
601   // Check that the value is a field property.
602   // reg2: elements + (index * kPointerSize).
603   const int kDetailsOffset =
604       SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
605   ld(reg1, FieldMemOperand(reg2, kDetailsOffset));
606   DCHECK_EQ(DATA, 0);
607   And(at, reg1, Operand(Smi::FromInt(PropertyDetails::TypeField::kMask)));
608   Branch(miss, ne, at, Operand(zero_reg));
609
610   // Get the value at the masked, scaled index and return.
611   const int kValueOffset =
612       SeededNumberDictionary::kElementsStartOffset + kPointerSize;
613   ld(result, FieldMemOperand(reg2, kValueOffset));
614 }
615
616
617 // ---------------------------------------------------------------------------
618 // Instruction macros.
619
620 void MacroAssembler::Addu(Register rd, Register rs, const Operand& rt) {
621   if (rt.is_reg()) {
622     addu(rd, rs, rt.rm());
623   } else {
624     if (is_int16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
625       addiu(rd, rs, rt.imm64_);
626     } else {
627       // li handles the relocation.
628       DCHECK(!rs.is(at));
629       li(at, rt);
630       addu(rd, rs, at);
631     }
632   }
633 }
634
635
636 void MacroAssembler::Daddu(Register rd, Register rs, const Operand& rt) {
637   if (rt.is_reg()) {
638     daddu(rd, rs, rt.rm());
639   } else {
640     if (is_int16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
641       daddiu(rd, rs, rt.imm64_);
642     } else {
643       // li handles the relocation.
644       DCHECK(!rs.is(at));
645       li(at, rt);
646       daddu(rd, rs, at);
647     }
648   }
649 }
650
651
652 void MacroAssembler::Subu(Register rd, Register rs, const Operand& rt) {
653   if (rt.is_reg()) {
654     subu(rd, rs, rt.rm());
655   } else {
656     if (is_int16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
657       addiu(rd, rs, -rt.imm64_);  // No subiu instr, use addiu(x, y, -imm).
658     } else {
659       // li handles the relocation.
660       DCHECK(!rs.is(at));
661       li(at, rt);
662       subu(rd, rs, at);
663     }
664   }
665 }
666
667
668 void MacroAssembler::Dsubu(Register rd, Register rs, const Operand& rt) {
669   if (rt.is_reg()) {
670     dsubu(rd, rs, rt.rm());
671   } else {
672     if (is_int16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
673       daddiu(rd, rs, -rt.imm64_);  // No subiu instr, use addiu(x, y, -imm).
674     } else {
675       // li handles the relocation.
676       DCHECK(!rs.is(at));
677       li(at, rt);
678       dsubu(rd, rs, at);
679     }
680   }
681 }
682
683
684 void MacroAssembler::Mul(Register rd, Register rs, const Operand& rt) {
685   if (rt.is_reg()) {
686     mul(rd, rs, rt.rm());
687   } else {
688     // li handles the relocation.
689     DCHECK(!rs.is(at));
690     li(at, rt);
691     mul(rd, rs, at);
692   }
693 }
694
695
696 void MacroAssembler::Mulh(Register rd, Register rs, const Operand& rt) {
697   if (rt.is_reg()) {
698     if (kArchVariant != kMips64r6) {
699       mult(rs, rt.rm());
700       mfhi(rd);
701     } else {
702       muh(rd, rs, rt.rm());
703     }
704   } else {
705     // li handles the relocation.
706     DCHECK(!rs.is(at));
707     li(at, rt);
708     if (kArchVariant != kMips64r6) {
709       mult(rs, at);
710       mfhi(rd);
711     } else {
712       muh(rd, rs, at);
713     }
714   }
715 }
716
717
718 void MacroAssembler::Mulhu(Register rd, Register rs, const Operand& rt) {
719   if (rt.is_reg()) {
720     if (kArchVariant != kMips64r6) {
721       multu(rs, rt.rm());
722       mfhi(rd);
723     } else {
724       muhu(rd, rs, rt.rm());
725     }
726   } else {
727     // li handles the relocation.
728     DCHECK(!rs.is(at));
729     li(at, rt);
730     if (kArchVariant != kMips64r6) {
731       multu(rs, at);
732       mfhi(rd);
733     } else {
734       muhu(rd, rs, at);
735     }
736   }
737 }
738
739
740 void MacroAssembler::Dmul(Register rd, Register rs, const Operand& rt) {
741   if (rt.is_reg()) {
742     if (kArchVariant == kMips64r6) {
743       dmul(rd, rs, rt.rm());
744     } else {
745       dmult(rs, rt.rm());
746       mflo(rd);
747     }
748   } else {
749     // li handles the relocation.
750     DCHECK(!rs.is(at));
751     li(at, rt);
752     if (kArchVariant == kMips64r6) {
753       dmul(rd, rs, at);
754     } else {
755       dmult(rs, at);
756       mflo(rd);
757     }
758   }
759 }
760
761
762 void MacroAssembler::Dmulh(Register rd, Register rs, const Operand& rt) {
763   if (rt.is_reg()) {
764     if (kArchVariant == kMips64r6) {
765       dmuh(rd, rs, rt.rm());
766     } else {
767       dmult(rs, rt.rm());
768       mfhi(rd);
769     }
770   } else {
771     // li handles the relocation.
772     DCHECK(!rs.is(at));
773     li(at, rt);
774     if (kArchVariant == kMips64r6) {
775       dmuh(rd, rs, at);
776     } else {
777       dmult(rs, at);
778       mfhi(rd);
779     }
780   }
781 }
782
783
784 void MacroAssembler::Mult(Register rs, const Operand& rt) {
785   if (rt.is_reg()) {
786     mult(rs, rt.rm());
787   } else {
788     // li handles the relocation.
789     DCHECK(!rs.is(at));
790     li(at, rt);
791     mult(rs, at);
792   }
793 }
794
795
796 void MacroAssembler::Dmult(Register rs, const Operand& rt) {
797   if (rt.is_reg()) {
798     dmult(rs, rt.rm());
799   } else {
800     // li handles the relocation.
801     DCHECK(!rs.is(at));
802     li(at, rt);
803     dmult(rs, at);
804   }
805 }
806
807
808 void MacroAssembler::Multu(Register rs, const Operand& rt) {
809   if (rt.is_reg()) {
810     multu(rs, rt.rm());
811   } else {
812     // li handles the relocation.
813     DCHECK(!rs.is(at));
814     li(at, rt);
815     multu(rs, at);
816   }
817 }
818
819
820 void MacroAssembler::Dmultu(Register rs, const Operand& rt) {
821   if (rt.is_reg()) {
822     dmultu(rs, rt.rm());
823   } else {
824     // li handles the relocation.
825     DCHECK(!rs.is(at));
826     li(at, rt);
827     dmultu(rs, at);
828   }
829 }
830
831
832 void MacroAssembler::Div(Register rs, const Operand& rt) {
833   if (rt.is_reg()) {
834     div(rs, rt.rm());
835   } else {
836     // li handles the relocation.
837     DCHECK(!rs.is(at));
838     li(at, rt);
839     div(rs, at);
840   }
841 }
842
843
844 void MacroAssembler::Div(Register res, Register rs, const Operand& rt) {
845   if (rt.is_reg()) {
846     if (kArchVariant != kMips64r6) {
847       div(rs, rt.rm());
848       mflo(res);
849     } else {
850       div(res, rs, rt.rm());
851     }
852   } else {
853     // li handles the relocation.
854     DCHECK(!rs.is(at));
855     li(at, rt);
856     if (kArchVariant != kMips64r6) {
857       div(rs, at);
858       mflo(res);
859     } else {
860       div(res, rs, at);
861     }
862   }
863 }
864
865
866 void MacroAssembler::Mod(Register rd, Register rs, const Operand& rt) {
867   if (rt.is_reg()) {
868     if (kArchVariant != kMips64r6) {
869       div(rs, rt.rm());
870       mfhi(rd);
871     } else {
872       mod(rd, rs, rt.rm());
873     }
874   } else {
875     // li handles the relocation.
876     DCHECK(!rs.is(at));
877     li(at, rt);
878     if (kArchVariant != kMips64r6) {
879       div(rs, at);
880       mfhi(rd);
881     } else {
882       mod(rd, rs, at);
883     }
884   }
885 }
886
887
888 void MacroAssembler::Modu(Register rd, Register rs, const Operand& rt) {
889   if (rt.is_reg()) {
890     if (kArchVariant != kMips64r6) {
891       divu(rs, rt.rm());
892       mfhi(rd);
893     } else {
894       modu(rd, rs, rt.rm());
895     }
896   } else {
897     // li handles the relocation.
898     DCHECK(!rs.is(at));
899     li(at, rt);
900     if (kArchVariant != kMips64r6) {
901       divu(rs, at);
902       mfhi(rd);
903     } else {
904       modu(rd, rs, at);
905     }
906   }
907 }
908
909
910 void MacroAssembler::Ddiv(Register rs, const Operand& rt) {
911   if (rt.is_reg()) {
912     ddiv(rs, rt.rm());
913   } else {
914     // li handles the relocation.
915     DCHECK(!rs.is(at));
916     li(at, rt);
917     ddiv(rs, at);
918   }
919 }
920
921
922 void MacroAssembler::Ddiv(Register rd, Register rs, const Operand& rt) {
923   if (kArchVariant != kMips64r6) {
924     if (rt.is_reg()) {
925       ddiv(rs, rt.rm());
926       mflo(rd);
927     } else {
928       // li handles the relocation.
929       DCHECK(!rs.is(at));
930       li(at, rt);
931       ddiv(rs, at);
932       mflo(rd);
933     }
934   } else {
935     if (rt.is_reg()) {
936       ddiv(rd, rs, rt.rm());
937     } else {
938       // li handles the relocation.
939       DCHECK(!rs.is(at));
940       li(at, rt);
941       ddiv(rd, rs, at);
942     }
943   }
944 }
945
946
947 void MacroAssembler::Divu(Register rs, const Operand& rt) {
948   if (rt.is_reg()) {
949     divu(rs, rt.rm());
950   } else {
951     // li handles the relocation.
952     DCHECK(!rs.is(at));
953     li(at, rt);
954     divu(rs, at);
955   }
956 }
957
958
959 void MacroAssembler::Divu(Register res, Register rs, const Operand& rt) {
960   if (rt.is_reg()) {
961     if (kArchVariant != kMips64r6) {
962       divu(rs, rt.rm());
963       mflo(res);
964     } else {
965       divu(res, rs, rt.rm());
966     }
967   } else {
968     // li handles the relocation.
969     DCHECK(!rs.is(at));
970     li(at, rt);
971     if (kArchVariant != kMips64r6) {
972       divu(rs, at);
973       mflo(res);
974     } else {
975       divu(res, rs, at);
976     }
977   }
978 }
979
980
981 void MacroAssembler::Ddivu(Register rs, const Operand& rt) {
982   if (rt.is_reg()) {
983     ddivu(rs, rt.rm());
984   } else {
985     // li handles the relocation.
986     DCHECK(!rs.is(at));
987     li(at, rt);
988     ddivu(rs, at);
989   }
990 }
991
992
993 void MacroAssembler::Ddivu(Register res, Register rs, const Operand& rt) {
994   if (rt.is_reg()) {
995     if (kArchVariant != kMips64r6) {
996       ddivu(rs, rt.rm());
997       mflo(res);
998     } else {
999       ddivu(res, rs, rt.rm());
1000     }
1001   } else {
1002     // li handles the relocation.
1003     DCHECK(!rs.is(at));
1004     li(at, rt);
1005     if (kArchVariant != kMips64r6) {
1006       ddivu(rs, at);
1007       mflo(res);
1008     } else {
1009       ddivu(res, rs, at);
1010     }
1011   }
1012 }
1013
1014
1015 void MacroAssembler::Dmod(Register rd, Register rs, const Operand& rt) {
1016   if (kArchVariant != kMips64r6) {
1017     if (rt.is_reg()) {
1018       ddiv(rs, rt.rm());
1019       mfhi(rd);
1020     } else {
1021       // li handles the relocation.
1022       DCHECK(!rs.is(at));
1023       li(at, rt);
1024       ddiv(rs, at);
1025       mfhi(rd);
1026     }
1027   } else {
1028     if (rt.is_reg()) {
1029       dmod(rd, rs, rt.rm());
1030     } else {
1031       // li handles the relocation.
1032       DCHECK(!rs.is(at));
1033       li(at, rt);
1034       dmod(rd, rs, at);
1035     }
1036   }
1037 }
1038
1039
1040 void MacroAssembler::Dmodu(Register rd, Register rs, const Operand& rt) {
1041   if (kArchVariant != kMips64r6) {
1042     if (rt.is_reg()) {
1043       ddivu(rs, rt.rm());
1044       mfhi(rd);
1045     } else {
1046       // li handles the relocation.
1047       DCHECK(!rs.is(at));
1048       li(at, rt);
1049       ddivu(rs, at);
1050       mfhi(rd);
1051     }
1052   } else {
1053     if (rt.is_reg()) {
1054       dmodu(rd, rs, rt.rm());
1055     } else {
1056       // li handles the relocation.
1057       DCHECK(!rs.is(at));
1058       li(at, rt);
1059       dmodu(rd, rs, at);
1060     }
1061   }
1062 }
1063
1064
1065 void MacroAssembler::And(Register rd, Register rs, const Operand& rt) {
1066   if (rt.is_reg()) {
1067     and_(rd, rs, rt.rm());
1068   } else {
1069     if (is_uint16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
1070       andi(rd, rs, rt.imm64_);
1071     } else {
1072       // li handles the relocation.
1073       DCHECK(!rs.is(at));
1074       li(at, rt);
1075       and_(rd, rs, at);
1076     }
1077   }
1078 }
1079
1080
1081 void MacroAssembler::Or(Register rd, Register rs, const Operand& rt) {
1082   if (rt.is_reg()) {
1083     or_(rd, rs, rt.rm());
1084   } else {
1085     if (is_uint16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
1086       ori(rd, rs, rt.imm64_);
1087     } else {
1088       // li handles the relocation.
1089       DCHECK(!rs.is(at));
1090       li(at, rt);
1091       or_(rd, rs, at);
1092     }
1093   }
1094 }
1095
1096
1097 void MacroAssembler::Xor(Register rd, Register rs, const Operand& rt) {
1098   if (rt.is_reg()) {
1099     xor_(rd, rs, rt.rm());
1100   } else {
1101     if (is_uint16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
1102       xori(rd, rs, rt.imm64_);
1103     } else {
1104       // li handles the relocation.
1105       DCHECK(!rs.is(at));
1106       li(at, rt);
1107       xor_(rd, rs, at);
1108     }
1109   }
1110 }
1111
1112
1113 void MacroAssembler::Nor(Register rd, Register rs, const Operand& rt) {
1114   if (rt.is_reg()) {
1115     nor(rd, rs, rt.rm());
1116   } else {
1117     // li handles the relocation.
1118     DCHECK(!rs.is(at));
1119     li(at, rt);
1120     nor(rd, rs, at);
1121   }
1122 }
1123
1124
1125 void MacroAssembler::Neg(Register rs, const Operand& rt) {
1126   DCHECK(rt.is_reg());
1127   DCHECK(!at.is(rs));
1128   DCHECK(!at.is(rt.rm()));
1129   li(at, -1);
1130   xor_(rs, rt.rm(), at);
1131 }
1132
1133
1134 void MacroAssembler::Slt(Register rd, Register rs, const Operand& rt) {
1135   if (rt.is_reg()) {
1136     slt(rd, rs, rt.rm());
1137   } else {
1138     if (is_int16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
1139       slti(rd, rs, rt.imm64_);
1140     } else {
1141       // li handles the relocation.
1142       DCHECK(!rs.is(at));
1143       li(at, rt);
1144       slt(rd, rs, at);
1145     }
1146   }
1147 }
1148
1149
1150 void MacroAssembler::Sltu(Register rd, Register rs, const Operand& rt) {
1151   if (rt.is_reg()) {
1152     sltu(rd, rs, rt.rm());
1153   } else {
1154     if (is_int16(rt.imm64_) && !MustUseReg(rt.rmode_)) {
1155       sltiu(rd, rs, rt.imm64_);
1156     } else {
1157       // li handles the relocation.
1158       DCHECK(!rs.is(at));
1159       li(at, rt);
1160       sltu(rd, rs, at);
1161     }
1162   }
1163 }
1164
1165
1166 void MacroAssembler::Ror(Register rd, Register rs, const Operand& rt) {
1167   if (rt.is_reg()) {
1168     rotrv(rd, rs, rt.rm());
1169   } else {
1170     rotr(rd, rs, rt.imm64_);
1171   }
1172 }
1173
1174
1175 void MacroAssembler::Dror(Register rd, Register rs, const Operand& rt) {
1176   if (rt.is_reg()) {
1177     drotrv(rd, rs, rt.rm());
1178   } else {
1179     drotr(rd, rs, rt.imm64_);
1180   }
1181 }
1182
1183
1184 void MacroAssembler::Pref(int32_t hint, const MemOperand& rs) {
1185     pref(hint, rs);
1186 }
1187
1188
1189 // ------------Pseudo-instructions-------------
1190
1191 void MacroAssembler::Ulw(Register rd, const MemOperand& rs) {
1192   lwr(rd, rs);
1193   lwl(rd, MemOperand(rs.rm(), rs.offset() + 3));
1194 }
1195
1196
1197 void MacroAssembler::Usw(Register rd, const MemOperand& rs) {
1198   swr(rd, rs);
1199   swl(rd, MemOperand(rs.rm(), rs.offset() + 3));
1200 }
1201
1202
1203 // Do 64-bit load from unaligned address. Note this only handles
1204 // the specific case of 32-bit aligned, but not 64-bit aligned.
1205 void MacroAssembler::Uld(Register rd, const MemOperand& rs, Register scratch) {
1206   // Assert fail if the offset from start of object IS actually aligned.
1207   // ONLY use with known misalignment, since there is performance cost.
1208   DCHECK((rs.offset() + kHeapObjectTag) & (kPointerSize - 1));
1209   // TODO(plind): endian dependency.
1210   lwu(rd, rs);
1211   lw(scratch, MemOperand(rs.rm(), rs.offset() + kPointerSize / 2));
1212   dsll32(scratch, scratch, 0);
1213   Daddu(rd, rd, scratch);
1214 }
1215
1216
1217 // Do 64-bit store to unaligned address. Note this only handles
1218 // the specific case of 32-bit aligned, but not 64-bit aligned.
1219 void MacroAssembler::Usd(Register rd, const MemOperand& rs, Register scratch) {
1220   // Assert fail if the offset from start of object IS actually aligned.
1221   // ONLY use with known misalignment, since there is performance cost.
1222   DCHECK((rs.offset() + kHeapObjectTag) & (kPointerSize - 1));
1223   // TODO(plind): endian dependency.
1224   sw(rd, rs);
1225   dsrl32(scratch, rd, 0);
1226   sw(scratch, MemOperand(rs.rm(), rs.offset() + kPointerSize / 2));
1227 }
1228
1229
1230 void MacroAssembler::li(Register dst, Handle<Object> value, LiFlags mode) {
1231   AllowDeferredHandleDereference smi_check;
1232   if (value->IsSmi()) {
1233     li(dst, Operand(value), mode);
1234   } else {
1235     DCHECK(value->IsHeapObject());
1236     if (isolate()->heap()->InNewSpace(*value)) {
1237       Handle<Cell> cell = isolate()->factory()->NewCell(value);
1238       li(dst, Operand(cell));
1239       ld(dst, FieldMemOperand(dst, Cell::kValueOffset));
1240     } else {
1241       li(dst, Operand(value));
1242     }
1243   }
1244 }
1245
1246
1247 void MacroAssembler::li(Register rd, Operand j, LiFlags mode) {
1248   DCHECK(!j.is_reg());
1249   BlockTrampolinePoolScope block_trampoline_pool(this);
1250   if (!MustUseReg(j.rmode_) && mode == OPTIMIZE_SIZE) {
1251     // Normal load of an immediate value which does not need Relocation Info.
1252     if (is_int32(j.imm64_)) {
1253       if (is_int16(j.imm64_)) {
1254         daddiu(rd, zero_reg, (j.imm64_ & kImm16Mask));
1255       } else if (!(j.imm64_ & kHiMask)) {
1256         ori(rd, zero_reg, (j.imm64_ & kImm16Mask));
1257       } else if (!(j.imm64_ & kImm16Mask)) {
1258         lui(rd, (j.imm64_ >> kLuiShift) & kImm16Mask);
1259       } else {
1260         lui(rd, (j.imm64_ >> kLuiShift) & kImm16Mask);
1261         ori(rd, rd, (j.imm64_ & kImm16Mask));
1262       }
1263     } else {
1264       lui(rd, (j.imm64_ >> 48) & kImm16Mask);
1265       ori(rd, rd, (j.imm64_ >> 32) & kImm16Mask);
1266       dsll(rd, rd, 16);
1267       ori(rd, rd, (j.imm64_ >> 16) & kImm16Mask);
1268       dsll(rd, rd, 16);
1269       ori(rd, rd, j.imm64_ & kImm16Mask);
1270     }
1271   } else if (MustUseReg(j.rmode_)) {
1272     RecordRelocInfo(j.rmode_, j.imm64_);
1273     lui(rd, (j.imm64_ >> 32) & kImm16Mask);
1274     ori(rd, rd, (j.imm64_ >> 16) & kImm16Mask);
1275     dsll(rd, rd, 16);
1276     ori(rd, rd, j.imm64_ & kImm16Mask);
1277   } else if (mode == ADDRESS_LOAD)  {
1278     // We always need the same number of instructions as we may need to patch
1279     // this code to load another value which may need all 4 instructions.
1280     lui(rd, (j.imm64_ >> 32) & kImm16Mask);
1281     ori(rd, rd, (j.imm64_ >> 16) & kImm16Mask);
1282     dsll(rd, rd, 16);
1283     ori(rd, rd, j.imm64_ & kImm16Mask);
1284   } else {
1285     lui(rd, (j.imm64_ >> 48) & kImm16Mask);
1286     ori(rd, rd, (j.imm64_ >> 32) & kImm16Mask);
1287     dsll(rd, rd, 16);
1288     ori(rd, rd, (j.imm64_ >> 16) & kImm16Mask);
1289     dsll(rd, rd, 16);
1290     ori(rd, rd, j.imm64_ & kImm16Mask);
1291   }
1292 }
1293
1294
1295 void MacroAssembler::MultiPush(RegList regs) {
1296   int16_t num_to_push = NumberOfBitsSet(regs);
1297   int16_t stack_offset = num_to_push * kPointerSize;
1298
1299   Dsubu(sp, sp, Operand(stack_offset));
1300   for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
1301     if ((regs & (1 << i)) != 0) {
1302       stack_offset -= kPointerSize;
1303       sd(ToRegister(i), MemOperand(sp, stack_offset));
1304     }
1305   }
1306 }
1307
1308
1309 void MacroAssembler::MultiPushReversed(RegList regs) {
1310   int16_t num_to_push = NumberOfBitsSet(regs);
1311   int16_t stack_offset = num_to_push * kPointerSize;
1312
1313   Dsubu(sp, sp, Operand(stack_offset));
1314   for (int16_t i = 0; i < kNumRegisters; i++) {
1315     if ((regs & (1 << i)) != 0) {
1316       stack_offset -= kPointerSize;
1317       sd(ToRegister(i), MemOperand(sp, stack_offset));
1318     }
1319   }
1320 }
1321
1322
1323 void MacroAssembler::MultiPop(RegList regs) {
1324   int16_t stack_offset = 0;
1325
1326   for (int16_t i = 0; i < kNumRegisters; i++) {
1327     if ((regs & (1 << i)) != 0) {
1328       ld(ToRegister(i), MemOperand(sp, stack_offset));
1329       stack_offset += kPointerSize;
1330     }
1331   }
1332   daddiu(sp, sp, stack_offset);
1333 }
1334
1335
1336 void MacroAssembler::MultiPopReversed(RegList regs) {
1337   int16_t stack_offset = 0;
1338
1339   for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
1340     if ((regs & (1 << i)) != 0) {
1341       ld(ToRegister(i), MemOperand(sp, stack_offset));
1342       stack_offset += kPointerSize;
1343     }
1344   }
1345   daddiu(sp, sp, stack_offset);
1346 }
1347
1348
1349 void MacroAssembler::MultiPushFPU(RegList regs) {
1350   int16_t num_to_push = NumberOfBitsSet(regs);
1351   int16_t stack_offset = num_to_push * kDoubleSize;
1352
1353   Dsubu(sp, sp, Operand(stack_offset));
1354   for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
1355     if ((regs & (1 << i)) != 0) {
1356       stack_offset -= kDoubleSize;
1357       sdc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1358     }
1359   }
1360 }
1361
1362
1363 void MacroAssembler::MultiPushReversedFPU(RegList regs) {
1364   int16_t num_to_push = NumberOfBitsSet(regs);
1365   int16_t stack_offset = num_to_push * kDoubleSize;
1366
1367   Dsubu(sp, sp, Operand(stack_offset));
1368   for (int16_t i = 0; i < kNumRegisters; i++) {
1369     if ((regs & (1 << i)) != 0) {
1370       stack_offset -= kDoubleSize;
1371       sdc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1372     }
1373   }
1374 }
1375
1376
1377 void MacroAssembler::MultiPopFPU(RegList regs) {
1378   int16_t stack_offset = 0;
1379
1380   for (int16_t i = 0; i < kNumRegisters; i++) {
1381     if ((regs & (1 << i)) != 0) {
1382       ldc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1383       stack_offset += kDoubleSize;
1384     }
1385   }
1386   daddiu(sp, sp, stack_offset);
1387 }
1388
1389
1390 void MacroAssembler::MultiPopReversedFPU(RegList regs) {
1391   int16_t stack_offset = 0;
1392
1393   for (int16_t i = kNumRegisters - 1; i >= 0; i--) {
1394     if ((regs & (1 << i)) != 0) {
1395       ldc1(FPURegister::from_code(i), MemOperand(sp, stack_offset));
1396       stack_offset += kDoubleSize;
1397     }
1398   }
1399   daddiu(sp, sp, stack_offset);
1400 }
1401
1402
1403 void MacroAssembler::FlushICache(Register address, unsigned instructions) {
1404   RegList saved_regs = kJSCallerSaved | ra.bit();
1405   MultiPush(saved_regs);
1406   AllowExternalCallThatCantCauseGC scope(this);
1407
1408   // Save to a0 in case address == a4.
1409   Move(a0, address);
1410   PrepareCallCFunction(2, a4);
1411
1412   li(a1, instructions * kInstrSize);
1413   CallCFunction(ExternalReference::flush_icache_function(isolate()), 2);
1414   MultiPop(saved_regs);
1415 }
1416
1417
1418 void MacroAssembler::Ext(Register rt,
1419                          Register rs,
1420                          uint16_t pos,
1421                          uint16_t size) {
1422   DCHECK(pos < 32);
1423   DCHECK(pos + size < 33);
1424   ext_(rt, rs, pos, size);
1425 }
1426
1427
1428 void MacroAssembler::Dext(Register rt, Register rs, uint16_t pos,
1429                           uint16_t size) {
1430   DCHECK(pos < 32);
1431   DCHECK(pos + size < 33);
1432   dext_(rt, rs, pos, size);
1433 }
1434
1435
1436 void MacroAssembler::Ins(Register rt,
1437                          Register rs,
1438                          uint16_t pos,
1439                          uint16_t size) {
1440   DCHECK(pos < 32);
1441   DCHECK(pos + size <= 32);
1442   DCHECK(size != 0);
1443   ins_(rt, rs, pos, size);
1444 }
1445
1446
1447 void MacroAssembler::Cvt_d_uw(FPURegister fd,
1448                               FPURegister fs,
1449                               FPURegister scratch) {
1450   // Move the data from fs to t8.
1451   mfc1(t8, fs);
1452   Cvt_d_uw(fd, t8, scratch);
1453 }
1454
1455
1456 void MacroAssembler::Cvt_d_uw(FPURegister fd,
1457                               Register rs,
1458                               FPURegister scratch) {
1459   // Convert rs to a FP value in fd (and fd + 1).
1460   // We do this by converting rs minus the MSB to avoid sign conversion,
1461   // then adding 2^31 to the result (if needed).
1462
1463   DCHECK(!fd.is(scratch));
1464   DCHECK(!rs.is(t9));
1465   DCHECK(!rs.is(at));
1466
1467   // Save rs's MSB to t9.
1468   Ext(t9, rs, 31, 1);
1469   // Remove rs's MSB.
1470   Ext(at, rs, 0, 31);
1471   // Move the result to fd.
1472   mtc1(at, fd);
1473   mthc1(zero_reg, fd);
1474
1475   // Convert fd to a real FP value.
1476   cvt_d_w(fd, fd);
1477
1478   Label conversion_done;
1479
1480   // If rs's MSB was 0, it's done.
1481   // Otherwise we need to add that to the FP register.
1482   Branch(&conversion_done, eq, t9, Operand(zero_reg));
1483
1484   // Load 2^31 into f20 as its float representation.
1485   li(at, 0x41E00000);
1486   mtc1(zero_reg, scratch);
1487   mthc1(at, scratch);
1488   // Add it to fd.
1489   add_d(fd, fd, scratch);
1490
1491   bind(&conversion_done);
1492 }
1493
1494
1495 void MacroAssembler::Round_l_d(FPURegister fd, FPURegister fs) {
1496   round_l_d(fd, fs);
1497 }
1498
1499
1500 void MacroAssembler::Floor_l_d(FPURegister fd, FPURegister fs) {
1501   floor_l_d(fd, fs);
1502 }
1503
1504
1505 void MacroAssembler::Ceil_l_d(FPURegister fd, FPURegister fs) {
1506   ceil_l_d(fd, fs);
1507 }
1508
1509
1510 void MacroAssembler::Trunc_l_d(FPURegister fd, FPURegister fs) {
1511   trunc_l_d(fd, fs);
1512 }
1513
1514
1515 void MacroAssembler::Trunc_l_ud(FPURegister fd,
1516                                 FPURegister fs,
1517                                 FPURegister scratch) {
1518   // Load to GPR.
1519   dmfc1(t8, fs);
1520   // Reset sign bit.
1521   li(at, 0x7fffffffffffffff);
1522   and_(t8, t8, at);
1523   dmtc1(t8, fs);
1524   trunc_l_d(fd, fs);
1525 }
1526
1527
1528 void MacroAssembler::Trunc_uw_d(FPURegister fd,
1529                                 FPURegister fs,
1530                                 FPURegister scratch) {
1531   Trunc_uw_d(fs, t8, scratch);
1532   mtc1(t8, fd);
1533 }
1534
1535
1536 void MacroAssembler::Trunc_w_d(FPURegister fd, FPURegister fs) {
1537   trunc_w_d(fd, fs);
1538 }
1539
1540
1541 void MacroAssembler::Round_w_d(FPURegister fd, FPURegister fs) {
1542   round_w_d(fd, fs);
1543 }
1544
1545
1546 void MacroAssembler::Floor_w_d(FPURegister fd, FPURegister fs) {
1547   floor_w_d(fd, fs);
1548 }
1549
1550
1551 void MacroAssembler::Ceil_w_d(FPURegister fd, FPURegister fs) {
1552   ceil_w_d(fd, fs);
1553 }
1554
1555
1556 void MacroAssembler::Trunc_uw_d(FPURegister fd,
1557                                 Register rs,
1558                                 FPURegister scratch) {
1559   DCHECK(!fd.is(scratch));
1560   DCHECK(!rs.is(at));
1561
1562   // Load 2^31 into scratch as its float representation.
1563   li(at, 0x41E00000);
1564   mtc1(zero_reg, scratch);
1565   mthc1(at, scratch);
1566   // Test if scratch > fd.
1567   // If fd < 2^31 we can convert it normally.
1568   Label simple_convert;
1569   BranchF(&simple_convert, NULL, lt, fd, scratch);
1570
1571   // First we subtract 2^31 from fd, then trunc it to rs
1572   // and add 2^31 to rs.
1573   sub_d(scratch, fd, scratch);
1574   trunc_w_d(scratch, scratch);
1575   mfc1(rs, scratch);
1576   Or(rs, rs, 1 << 31);
1577
1578   Label done;
1579   Branch(&done);
1580   // Simple conversion.
1581   bind(&simple_convert);
1582   trunc_w_d(scratch, fd);
1583   mfc1(rs, scratch);
1584
1585   bind(&done);
1586 }
1587
1588
1589 void MacroAssembler::Madd_d(FPURegister fd, FPURegister fr, FPURegister fs,
1590     FPURegister ft, FPURegister scratch) {
1591   if (0) {  // TODO(plind): find reasonable arch-variant symbol names.
1592     madd_d(fd, fr, fs, ft);
1593   } else {
1594     // Can not change source regs's value.
1595     DCHECK(!fr.is(scratch) && !fs.is(scratch) && !ft.is(scratch));
1596     mul_d(scratch, fs, ft);
1597     add_d(fd, fr, scratch);
1598   }
1599 }
1600
1601
1602 void MacroAssembler::BranchF(Label* target,
1603                              Label* nan,
1604                              Condition cc,
1605                              FPURegister cmp1,
1606                              FPURegister cmp2,
1607                              BranchDelaySlot bd) {
1608   BlockTrampolinePoolScope block_trampoline_pool(this);
1609   if (cc == al) {
1610     Branch(bd, target);
1611     return;
1612   }
1613
1614   DCHECK(nan || target);
1615   // Check for unordered (NaN) cases.
1616   if (nan) {
1617     if (kArchVariant != kMips64r6) {
1618       c(UN, D, cmp1, cmp2);
1619       bc1t(nan);
1620     } else {
1621       // Use f31 for comparison result. It has to be unavailable to lithium
1622       // register allocator.
1623       DCHECK(!cmp1.is(f31) && !cmp2.is(f31));
1624       cmp(UN, L, f31, cmp1, cmp2);
1625       bc1nez(nan, f31);
1626     }
1627   }
1628
1629   if (kArchVariant != kMips64r6) {
1630     if (target) {
1631       // Here NaN cases were either handled by this function or are assumed to
1632       // have been handled by the caller.
1633       switch (cc) {
1634         case lt:
1635           c(OLT, D, cmp1, cmp2);
1636           bc1t(target);
1637           break;
1638         case gt:
1639           c(ULE, D, cmp1, cmp2);
1640           bc1f(target);
1641           break;
1642         case ge:
1643           c(ULT, D, cmp1, cmp2);
1644           bc1f(target);
1645           break;
1646         case le:
1647           c(OLE, D, cmp1, cmp2);
1648           bc1t(target);
1649           break;
1650         case eq:
1651           c(EQ, D, cmp1, cmp2);
1652           bc1t(target);
1653           break;
1654         case ueq:
1655           c(UEQ, D, cmp1, cmp2);
1656           bc1t(target);
1657           break;
1658         case ne:
1659           c(EQ, D, cmp1, cmp2);
1660           bc1f(target);
1661           break;
1662         case nue:
1663           c(UEQ, D, cmp1, cmp2);
1664           bc1f(target);
1665           break;
1666         default:
1667           CHECK(0);
1668       }
1669     }
1670   } else {
1671     if (target) {
1672       // Here NaN cases were either handled by this function or are assumed to
1673       // have been handled by the caller.
1674       // Unsigned conditions are treated as their signed counterpart.
1675       // Use f31 for comparison result, it is valid in fp64 (FR = 1) mode.
1676       DCHECK(!cmp1.is(f31) && !cmp2.is(f31));
1677       switch (cc) {
1678         case lt:
1679           cmp(OLT, L, f31, cmp1, cmp2);
1680           bc1nez(target, f31);
1681           break;
1682         case gt:
1683           cmp(ULE, L, f31, cmp1, cmp2);
1684           bc1eqz(target, f31);
1685           break;
1686         case ge:
1687           cmp(ULT, L, f31, cmp1, cmp2);
1688           bc1eqz(target, f31);
1689           break;
1690         case le:
1691           cmp(OLE, L, f31, cmp1, cmp2);
1692           bc1nez(target, f31);
1693           break;
1694         case eq:
1695           cmp(EQ, L, f31, cmp1, cmp2);
1696           bc1nez(target, f31);
1697           break;
1698         case ueq:
1699           cmp(UEQ, L, f31, cmp1, cmp2);
1700           bc1nez(target, f31);
1701           break;
1702         case ne:
1703           cmp(EQ, L, f31, cmp1, cmp2);
1704           bc1eqz(target, f31);
1705           break;
1706         case nue:
1707           cmp(UEQ, L, f31, cmp1, cmp2);
1708           bc1eqz(target, f31);
1709           break;
1710         default:
1711           CHECK(0);
1712       }
1713     }
1714   }
1715
1716   if (bd == PROTECT) {
1717     nop();
1718   }
1719 }
1720
1721
1722 void MacroAssembler::FmoveLow(FPURegister dst, Register src_low) {
1723   DCHECK(!src_low.is(at));
1724   mfhc1(at, dst);
1725   mtc1(src_low, dst);
1726   mthc1(at, dst);
1727 }
1728
1729
1730 void MacroAssembler::Move(FPURegister dst, float imm) {
1731   li(at, Operand(bit_cast<int32_t>(imm)));
1732   mtc1(at, dst);
1733 }
1734
1735
1736 void MacroAssembler::Move(FPURegister dst, double imm) {
1737   static const DoubleRepresentation minus_zero(-0.0);
1738   static const DoubleRepresentation zero(0.0);
1739   DoubleRepresentation value_rep(imm);
1740   // Handle special values first.
1741   if (value_rep == zero && has_double_zero_reg_set_) {
1742     mov_d(dst, kDoubleRegZero);
1743   } else if (value_rep == minus_zero && has_double_zero_reg_set_) {
1744     neg_d(dst, kDoubleRegZero);
1745   } else {
1746     uint32_t lo, hi;
1747     DoubleAsTwoUInt32(imm, &lo, &hi);
1748     // Move the low part of the double into the lower bits of the corresponding
1749     // FPU register.
1750     if (lo != 0) {
1751       li(at, Operand(lo));
1752       mtc1(at, dst);
1753     } else {
1754       mtc1(zero_reg, dst);
1755     }
1756     // Move the high part of the double into the high bits of the corresponding
1757     // FPU register.
1758     if (hi != 0) {
1759       li(at, Operand(hi));
1760       mthc1(at, dst);
1761     } else {
1762       mthc1(zero_reg, dst);
1763     }
1764     if (dst.is(kDoubleRegZero)) has_double_zero_reg_set_ = true;
1765   }
1766 }
1767
1768
1769 void MacroAssembler::Movz(Register rd, Register rs, Register rt) {
1770   if (kArchVariant == kMips64r6) {
1771     Label done;
1772     Branch(&done, ne, rt, Operand(zero_reg));
1773     mov(rd, rs);
1774     bind(&done);
1775   } else {
1776     movz(rd, rs, rt);
1777   }
1778 }
1779
1780
1781 void MacroAssembler::Movn(Register rd, Register rs, Register rt) {
1782   if (kArchVariant == kMips64r6) {
1783     Label done;
1784     Branch(&done, eq, rt, Operand(zero_reg));
1785     mov(rd, rs);
1786     bind(&done);
1787   } else {
1788     movn(rd, rs, rt);
1789   }
1790 }
1791
1792
1793 void MacroAssembler::Movt(Register rd, Register rs, uint16_t cc) {
1794   movt(rd, rs, cc);
1795 }
1796
1797
1798 void MacroAssembler::Movf(Register rd, Register rs, uint16_t cc) {
1799   movf(rd, rs, cc);
1800 }
1801
1802
1803 void MacroAssembler::Clz(Register rd, Register rs) {
1804   clz(rd, rs);
1805 }
1806
1807
1808 void MacroAssembler::EmitFPUTruncate(FPURoundingMode rounding_mode,
1809                                      Register result,
1810                                      DoubleRegister double_input,
1811                                      Register scratch,
1812                                      DoubleRegister double_scratch,
1813                                      Register except_flag,
1814                                      CheckForInexactConversion check_inexact) {
1815   DCHECK(!result.is(scratch));
1816   DCHECK(!double_input.is(double_scratch));
1817   DCHECK(!except_flag.is(scratch));
1818
1819   Label done;
1820
1821   // Clear the except flag (0 = no exception)
1822   mov(except_flag, zero_reg);
1823
1824   // Test for values that can be exactly represented as a signed 32-bit integer.
1825   cvt_w_d(double_scratch, double_input);
1826   mfc1(result, double_scratch);
1827   cvt_d_w(double_scratch, double_scratch);
1828   BranchF(&done, NULL, eq, double_input, double_scratch);
1829
1830   int32_t except_mask = kFCSRFlagMask;  // Assume interested in all exceptions.
1831
1832   if (check_inexact == kDontCheckForInexactConversion) {
1833     // Ignore inexact exceptions.
1834     except_mask &= ~kFCSRInexactFlagMask;
1835   }
1836
1837   // Save FCSR.
1838   cfc1(scratch, FCSR);
1839   // Disable FPU exceptions.
1840   ctc1(zero_reg, FCSR);
1841
1842   // Do operation based on rounding mode.
1843   switch (rounding_mode) {
1844     case kRoundToNearest:
1845       Round_w_d(double_scratch, double_input);
1846       break;
1847     case kRoundToZero:
1848       Trunc_w_d(double_scratch, double_input);
1849       break;
1850     case kRoundToPlusInf:
1851       Ceil_w_d(double_scratch, double_input);
1852       break;
1853     case kRoundToMinusInf:
1854       Floor_w_d(double_scratch, double_input);
1855       break;
1856   }  // End of switch-statement.
1857
1858   // Retrieve FCSR.
1859   cfc1(except_flag, FCSR);
1860   // Restore FCSR.
1861   ctc1(scratch, FCSR);
1862   // Move the converted value into the result register.
1863   mfc1(result, double_scratch);
1864
1865   // Check for fpu exceptions.
1866   And(except_flag, except_flag, Operand(except_mask));
1867
1868   bind(&done);
1869 }
1870
1871
1872 void MacroAssembler::TryInlineTruncateDoubleToI(Register result,
1873                                                 DoubleRegister double_input,
1874                                                 Label* done) {
1875   DoubleRegister single_scratch = kLithiumScratchDouble.low();
1876   Register scratch = at;
1877   Register scratch2 = t9;
1878
1879   // Clear cumulative exception flags and save the FCSR.
1880   cfc1(scratch2, FCSR);
1881   ctc1(zero_reg, FCSR);
1882   // Try a conversion to a signed integer.
1883   trunc_w_d(single_scratch, double_input);
1884   mfc1(result, single_scratch);
1885   // Retrieve and restore the FCSR.
1886   cfc1(scratch, FCSR);
1887   ctc1(scratch2, FCSR);
1888   // Check for overflow and NaNs.
1889   And(scratch,
1890       scratch,
1891       kFCSROverflowFlagMask | kFCSRUnderflowFlagMask | kFCSRInvalidOpFlagMask);
1892   // If we had no exceptions we are done.
1893   Branch(done, eq, scratch, Operand(zero_reg));
1894 }
1895
1896
1897 void MacroAssembler::TruncateDoubleToI(Register result,
1898                                        DoubleRegister double_input) {
1899   Label done;
1900
1901   TryInlineTruncateDoubleToI(result, double_input, &done);
1902
1903   // If we fell through then inline version didn't succeed - call stub instead.
1904   push(ra);
1905   Dsubu(sp, sp, Operand(kDoubleSize));  // Put input on stack.
1906   sdc1(double_input, MemOperand(sp, 0));
1907
1908   DoubleToIStub stub(isolate(), sp, result, 0, true, true);
1909   CallStub(&stub);
1910
1911   Daddu(sp, sp, Operand(kDoubleSize));
1912   pop(ra);
1913
1914   bind(&done);
1915 }
1916
1917
1918 void MacroAssembler::TruncateHeapNumberToI(Register result, Register object) {
1919   Label done;
1920   DoubleRegister double_scratch = f12;
1921   DCHECK(!result.is(object));
1922
1923   ldc1(double_scratch,
1924        MemOperand(object, HeapNumber::kValueOffset - kHeapObjectTag));
1925   TryInlineTruncateDoubleToI(result, double_scratch, &done);
1926
1927   // If we fell through then inline version didn't succeed - call stub instead.
1928   push(ra);
1929   DoubleToIStub stub(isolate(),
1930                      object,
1931                      result,
1932                      HeapNumber::kValueOffset - kHeapObjectTag,
1933                      true,
1934                      true);
1935   CallStub(&stub);
1936   pop(ra);
1937
1938   bind(&done);
1939 }
1940
1941
1942 void MacroAssembler::TruncateNumberToI(Register object,
1943                                        Register result,
1944                                        Register heap_number_map,
1945                                        Register scratch,
1946                                        Label* not_number) {
1947   Label done;
1948   DCHECK(!result.is(object));
1949
1950   UntagAndJumpIfSmi(result, object, &done);
1951   JumpIfNotHeapNumber(object, heap_number_map, scratch, not_number);
1952   TruncateHeapNumberToI(result, object);
1953
1954   bind(&done);
1955 }
1956
1957
1958 void MacroAssembler::GetLeastBitsFromSmi(Register dst,
1959                                          Register src,
1960                                          int num_least_bits) {
1961   // Ext(dst, src, kSmiTagSize, num_least_bits);
1962   SmiUntag(dst, src);
1963   And(dst, dst, Operand((1 << num_least_bits) - 1));
1964 }
1965
1966
1967 void MacroAssembler::GetLeastBitsFromInt32(Register dst,
1968                                            Register src,
1969                                            int num_least_bits) {
1970   DCHECK(!src.is(dst));
1971   And(dst, src, Operand((1 << num_least_bits) - 1));
1972 }
1973
1974
1975 // Emulated condtional branches do not emit a nop in the branch delay slot.
1976 //
1977 // BRANCH_ARGS_CHECK checks that conditional jump arguments are correct.
1978 #define BRANCH_ARGS_CHECK(cond, rs, rt) DCHECK(                                \
1979     (cond == cc_always && rs.is(zero_reg) && rt.rm().is(zero_reg)) ||          \
1980     (cond != cc_always && (!rs.is(zero_reg) || !rt.rm().is(zero_reg))))
1981
1982
1983 void MacroAssembler::Branch(int16_t offset, BranchDelaySlot bdslot) {
1984   BranchShort(offset, bdslot);
1985 }
1986
1987
1988 void MacroAssembler::Branch(int16_t offset, Condition cond, Register rs,
1989                             const Operand& rt,
1990                             BranchDelaySlot bdslot) {
1991   BranchShort(offset, cond, rs, rt, bdslot);
1992 }
1993
1994
1995 void MacroAssembler::Branch(Label* L, BranchDelaySlot bdslot) {
1996   if (L->is_bound()) {
1997     if (is_near(L)) {
1998       BranchShort(L, bdslot);
1999     } else {
2000       Jr(L, bdslot);
2001     }
2002   } else {
2003     if (is_trampoline_emitted()) {
2004       Jr(L, bdslot);
2005     } else {
2006       BranchShort(L, bdslot);
2007     }
2008   }
2009 }
2010
2011
2012 void MacroAssembler::Branch(Label* L, Condition cond, Register rs,
2013                             const Operand& rt,
2014                             BranchDelaySlot bdslot) {
2015   if (L->is_bound()) {
2016     if (is_near(L)) {
2017       BranchShort(L, cond, rs, rt, bdslot);
2018     } else {
2019       if (cond != cc_always) {
2020         Label skip;
2021         Condition neg_cond = NegateCondition(cond);
2022         BranchShort(&skip, neg_cond, rs, rt);
2023         Jr(L, bdslot);
2024         bind(&skip);
2025       } else {
2026         Jr(L, bdslot);
2027       }
2028     }
2029   } else {
2030     if (is_trampoline_emitted()) {
2031       if (cond != cc_always) {
2032         Label skip;
2033         Condition neg_cond = NegateCondition(cond);
2034         BranchShort(&skip, neg_cond, rs, rt);
2035         Jr(L, bdslot);
2036         bind(&skip);
2037       } else {
2038         Jr(L, bdslot);
2039       }
2040     } else {
2041       BranchShort(L, cond, rs, rt, bdslot);
2042     }
2043   }
2044 }
2045
2046
2047 void MacroAssembler::Branch(Label* L,
2048                             Condition cond,
2049                             Register rs,
2050                             Heap::RootListIndex index,
2051                             BranchDelaySlot bdslot) {
2052   LoadRoot(at, index);
2053   Branch(L, cond, rs, Operand(at), bdslot);
2054 }
2055
2056
2057 void MacroAssembler::BranchShort(int16_t offset, BranchDelaySlot bdslot) {
2058   b(offset);
2059
2060   // Emit a nop in the branch delay slot if required.
2061   if (bdslot == PROTECT)
2062     nop();
2063 }
2064
2065
2066 void MacroAssembler::BranchShort(int16_t offset, Condition cond, Register rs,
2067                                  const Operand& rt,
2068                                  BranchDelaySlot bdslot) {
2069   BRANCH_ARGS_CHECK(cond, rs, rt);
2070   DCHECK(!rs.is(zero_reg));
2071   Register r2 = no_reg;
2072   Register scratch = at;
2073
2074   if (rt.is_reg()) {
2075     // NOTE: 'at' can be clobbered by Branch but it is legal to use it as rs or
2076     // rt.
2077     BlockTrampolinePoolScope block_trampoline_pool(this);
2078     r2 = rt.rm_;
2079     switch (cond) {
2080       case cc_always:
2081         b(offset);
2082         break;
2083       case eq:
2084         beq(rs, r2, offset);
2085         break;
2086       case ne:
2087         bne(rs, r2, offset);
2088         break;
2089       // Signed comparison.
2090       case greater:
2091         if (r2.is(zero_reg)) {
2092           bgtz(rs, offset);
2093         } else {
2094           slt(scratch, r2, rs);
2095           bne(scratch, zero_reg, offset);
2096         }
2097         break;
2098       case greater_equal:
2099         if (r2.is(zero_reg)) {
2100           bgez(rs, offset);
2101         } else {
2102           slt(scratch, rs, r2);
2103           beq(scratch, zero_reg, offset);
2104         }
2105         break;
2106       case less:
2107         if (r2.is(zero_reg)) {
2108           bltz(rs, offset);
2109         } else {
2110           slt(scratch, rs, r2);
2111           bne(scratch, zero_reg, offset);
2112         }
2113         break;
2114       case less_equal:
2115         if (r2.is(zero_reg)) {
2116           blez(rs, offset);
2117         } else {
2118           slt(scratch, r2, rs);
2119           beq(scratch, zero_reg, offset);
2120         }
2121         break;
2122       // Unsigned comparison.
2123       case Ugreater:
2124         if (r2.is(zero_reg)) {
2125           bne(rs, zero_reg, offset);
2126         } else {
2127           sltu(scratch, r2, rs);
2128           bne(scratch, zero_reg, offset);
2129         }
2130         break;
2131       case Ugreater_equal:
2132         if (r2.is(zero_reg)) {
2133           b(offset);
2134         } else {
2135           sltu(scratch, rs, r2);
2136           beq(scratch, zero_reg, offset);
2137         }
2138         break;
2139       case Uless:
2140         if (r2.is(zero_reg)) {
2141           // No code needs to be emitted.
2142           return;
2143         } else {
2144           sltu(scratch, rs, r2);
2145           bne(scratch, zero_reg, offset);
2146         }
2147         break;
2148       case Uless_equal:
2149         if (r2.is(zero_reg)) {
2150           beq(rs, zero_reg, offset);
2151         } else {
2152           sltu(scratch, r2, rs);
2153           beq(scratch, zero_reg, offset);
2154         }
2155         break;
2156       default:
2157         UNREACHABLE();
2158     }
2159   } else {
2160     // Be careful to always use shifted_branch_offset only just before the
2161     // branch instruction, as the location will be remember for patching the
2162     // target.
2163     BlockTrampolinePoolScope block_trampoline_pool(this);
2164     switch (cond) {
2165       case cc_always:
2166         b(offset);
2167         break;
2168       case eq:
2169         if (rt.imm64_ == 0) {
2170           beq(rs, zero_reg, offset);
2171         } else {
2172           // We don't want any other register but scratch clobbered.
2173           DCHECK(!scratch.is(rs));
2174           r2 = scratch;
2175           li(r2, rt);
2176           beq(rs, r2, offset);
2177         }
2178         break;
2179       case ne:
2180         if (rt.imm64_ == 0) {
2181           bne(rs, zero_reg, offset);
2182         } else {
2183           // We don't want any other register but scratch clobbered.
2184           DCHECK(!scratch.is(rs));
2185           r2 = scratch;
2186           li(r2, rt);
2187           bne(rs, r2, offset);
2188         }
2189         break;
2190       // Signed comparison.
2191       case greater:
2192         if (rt.imm64_ == 0) {
2193           bgtz(rs, offset);
2194         } else {
2195           r2 = scratch;
2196           li(r2, rt);
2197           slt(scratch, r2, rs);
2198           bne(scratch, zero_reg, offset);
2199         }
2200         break;
2201       case greater_equal:
2202         if (rt.imm64_ == 0) {
2203           bgez(rs, offset);
2204         } else if (is_int16(rt.imm64_)) {
2205           slti(scratch, rs, rt.imm64_);
2206           beq(scratch, zero_reg, offset);
2207         } else {
2208           r2 = scratch;
2209           li(r2, rt);
2210           slt(scratch, rs, r2);
2211           beq(scratch, zero_reg, offset);
2212         }
2213         break;
2214       case less:
2215         if (rt.imm64_ == 0) {
2216           bltz(rs, offset);
2217         } else if (is_int16(rt.imm64_)) {
2218           slti(scratch, rs, rt.imm64_);
2219           bne(scratch, zero_reg, offset);
2220         } else {
2221           r2 = scratch;
2222           li(r2, rt);
2223           slt(scratch, rs, r2);
2224           bne(scratch, zero_reg, offset);
2225         }
2226         break;
2227       case less_equal:
2228         if (rt.imm64_ == 0) {
2229           blez(rs, offset);
2230         } else {
2231           r2 = scratch;
2232           li(r2, rt);
2233           slt(scratch, r2, rs);
2234           beq(scratch, zero_reg, offset);
2235        }
2236        break;
2237       // Unsigned comparison.
2238       case Ugreater:
2239         if (rt.imm64_ == 0) {
2240           bne(rs, zero_reg, offset);
2241         } else {
2242           r2 = scratch;
2243           li(r2, rt);
2244           sltu(scratch, r2, rs);
2245           bne(scratch, zero_reg, offset);
2246         }
2247         break;
2248       case Ugreater_equal:
2249         if (rt.imm64_ == 0) {
2250           b(offset);
2251         } else if (is_int16(rt.imm64_)) {
2252           sltiu(scratch, rs, rt.imm64_);
2253           beq(scratch, zero_reg, offset);
2254         } else {
2255           r2 = scratch;
2256           li(r2, rt);
2257           sltu(scratch, rs, r2);
2258           beq(scratch, zero_reg, offset);
2259         }
2260         break;
2261       case Uless:
2262         if (rt.imm64_ == 0) {
2263           // No code needs to be emitted.
2264           return;
2265         } else if (is_int16(rt.imm64_)) {
2266           sltiu(scratch, rs, rt.imm64_);
2267           bne(scratch, zero_reg, offset);
2268         } else {
2269           r2 = scratch;
2270           li(r2, rt);
2271           sltu(scratch, rs, r2);
2272           bne(scratch, zero_reg, offset);
2273         }
2274         break;
2275       case Uless_equal:
2276         if (rt.imm64_ == 0) {
2277           beq(rs, zero_reg, offset);
2278         } else {
2279           r2 = scratch;
2280           li(r2, rt);
2281           sltu(scratch, r2, rs);
2282           beq(scratch, zero_reg, offset);
2283         }
2284         break;
2285       default:
2286         UNREACHABLE();
2287     }
2288   }
2289   // Emit a nop in the branch delay slot if required.
2290   if (bdslot == PROTECT)
2291     nop();
2292 }
2293
2294
2295 void MacroAssembler::BranchShort(Label* L, BranchDelaySlot bdslot) {
2296   // We use branch_offset as an argument for the branch instructions to be sure
2297   // it is called just before generating the branch instruction, as needed.
2298
2299   b(shifted_branch_offset(L, false));
2300
2301   // Emit a nop in the branch delay slot if required.
2302   if (bdslot == PROTECT)
2303     nop();
2304 }
2305
2306
2307 void MacroAssembler::BranchShort(Label* L, Condition cond, Register rs,
2308                                  const Operand& rt,
2309                                  BranchDelaySlot bdslot) {
2310   BRANCH_ARGS_CHECK(cond, rs, rt);
2311
2312   int32_t offset = 0;
2313   Register r2 = no_reg;
2314   Register scratch = at;
2315   if (rt.is_reg()) {
2316     BlockTrampolinePoolScope block_trampoline_pool(this);
2317     r2 = rt.rm_;
2318     // Be careful to always use shifted_branch_offset only just before the
2319     // branch instruction, as the location will be remember for patching the
2320     // target.
2321     switch (cond) {
2322       case cc_always:
2323         offset = shifted_branch_offset(L, false);
2324         b(offset);
2325         break;
2326       case eq:
2327         offset = shifted_branch_offset(L, false);
2328         beq(rs, r2, offset);
2329         break;
2330       case ne:
2331         offset = shifted_branch_offset(L, false);
2332         bne(rs, r2, offset);
2333         break;
2334       // Signed comparison.
2335       case greater:
2336         if (r2.is(zero_reg)) {
2337           offset = shifted_branch_offset(L, false);
2338           bgtz(rs, offset);
2339         } else {
2340           slt(scratch, r2, rs);
2341           offset = shifted_branch_offset(L, false);
2342           bne(scratch, zero_reg, offset);
2343         }
2344         break;
2345       case greater_equal:
2346         if (r2.is(zero_reg)) {
2347           offset = shifted_branch_offset(L, false);
2348           bgez(rs, offset);
2349         } else {
2350           slt(scratch, rs, r2);
2351           offset = shifted_branch_offset(L, false);
2352           beq(scratch, zero_reg, offset);
2353         }
2354         break;
2355       case less:
2356         if (r2.is(zero_reg)) {
2357           offset = shifted_branch_offset(L, false);
2358           bltz(rs, offset);
2359         } else {
2360           slt(scratch, rs, r2);
2361           offset = shifted_branch_offset(L, false);
2362           bne(scratch, zero_reg, offset);
2363         }
2364         break;
2365       case less_equal:
2366         if (r2.is(zero_reg)) {
2367           offset = shifted_branch_offset(L, false);
2368           blez(rs, offset);
2369         } else {
2370           slt(scratch, r2, rs);
2371           offset = shifted_branch_offset(L, false);
2372           beq(scratch, zero_reg, offset);
2373         }
2374         break;
2375       // Unsigned comparison.
2376       case Ugreater:
2377         if (r2.is(zero_reg)) {
2378           offset = shifted_branch_offset(L, false);
2379           bne(rs, zero_reg, offset);
2380         } else {
2381           sltu(scratch, r2, rs);
2382           offset = shifted_branch_offset(L, false);
2383           bne(scratch, zero_reg, offset);
2384         }
2385         break;
2386       case Ugreater_equal:
2387         if (r2.is(zero_reg)) {
2388           offset = shifted_branch_offset(L, false);
2389           b(offset);
2390         } else {
2391           sltu(scratch, rs, r2);
2392           offset = shifted_branch_offset(L, false);
2393           beq(scratch, zero_reg, offset);
2394         }
2395         break;
2396       case Uless:
2397         if (r2.is(zero_reg)) {
2398           // No code needs to be emitted.
2399           return;
2400         } else {
2401           sltu(scratch, rs, r2);
2402           offset = shifted_branch_offset(L, false);
2403           bne(scratch, zero_reg, offset);
2404         }
2405         break;
2406       case Uless_equal:
2407         if (r2.is(zero_reg)) {
2408           offset = shifted_branch_offset(L, false);
2409           beq(rs, zero_reg, offset);
2410         } else {
2411           sltu(scratch, r2, rs);
2412           offset = shifted_branch_offset(L, false);
2413           beq(scratch, zero_reg, offset);
2414         }
2415         break;
2416       default:
2417         UNREACHABLE();
2418     }
2419   } else {
2420     // Be careful to always use shifted_branch_offset only just before the
2421     // branch instruction, as the location will be remember for patching the
2422     // target.
2423     BlockTrampolinePoolScope block_trampoline_pool(this);
2424     switch (cond) {
2425       case cc_always:
2426         offset = shifted_branch_offset(L, false);
2427         b(offset);
2428         break;
2429       case eq:
2430         if (rt.imm64_ == 0) {
2431           offset = shifted_branch_offset(L, false);
2432           beq(rs, zero_reg, offset);
2433         } else {
2434           DCHECK(!scratch.is(rs));
2435           r2 = scratch;
2436           li(r2, rt);
2437           offset = shifted_branch_offset(L, false);
2438           beq(rs, r2, offset);
2439         }
2440         break;
2441       case ne:
2442         if (rt.imm64_ == 0) {
2443           offset = shifted_branch_offset(L, false);
2444           bne(rs, zero_reg, offset);
2445         } else {
2446           DCHECK(!scratch.is(rs));
2447           r2 = scratch;
2448           li(r2, rt);
2449           offset = shifted_branch_offset(L, false);
2450           bne(rs, r2, offset);
2451         }
2452         break;
2453       // Signed comparison.
2454       case greater:
2455         if (rt.imm64_ == 0) {
2456           offset = shifted_branch_offset(L, false);
2457           bgtz(rs, offset);
2458         } else {
2459           DCHECK(!scratch.is(rs));
2460           r2 = scratch;
2461           li(r2, rt);
2462           slt(scratch, r2, rs);
2463           offset = shifted_branch_offset(L, false);
2464           bne(scratch, zero_reg, offset);
2465         }
2466         break;
2467       case greater_equal:
2468         if (rt.imm64_ == 0) {
2469           offset = shifted_branch_offset(L, false);
2470           bgez(rs, offset);
2471         } else if (is_int16(rt.imm64_)) {
2472           slti(scratch, rs, rt.imm64_);
2473           offset = shifted_branch_offset(L, false);
2474           beq(scratch, zero_reg, offset);
2475         } else {
2476           DCHECK(!scratch.is(rs));
2477           r2 = scratch;
2478           li(r2, rt);
2479           slt(scratch, rs, r2);
2480           offset = shifted_branch_offset(L, false);
2481           beq(scratch, zero_reg, offset);
2482         }
2483         break;
2484       case less:
2485         if (rt.imm64_ == 0) {
2486           offset = shifted_branch_offset(L, false);
2487           bltz(rs, offset);
2488         } else if (is_int16(rt.imm64_)) {
2489           slti(scratch, rs, rt.imm64_);
2490           offset = shifted_branch_offset(L, false);
2491           bne(scratch, zero_reg, offset);
2492         } else {
2493           DCHECK(!scratch.is(rs));
2494           r2 = scratch;
2495           li(r2, rt);
2496           slt(scratch, rs, r2);
2497           offset = shifted_branch_offset(L, false);
2498           bne(scratch, zero_reg, offset);
2499         }
2500         break;
2501       case less_equal:
2502         if (rt.imm64_ == 0) {
2503           offset = shifted_branch_offset(L, false);
2504           blez(rs, offset);
2505         } else {
2506           DCHECK(!scratch.is(rs));
2507           r2 = scratch;
2508           li(r2, rt);
2509           slt(scratch, r2, rs);
2510           offset = shifted_branch_offset(L, false);
2511           beq(scratch, zero_reg, offset);
2512         }
2513         break;
2514       // Unsigned comparison.
2515       case Ugreater:
2516         if (rt.imm64_ == 0) {
2517           offset = shifted_branch_offset(L, false);
2518           bne(rs, zero_reg, offset);
2519         } else {
2520           DCHECK(!scratch.is(rs));
2521           r2 = scratch;
2522           li(r2, rt);
2523           sltu(scratch, r2, rs);
2524           offset = shifted_branch_offset(L, false);
2525           bne(scratch, zero_reg, offset);
2526         }
2527         break;
2528       case Ugreater_equal:
2529         if (rt.imm64_ == 0) {
2530           offset = shifted_branch_offset(L, false);
2531           b(offset);
2532         } else if (is_int16(rt.imm64_)) {
2533           sltiu(scratch, rs, rt.imm64_);
2534           offset = shifted_branch_offset(L, false);
2535           beq(scratch, zero_reg, offset);
2536         } else {
2537           DCHECK(!scratch.is(rs));
2538           r2 = scratch;
2539           li(r2, rt);
2540           sltu(scratch, rs, r2);
2541           offset = shifted_branch_offset(L, false);
2542           beq(scratch, zero_reg, offset);
2543         }
2544         break;
2545      case Uless:
2546         if (rt.imm64_ == 0) {
2547           // No code needs to be emitted.
2548           return;
2549         } else if (is_int16(rt.imm64_)) {
2550           sltiu(scratch, rs, rt.imm64_);
2551           offset = shifted_branch_offset(L, false);
2552           bne(scratch, zero_reg, offset);
2553         } else {
2554           DCHECK(!scratch.is(rs));
2555           r2 = scratch;
2556           li(r2, rt);
2557           sltu(scratch, rs, r2);
2558           offset = shifted_branch_offset(L, false);
2559           bne(scratch, zero_reg, offset);
2560         }
2561         break;
2562       case Uless_equal:
2563         if (rt.imm64_ == 0) {
2564           offset = shifted_branch_offset(L, false);
2565           beq(rs, zero_reg, offset);
2566         } else {
2567           DCHECK(!scratch.is(rs));
2568           r2 = scratch;
2569           li(r2, rt);
2570           sltu(scratch, r2, rs);
2571           offset = shifted_branch_offset(L, false);
2572           beq(scratch, zero_reg, offset);
2573         }
2574         break;
2575       default:
2576         UNREACHABLE();
2577     }
2578   }
2579   // Check that offset could actually hold on an int16_t.
2580   DCHECK(is_int16(offset));
2581   // Emit a nop in the branch delay slot if required.
2582   if (bdslot == PROTECT)
2583     nop();
2584 }
2585
2586
2587 void MacroAssembler::BranchAndLink(int16_t offset, BranchDelaySlot bdslot) {
2588   BranchAndLinkShort(offset, bdslot);
2589 }
2590
2591
2592 void MacroAssembler::BranchAndLink(int16_t offset, Condition cond, Register rs,
2593                                    const Operand& rt,
2594                                    BranchDelaySlot bdslot) {
2595   BranchAndLinkShort(offset, cond, rs, rt, bdslot);
2596 }
2597
2598
2599 void MacroAssembler::BranchAndLink(Label* L, BranchDelaySlot bdslot) {
2600   if (L->is_bound()) {
2601     if (is_near(L)) {
2602       BranchAndLinkShort(L, bdslot);
2603     } else {
2604       Jalr(L, bdslot);
2605     }
2606   } else {
2607     if (is_trampoline_emitted()) {
2608       Jalr(L, bdslot);
2609     } else {
2610       BranchAndLinkShort(L, bdslot);
2611     }
2612   }
2613 }
2614
2615
2616 void MacroAssembler::BranchAndLink(Label* L, Condition cond, Register rs,
2617                                    const Operand& rt,
2618                                    BranchDelaySlot bdslot) {
2619   if (L->is_bound()) {
2620     if (is_near(L)) {
2621       BranchAndLinkShort(L, cond, rs, rt, bdslot);
2622     } else {
2623       Label skip;
2624       Condition neg_cond = NegateCondition(cond);
2625       BranchShort(&skip, neg_cond, rs, rt);
2626       Jalr(L, bdslot);
2627       bind(&skip);
2628     }
2629   } else {
2630     if (is_trampoline_emitted()) {
2631       Label skip;
2632       Condition neg_cond = NegateCondition(cond);
2633       BranchShort(&skip, neg_cond, rs, rt);
2634       Jalr(L, bdslot);
2635       bind(&skip);
2636     } else {
2637       BranchAndLinkShort(L, cond, rs, rt, bdslot);
2638     }
2639   }
2640 }
2641
2642
2643 // We need to use a bgezal or bltzal, but they can't be used directly with the
2644 // slt instructions. We could use sub or add instead but we would miss overflow
2645 // cases, so we keep slt and add an intermediate third instruction.
2646 void MacroAssembler::BranchAndLinkShort(int16_t offset,
2647                                         BranchDelaySlot bdslot) {
2648   bal(offset);
2649
2650   // Emit a nop in the branch delay slot if required.
2651   if (bdslot == PROTECT)
2652     nop();
2653 }
2654
2655
2656 void MacroAssembler::BranchAndLinkShort(int16_t offset, Condition cond,
2657                                         Register rs, const Operand& rt,
2658                                         BranchDelaySlot bdslot) {
2659   BRANCH_ARGS_CHECK(cond, rs, rt);
2660   Register r2 = no_reg;
2661   Register scratch = at;
2662
2663   if (rt.is_reg()) {
2664     r2 = rt.rm_;
2665   } else if (cond != cc_always) {
2666     r2 = scratch;
2667     li(r2, rt);
2668   }
2669
2670   {
2671     BlockTrampolinePoolScope block_trampoline_pool(this);
2672     switch (cond) {
2673       case cc_always:
2674         bal(offset);
2675         break;
2676       case eq:
2677         bne(rs, r2, 2);
2678         nop();
2679         bal(offset);
2680         break;
2681       case ne:
2682         beq(rs, r2, 2);
2683         nop();
2684         bal(offset);
2685         break;
2686
2687       // Signed comparison.
2688       case greater:
2689         // rs > rt
2690         slt(scratch, r2, rs);
2691         beq(scratch, zero_reg, 2);
2692         nop();
2693         bal(offset);
2694         break;
2695       case greater_equal:
2696         // rs >= rt
2697         slt(scratch, rs, r2);
2698         bne(scratch, zero_reg, 2);
2699         nop();
2700         bal(offset);
2701         break;
2702       case less:
2703         // rs < r2
2704         slt(scratch, rs, r2);
2705         bne(scratch, zero_reg, 2);
2706         nop();
2707         bal(offset);
2708         break;
2709       case less_equal:
2710         // rs <= r2
2711         slt(scratch, r2, rs);
2712         bne(scratch, zero_reg, 2);
2713         nop();
2714         bal(offset);
2715         break;
2716
2717
2718       // Unsigned comparison.
2719       case Ugreater:
2720         // rs > rt
2721         sltu(scratch, r2, rs);
2722         beq(scratch, zero_reg, 2);
2723         nop();
2724         bal(offset);
2725         break;
2726       case Ugreater_equal:
2727         // rs >= rt
2728         sltu(scratch, rs, r2);
2729         bne(scratch, zero_reg, 2);
2730         nop();
2731         bal(offset);
2732         break;
2733       case Uless:
2734         // rs < r2
2735         sltu(scratch, rs, r2);
2736         bne(scratch, zero_reg, 2);
2737         nop();
2738         bal(offset);
2739         break;
2740       case Uless_equal:
2741         // rs <= r2
2742         sltu(scratch, r2, rs);
2743         bne(scratch, zero_reg, 2);
2744         nop();
2745         bal(offset);
2746         break;
2747       default:
2748         UNREACHABLE();
2749     }
2750   }
2751   // Emit a nop in the branch delay slot if required.
2752   if (bdslot == PROTECT)
2753     nop();
2754 }
2755
2756
2757 void MacroAssembler::BranchAndLinkShort(Label* L, BranchDelaySlot bdslot) {
2758   bal(shifted_branch_offset(L, false));
2759
2760   // Emit a nop in the branch delay slot if required.
2761   if (bdslot == PROTECT)
2762     nop();
2763 }
2764
2765
2766 void MacroAssembler::BranchAndLinkShort(Label* L, Condition cond, Register rs,
2767                                         const Operand& rt,
2768                                         BranchDelaySlot bdslot) {
2769   BRANCH_ARGS_CHECK(cond, rs, rt);
2770
2771   int32_t offset = 0;
2772   Register r2 = no_reg;
2773   Register scratch = at;
2774   if (rt.is_reg()) {
2775     r2 = rt.rm_;
2776   } else if (cond != cc_always) {
2777     r2 = scratch;
2778     li(r2, rt);
2779   }
2780
2781   {
2782     BlockTrampolinePoolScope block_trampoline_pool(this);
2783     switch (cond) {
2784       case cc_always:
2785         offset = shifted_branch_offset(L, false);
2786         bal(offset);
2787         break;
2788       case eq:
2789         bne(rs, r2, 2);
2790         nop();
2791         offset = shifted_branch_offset(L, false);
2792         bal(offset);
2793         break;
2794       case ne:
2795         beq(rs, r2, 2);
2796         nop();
2797         offset = shifted_branch_offset(L, false);
2798         bal(offset);
2799         break;
2800
2801       // Signed comparison.
2802       case greater:
2803         // rs > rt
2804         slt(scratch, r2, rs);
2805         beq(scratch, zero_reg, 2);
2806         nop();
2807         offset = shifted_branch_offset(L, false);
2808         bal(offset);
2809         break;
2810       case greater_equal:
2811         // rs >= rt
2812         slt(scratch, rs, r2);
2813         bne(scratch, zero_reg, 2);
2814         nop();
2815         offset = shifted_branch_offset(L, false);
2816         bal(offset);
2817         break;
2818       case less:
2819         // rs < r2
2820         slt(scratch, rs, r2);
2821         bne(scratch, zero_reg, 2);
2822         nop();
2823         offset = shifted_branch_offset(L, false);
2824         bal(offset);
2825         break;
2826       case less_equal:
2827         // rs <= r2
2828         slt(scratch, r2, rs);
2829         bne(scratch, zero_reg, 2);
2830         nop();
2831         offset = shifted_branch_offset(L, false);
2832         bal(offset);
2833         break;
2834
2835
2836       // Unsigned comparison.
2837       case Ugreater:
2838         // rs > rt
2839         sltu(scratch, r2, rs);
2840         beq(scratch, zero_reg, 2);
2841         nop();
2842         offset = shifted_branch_offset(L, false);
2843         bal(offset);
2844         break;
2845       case Ugreater_equal:
2846         // rs >= rt
2847         sltu(scratch, rs, r2);
2848         bne(scratch, zero_reg, 2);
2849         nop();
2850         offset = shifted_branch_offset(L, false);
2851         bal(offset);
2852         break;
2853       case Uless:
2854         // rs < r2
2855         sltu(scratch, rs, r2);
2856         bne(scratch, zero_reg, 2);
2857         nop();
2858         offset = shifted_branch_offset(L, false);
2859         bal(offset);
2860         break;
2861       case Uless_equal:
2862         // rs <= r2
2863         sltu(scratch, r2, rs);
2864         bne(scratch, zero_reg, 2);
2865         nop();
2866         offset = shifted_branch_offset(L, false);
2867         bal(offset);
2868         break;
2869
2870       default:
2871         UNREACHABLE();
2872     }
2873   }
2874   // Check that offset could actually hold on an int16_t.
2875   DCHECK(is_int16(offset));
2876
2877   // Emit a nop in the branch delay slot if required.
2878   if (bdslot == PROTECT)
2879     nop();
2880 }
2881
2882
2883 void MacroAssembler::Jump(Register target,
2884                           Condition cond,
2885                           Register rs,
2886                           const Operand& rt,
2887                           BranchDelaySlot bd) {
2888   BlockTrampolinePoolScope block_trampoline_pool(this);
2889   if (cond == cc_always) {
2890     jr(target);
2891   } else {
2892     BRANCH_ARGS_CHECK(cond, rs, rt);
2893     Branch(2, NegateCondition(cond), rs, rt);
2894     jr(target);
2895   }
2896   // Emit a nop in the branch delay slot if required.
2897   if (bd == PROTECT)
2898     nop();
2899 }
2900
2901
2902 void MacroAssembler::Jump(intptr_t target,
2903                           RelocInfo::Mode rmode,
2904                           Condition cond,
2905                           Register rs,
2906                           const Operand& rt,
2907                           BranchDelaySlot bd) {
2908   Label skip;
2909   if (cond != cc_always) {
2910     Branch(USE_DELAY_SLOT, &skip, NegateCondition(cond), rs, rt);
2911   }
2912   // The first instruction of 'li' may be placed in the delay slot.
2913   // This is not an issue, t9 is expected to be clobbered anyway.
2914   li(t9, Operand(target, rmode));
2915   Jump(t9, al, zero_reg, Operand(zero_reg), bd);
2916   bind(&skip);
2917 }
2918
2919
2920 void MacroAssembler::Jump(Address target,
2921                           RelocInfo::Mode rmode,
2922                           Condition cond,
2923                           Register rs,
2924                           const Operand& rt,
2925                           BranchDelaySlot bd) {
2926   DCHECK(!RelocInfo::IsCodeTarget(rmode));
2927   Jump(reinterpret_cast<intptr_t>(target), rmode, cond, rs, rt, bd);
2928 }
2929
2930
2931 void MacroAssembler::Jump(Handle<Code> code,
2932                           RelocInfo::Mode rmode,
2933                           Condition cond,
2934                           Register rs,
2935                           const Operand& rt,
2936                           BranchDelaySlot bd) {
2937   DCHECK(RelocInfo::IsCodeTarget(rmode));
2938   AllowDeferredHandleDereference embedding_raw_address;
2939   Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond, rs, rt, bd);
2940 }
2941
2942
2943 int MacroAssembler::CallSize(Register target,
2944                              Condition cond,
2945                              Register rs,
2946                              const Operand& rt,
2947                              BranchDelaySlot bd) {
2948   int size = 0;
2949
2950   if (cond == cc_always) {
2951     size += 1;
2952   } else {
2953     size += 3;
2954   }
2955
2956   if (bd == PROTECT)
2957     size += 1;
2958
2959   return size * kInstrSize;
2960 }
2961
2962
2963 // Note: To call gcc-compiled C code on mips, you must call thru t9.
2964 void MacroAssembler::Call(Register target,
2965                           Condition cond,
2966                           Register rs,
2967                           const Operand& rt,
2968                           BranchDelaySlot bd) {
2969   BlockTrampolinePoolScope block_trampoline_pool(this);
2970   Label start;
2971   bind(&start);
2972   if (cond == cc_always) {
2973     jalr(target);
2974   } else {
2975     BRANCH_ARGS_CHECK(cond, rs, rt);
2976     Branch(2, NegateCondition(cond), rs, rt);
2977     jalr(target);
2978   }
2979   // Emit a nop in the branch delay slot if required.
2980   if (bd == PROTECT)
2981     nop();
2982
2983   DCHECK_EQ(CallSize(target, cond, rs, rt, bd),
2984             SizeOfCodeGeneratedSince(&start));
2985 }
2986
2987
2988 int MacroAssembler::CallSize(Address target,
2989                              RelocInfo::Mode rmode,
2990                              Condition cond,
2991                              Register rs,
2992                              const Operand& rt,
2993                              BranchDelaySlot bd) {
2994   int size = CallSize(t9, cond, rs, rt, bd);
2995   return size + 4 * kInstrSize;
2996 }
2997
2998
2999 void MacroAssembler::Call(Address target,
3000                           RelocInfo::Mode rmode,
3001                           Condition cond,
3002                           Register rs,
3003                           const Operand& rt,
3004                           BranchDelaySlot bd) {
3005   BlockTrampolinePoolScope block_trampoline_pool(this);
3006   Label start;
3007   bind(&start);
3008   int64_t target_int = reinterpret_cast<int64_t>(target);
3009   // Must record previous source positions before the
3010   // li() generates a new code target.
3011   positions_recorder()->WriteRecordedPositions();
3012   li(t9, Operand(target_int, rmode), ADDRESS_LOAD);
3013   Call(t9, cond, rs, rt, bd);
3014   DCHECK_EQ(CallSize(target, rmode, cond, rs, rt, bd),
3015             SizeOfCodeGeneratedSince(&start));
3016 }
3017
3018
3019 int MacroAssembler::CallSize(Handle<Code> code,
3020                              RelocInfo::Mode rmode,
3021                              TypeFeedbackId ast_id,
3022                              Condition cond,
3023                              Register rs,
3024                              const Operand& rt,
3025                              BranchDelaySlot bd) {
3026   AllowDeferredHandleDereference using_raw_address;
3027   return CallSize(reinterpret_cast<Address>(code.location()),
3028       rmode, cond, rs, rt, bd);
3029 }
3030
3031
3032 void MacroAssembler::Call(Handle<Code> code,
3033                           RelocInfo::Mode rmode,
3034                           TypeFeedbackId ast_id,
3035                           Condition cond,
3036                           Register rs,
3037                           const Operand& rt,
3038                           BranchDelaySlot bd) {
3039   BlockTrampolinePoolScope block_trampoline_pool(this);
3040   Label start;
3041   bind(&start);
3042   DCHECK(RelocInfo::IsCodeTarget(rmode));
3043   if (rmode == RelocInfo::CODE_TARGET && !ast_id.IsNone()) {
3044     SetRecordedAstId(ast_id);
3045     rmode = RelocInfo::CODE_TARGET_WITH_ID;
3046   }
3047   AllowDeferredHandleDereference embedding_raw_address;
3048   Call(reinterpret_cast<Address>(code.location()), rmode, cond, rs, rt, bd);
3049   DCHECK_EQ(CallSize(code, rmode, ast_id, cond, rs, rt, bd),
3050             SizeOfCodeGeneratedSince(&start));
3051 }
3052
3053
3054 void MacroAssembler::Ret(Condition cond,
3055                          Register rs,
3056                          const Operand& rt,
3057                          BranchDelaySlot bd) {
3058   Jump(ra, cond, rs, rt, bd);
3059 }
3060
3061
3062 void MacroAssembler::J(Label* L, BranchDelaySlot bdslot) {
3063   BlockTrampolinePoolScope block_trampoline_pool(this);
3064
3065   uint64_t imm28;
3066   imm28 = jump_address(L);
3067   imm28 &= kImm28Mask;
3068   { BlockGrowBufferScope block_buf_growth(this);
3069     // Buffer growth (and relocation) must be blocked for internal references
3070     // until associated instructions are emitted and available to be patched.
3071     RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE_ENCODED);
3072     j(imm28);
3073   }
3074   // Emit a nop in the branch delay slot if required.
3075   if (bdslot == PROTECT)
3076     nop();
3077 }
3078
3079
3080 void MacroAssembler::Jr(Label* L, BranchDelaySlot bdslot) {
3081   BlockTrampolinePoolScope block_trampoline_pool(this);
3082
3083   uint64_t imm64;
3084   imm64 = jump_address(L);
3085   { BlockGrowBufferScope block_buf_growth(this);
3086     // Buffer growth (and relocation) must be blocked for internal references
3087     // until associated instructions are emitted and available to be patched.
3088     RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE_ENCODED);
3089     li(at, Operand(imm64), ADDRESS_LOAD);
3090   }
3091   jr(at);
3092
3093   // Emit a nop in the branch delay slot if required.
3094   if (bdslot == PROTECT)
3095     nop();
3096 }
3097
3098
3099 void MacroAssembler::Jalr(Label* L, BranchDelaySlot bdslot) {
3100   BlockTrampolinePoolScope block_trampoline_pool(this);
3101
3102   uint64_t imm64;
3103   imm64 = jump_address(L);
3104   { BlockGrowBufferScope block_buf_growth(this);
3105     // Buffer growth (and relocation) must be blocked for internal references
3106     // until associated instructions are emitted and available to be patched.
3107     RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE_ENCODED);
3108     li(at, Operand(imm64), ADDRESS_LOAD);
3109   }
3110   jalr(at);
3111
3112   // Emit a nop in the branch delay slot if required.
3113   if (bdslot == PROTECT)
3114     nop();
3115 }
3116
3117
3118 void MacroAssembler::DropAndRet(int drop) {
3119   Ret(USE_DELAY_SLOT);
3120   daddiu(sp, sp, drop * kPointerSize);
3121 }
3122
3123 void MacroAssembler::DropAndRet(int drop,
3124                                 Condition cond,
3125                                 Register r1,
3126                                 const Operand& r2) {
3127   // Both Drop and Ret need to be conditional.
3128   Label skip;
3129   if (cond != cc_always) {
3130     Branch(&skip, NegateCondition(cond), r1, r2);
3131   }
3132
3133   Drop(drop);
3134   Ret();
3135
3136   if (cond != cc_always) {
3137     bind(&skip);
3138   }
3139 }
3140
3141
3142 void MacroAssembler::Drop(int count,
3143                           Condition cond,
3144                           Register reg,
3145                           const Operand& op) {
3146   if (count <= 0) {
3147     return;
3148   }
3149
3150   Label skip;
3151
3152   if (cond != al) {
3153      Branch(&skip, NegateCondition(cond), reg, op);
3154   }
3155
3156   daddiu(sp, sp, count * kPointerSize);
3157
3158   if (cond != al) {
3159     bind(&skip);
3160   }
3161 }
3162
3163
3164
3165 void MacroAssembler::Swap(Register reg1,
3166                           Register reg2,
3167                           Register scratch) {
3168   if (scratch.is(no_reg)) {
3169     Xor(reg1, reg1, Operand(reg2));
3170     Xor(reg2, reg2, Operand(reg1));
3171     Xor(reg1, reg1, Operand(reg2));
3172   } else {
3173     mov(scratch, reg1);
3174     mov(reg1, reg2);
3175     mov(reg2, scratch);
3176   }
3177 }
3178
3179
3180 void MacroAssembler::Call(Label* target) {
3181   BranchAndLink(target);
3182 }
3183
3184
3185 void MacroAssembler::Push(Handle<Object> handle) {
3186   li(at, Operand(handle));
3187   push(at);
3188 }
3189
3190
3191 void MacroAssembler::PushRegisterAsTwoSmis(Register src, Register scratch) {
3192   DCHECK(!src.is(scratch));
3193   mov(scratch, src);
3194   dsrl32(src, src, 0);
3195   dsll32(src, src, 0);
3196   push(src);
3197   dsll32(scratch, scratch, 0);
3198   push(scratch);
3199 }
3200
3201
3202 void MacroAssembler::PopRegisterAsTwoSmis(Register dst, Register scratch) {
3203   DCHECK(!dst.is(scratch));
3204   pop(scratch);
3205   dsrl32(scratch, scratch, 0);
3206   pop(dst);
3207   dsrl32(dst, dst, 0);
3208   dsll32(dst, dst, 0);
3209   or_(dst, dst, scratch);
3210 }
3211
3212
3213 void MacroAssembler::DebugBreak() {
3214   PrepareCEntryArgs(0);
3215   PrepareCEntryFunction(ExternalReference(Runtime::kDebugBreak, isolate()));
3216   CEntryStub ces(isolate(), 1);
3217   DCHECK(AllowThisStubCall(&ces));
3218   Call(ces.GetCode(), RelocInfo::DEBUG_BREAK);
3219 }
3220
3221
3222 // ---------------------------------------------------------------------------
3223 // Exception handling.
3224
3225 void MacroAssembler::PushStackHandler() {
3226   // Adjust this code if not the case.
3227   STATIC_ASSERT(StackHandlerConstants::kSize == 1 * kPointerSize);
3228   STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
3229
3230   // Link the current handler as the next handler.
3231   li(a6, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
3232   ld(a5, MemOperand(a6));
3233   push(a5);
3234
3235   // Set this new handler as the current one.
3236   sd(sp, MemOperand(a6));
3237 }
3238
3239
3240 void MacroAssembler::PopStackHandler() {
3241   STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3242   pop(a1);
3243   Daddu(sp, sp, Operand(static_cast<int64_t>(StackHandlerConstants::kSize -
3244                                              kPointerSize)));
3245   li(at, Operand(ExternalReference(Isolate::kHandlerAddress, isolate())));
3246   sd(a1, MemOperand(at));
3247 }
3248
3249
3250 void MacroAssembler::Allocate(int object_size,
3251                               Register result,
3252                               Register scratch1,
3253                               Register scratch2,
3254                               Label* gc_required,
3255                               AllocationFlags flags) {
3256   DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
3257   if (!FLAG_inline_new) {
3258     if (emit_debug_code()) {
3259       // Trash the registers to simulate an allocation failure.
3260       li(result, 0x7091);
3261       li(scratch1, 0x7191);
3262       li(scratch2, 0x7291);
3263     }
3264     jmp(gc_required);
3265     return;
3266   }
3267
3268   DCHECK(!result.is(scratch1));
3269   DCHECK(!result.is(scratch2));
3270   DCHECK(!scratch1.is(scratch2));
3271   DCHECK(!scratch1.is(t9));
3272   DCHECK(!scratch2.is(t9));
3273   DCHECK(!result.is(t9));
3274
3275   // Make object size into bytes.
3276   if ((flags & SIZE_IN_WORDS) != 0) {
3277     object_size *= kPointerSize;
3278   }
3279   DCHECK(0 == (object_size & kObjectAlignmentMask));
3280
3281   // Check relative positions of allocation top and limit addresses.
3282   // ARM adds additional checks to make sure the ldm instruction can be
3283   // used. On MIPS we don't have ldm so we don't need additional checks either.
3284   ExternalReference allocation_top =
3285       AllocationUtils::GetAllocationTopReference(isolate(), flags);
3286   ExternalReference allocation_limit =
3287       AllocationUtils::GetAllocationLimitReference(isolate(), flags);
3288
3289   intptr_t top   =
3290       reinterpret_cast<intptr_t>(allocation_top.address());
3291   intptr_t limit =
3292       reinterpret_cast<intptr_t>(allocation_limit.address());
3293   DCHECK((limit - top) == kPointerSize);
3294
3295   // Set up allocation top address and object size registers.
3296   Register topaddr = scratch1;
3297   li(topaddr, Operand(allocation_top));
3298
3299   // This code stores a temporary value in t9.
3300   if ((flags & RESULT_CONTAINS_TOP) == 0) {
3301     // Load allocation top into result and allocation limit into t9.
3302     ld(result, MemOperand(topaddr));
3303     ld(t9, MemOperand(topaddr, kPointerSize));
3304   } else {
3305     if (emit_debug_code()) {
3306       // Assert that result actually contains top on entry. t9 is used
3307       // immediately below so this use of t9 does not cause difference with
3308       // respect to register content between debug and release mode.
3309       ld(t9, MemOperand(topaddr));
3310       Check(eq, kUnexpectedAllocationTop, result, Operand(t9));
3311     }
3312     // Load allocation limit into t9. Result already contains allocation top.
3313     ld(t9, MemOperand(topaddr, limit - top));
3314   }
3315
3316   DCHECK(kPointerSize == kDoubleSize);
3317   if (emit_debug_code()) {
3318     And(at, result, Operand(kDoubleAlignmentMask));
3319     Check(eq, kAllocationIsNotDoubleAligned, at, Operand(zero_reg));
3320   }
3321
3322   // Calculate new top and bail out if new space is exhausted. Use result
3323   // to calculate the new top.
3324   Daddu(scratch2, result, Operand(object_size));
3325   Branch(gc_required, Ugreater, scratch2, Operand(t9));
3326   sd(scratch2, MemOperand(topaddr));
3327
3328   // Tag object if requested.
3329   if ((flags & TAG_OBJECT) != 0) {
3330     Daddu(result, result, Operand(kHeapObjectTag));
3331   }
3332 }
3333
3334
3335 void MacroAssembler::Allocate(Register object_size,
3336                               Register result,
3337                               Register scratch1,
3338                               Register scratch2,
3339                               Label* gc_required,
3340                               AllocationFlags flags) {
3341   if (!FLAG_inline_new) {
3342     if (emit_debug_code()) {
3343       // Trash the registers to simulate an allocation failure.
3344       li(result, 0x7091);
3345       li(scratch1, 0x7191);
3346       li(scratch2, 0x7291);
3347     }
3348     jmp(gc_required);
3349     return;
3350   }
3351
3352   DCHECK(!result.is(scratch1));
3353   DCHECK(!result.is(scratch2));
3354   DCHECK(!scratch1.is(scratch2));
3355   DCHECK(!object_size.is(t9));
3356   DCHECK(!scratch1.is(t9) && !scratch2.is(t9) && !result.is(t9));
3357
3358   // Check relative positions of allocation top and limit addresses.
3359   // ARM adds additional checks to make sure the ldm instruction can be
3360   // used. On MIPS we don't have ldm so we don't need additional checks either.
3361   ExternalReference allocation_top =
3362       AllocationUtils::GetAllocationTopReference(isolate(), flags);
3363   ExternalReference allocation_limit =
3364       AllocationUtils::GetAllocationLimitReference(isolate(), flags);
3365   intptr_t top   =
3366       reinterpret_cast<intptr_t>(allocation_top.address());
3367   intptr_t limit =
3368       reinterpret_cast<intptr_t>(allocation_limit.address());
3369   DCHECK((limit - top) == kPointerSize);
3370
3371   // Set up allocation top address and object size registers.
3372   Register topaddr = scratch1;
3373   li(topaddr, Operand(allocation_top));
3374
3375   // This code stores a temporary value in t9.
3376   if ((flags & RESULT_CONTAINS_TOP) == 0) {
3377     // Load allocation top into result and allocation limit into t9.
3378     ld(result, MemOperand(topaddr));
3379     ld(t9, MemOperand(topaddr, kPointerSize));
3380   } else {
3381     if (emit_debug_code()) {
3382       // Assert that result actually contains top on entry. t9 is used
3383       // immediately below so this use of t9 does not cause difference with
3384       // respect to register content between debug and release mode.
3385       ld(t9, MemOperand(topaddr));
3386       Check(eq, kUnexpectedAllocationTop, result, Operand(t9));
3387     }
3388     // Load allocation limit into t9. Result already contains allocation top.
3389     ld(t9, MemOperand(topaddr, limit - top));
3390   }
3391
3392   DCHECK(kPointerSize == kDoubleSize);
3393   if (emit_debug_code()) {
3394     And(at, result, Operand(kDoubleAlignmentMask));
3395     Check(eq, kAllocationIsNotDoubleAligned, at, Operand(zero_reg));
3396   }
3397
3398   // Calculate new top and bail out if new space is exhausted. Use result
3399   // to calculate the new top. Object size may be in words so a shift is
3400   // required to get the number of bytes.
3401   if ((flags & SIZE_IN_WORDS) != 0) {
3402     dsll(scratch2, object_size, kPointerSizeLog2);
3403     Daddu(scratch2, result, scratch2);
3404   } else {
3405     Daddu(scratch2, result, Operand(object_size));
3406   }
3407   Branch(gc_required, Ugreater, scratch2, Operand(t9));
3408
3409   // Update allocation top. result temporarily holds the new top.
3410   if (emit_debug_code()) {
3411     And(t9, scratch2, Operand(kObjectAlignmentMask));
3412     Check(eq, kUnalignedAllocationInNewSpace, t9, Operand(zero_reg));
3413   }
3414   sd(scratch2, MemOperand(topaddr));
3415
3416   // Tag object if requested.
3417   if ((flags & TAG_OBJECT) != 0) {
3418     Daddu(result, result, Operand(kHeapObjectTag));
3419   }
3420 }
3421
3422
3423 void MacroAssembler::UndoAllocationInNewSpace(Register object,
3424                                               Register scratch) {
3425   ExternalReference new_space_allocation_top =
3426       ExternalReference::new_space_allocation_top_address(isolate());
3427
3428   // Make sure the object has no tag before resetting top.
3429   And(object, object, Operand(~kHeapObjectTagMask));
3430 #ifdef DEBUG
3431   // Check that the object un-allocated is below the current top.
3432   li(scratch, Operand(new_space_allocation_top));
3433   ld(scratch, MemOperand(scratch));
3434   Check(less, kUndoAllocationOfNonAllocatedMemory,
3435       object, Operand(scratch));
3436 #endif
3437   // Write the address of the object to un-allocate as the current top.
3438   li(scratch, Operand(new_space_allocation_top));
3439   sd(object, MemOperand(scratch));
3440 }
3441
3442
3443 void MacroAssembler::AllocateTwoByteString(Register result,
3444                                            Register length,
3445                                            Register scratch1,
3446                                            Register scratch2,
3447                                            Register scratch3,
3448                                            Label* gc_required) {
3449   // Calculate the number of bytes needed for the characters in the string while
3450   // observing object alignment.
3451   DCHECK((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3452   dsll(scratch1, length, 1);  // Length in bytes, not chars.
3453   daddiu(scratch1, scratch1,
3454        kObjectAlignmentMask + SeqTwoByteString::kHeaderSize);
3455   And(scratch1, scratch1, Operand(~kObjectAlignmentMask));
3456
3457   // Allocate two-byte string in new space.
3458   Allocate(scratch1,
3459            result,
3460            scratch2,
3461            scratch3,
3462            gc_required,
3463            TAG_OBJECT);
3464
3465   // Set the map, length and hash field.
3466   InitializeNewString(result,
3467                       length,
3468                       Heap::kStringMapRootIndex,
3469                       scratch1,
3470                       scratch2);
3471 }
3472
3473
3474 void MacroAssembler::AllocateOneByteString(Register result, Register length,
3475                                            Register scratch1, Register scratch2,
3476                                            Register scratch3,
3477                                            Label* gc_required) {
3478   // Calculate the number of bytes needed for the characters in the string
3479   // while observing object alignment.
3480   DCHECK((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3481   DCHECK(kCharSize == 1);
3482   daddiu(scratch1, length,
3483       kObjectAlignmentMask + SeqOneByteString::kHeaderSize);
3484   And(scratch1, scratch1, Operand(~kObjectAlignmentMask));
3485
3486   // Allocate one-byte string in new space.
3487   Allocate(scratch1,
3488            result,
3489            scratch2,
3490            scratch3,
3491            gc_required,
3492            TAG_OBJECT);
3493
3494   // Set the map, length and hash field.
3495   InitializeNewString(result, length, Heap::kOneByteStringMapRootIndex,
3496                       scratch1, scratch2);
3497 }
3498
3499
3500 void MacroAssembler::AllocateTwoByteConsString(Register result,
3501                                                Register length,
3502                                                Register scratch1,
3503                                                Register scratch2,
3504                                                Label* gc_required) {
3505   Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
3506            TAG_OBJECT);
3507   InitializeNewString(result,
3508                       length,
3509                       Heap::kConsStringMapRootIndex,
3510                       scratch1,
3511                       scratch2);
3512 }
3513
3514
3515 void MacroAssembler::AllocateOneByteConsString(Register result, Register length,
3516                                                Register scratch1,
3517                                                Register scratch2,
3518                                                Label* gc_required) {
3519   Allocate(ConsString::kSize,
3520            result,
3521            scratch1,
3522            scratch2,
3523            gc_required,
3524            TAG_OBJECT);
3525
3526   InitializeNewString(result, length, Heap::kConsOneByteStringMapRootIndex,
3527                       scratch1, scratch2);
3528 }
3529
3530
3531 void MacroAssembler::AllocateTwoByteSlicedString(Register result,
3532                                                  Register length,
3533                                                  Register scratch1,
3534                                                  Register scratch2,
3535                                                  Label* gc_required) {
3536   Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
3537            TAG_OBJECT);
3538
3539   InitializeNewString(result,
3540                       length,
3541                       Heap::kSlicedStringMapRootIndex,
3542                       scratch1,
3543                       scratch2);
3544 }
3545
3546
3547 void MacroAssembler::AllocateOneByteSlicedString(Register result,
3548                                                  Register length,
3549                                                  Register scratch1,
3550                                                  Register scratch2,
3551                                                  Label* gc_required) {
3552   Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
3553            TAG_OBJECT);
3554
3555   InitializeNewString(result, length, Heap::kSlicedOneByteStringMapRootIndex,
3556                       scratch1, scratch2);
3557 }
3558
3559
3560 void MacroAssembler::JumpIfNotUniqueNameInstanceType(Register reg,
3561                                                      Label* not_unique_name) {
3562   STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3563   Label succeed;
3564   And(at, reg, Operand(kIsNotStringMask | kIsNotInternalizedMask));
3565   Branch(&succeed, eq, at, Operand(zero_reg));
3566   Branch(not_unique_name, ne, reg, Operand(SYMBOL_TYPE));
3567
3568   bind(&succeed);
3569 }
3570
3571
3572 // Allocates a heap number or jumps to the label if the young space is full and
3573 // a scavenge is needed.
3574 void MacroAssembler::AllocateHeapNumber(Register result,
3575                                         Register scratch1,
3576                                         Register scratch2,
3577                                         Register heap_number_map,
3578                                         Label* need_gc,
3579                                         TaggingMode tagging_mode,
3580                                         MutableMode mode) {
3581   // Allocate an object in the heap for the heap number and tag it as a heap
3582   // object.
3583   Allocate(HeapNumber::kSize, result, scratch1, scratch2, need_gc,
3584            tagging_mode == TAG_RESULT ? TAG_OBJECT : NO_ALLOCATION_FLAGS);
3585
3586   Heap::RootListIndex map_index = mode == MUTABLE
3587       ? Heap::kMutableHeapNumberMapRootIndex
3588       : Heap::kHeapNumberMapRootIndex;
3589   AssertIsRoot(heap_number_map, map_index);
3590
3591   // Store heap number map in the allocated object.
3592   if (tagging_mode == TAG_RESULT) {
3593     sd(heap_number_map, FieldMemOperand(result, HeapObject::kMapOffset));
3594   } else {
3595     sd(heap_number_map, MemOperand(result, HeapObject::kMapOffset));
3596   }
3597 }
3598
3599
3600 void MacroAssembler::AllocateHeapNumberWithValue(Register result,
3601                                                  FPURegister value,
3602                                                  Register scratch1,
3603                                                  Register scratch2,
3604                                                  Label* gc_required) {
3605   LoadRoot(t8, Heap::kHeapNumberMapRootIndex);
3606   AllocateHeapNumber(result, scratch1, scratch2, t8, gc_required);
3607   sdc1(value, FieldMemOperand(result, HeapNumber::kValueOffset));
3608 }
3609
3610
3611 // Copies a fixed number of fields of heap objects from src to dst.
3612 void MacroAssembler::CopyFields(Register dst,
3613                                 Register src,
3614                                 RegList temps,
3615                                 int field_count) {
3616   DCHECK((temps & dst.bit()) == 0);
3617   DCHECK((temps & src.bit()) == 0);
3618   // Primitive implementation using only one temporary register.
3619
3620   Register tmp = no_reg;
3621   // Find a temp register in temps list.
3622   for (int i = 0; i < kNumRegisters; i++) {
3623     if ((temps & (1 << i)) != 0) {
3624       tmp.code_ = i;
3625       break;
3626     }
3627   }
3628   DCHECK(!tmp.is(no_reg));
3629
3630   for (int i = 0; i < field_count; i++) {
3631     ld(tmp, FieldMemOperand(src, i * kPointerSize));
3632     sd(tmp, FieldMemOperand(dst, i * kPointerSize));
3633   }
3634 }
3635
3636
3637 void MacroAssembler::CopyBytes(Register src,
3638                                Register dst,
3639                                Register length,
3640                                Register scratch) {
3641   Label align_loop_1, word_loop, byte_loop, byte_loop_1, done;
3642
3643   // Align src before copying in word size chunks.
3644   Branch(&byte_loop, le, length, Operand(kPointerSize));
3645   bind(&align_loop_1);
3646   And(scratch, src, kPointerSize - 1);
3647   Branch(&word_loop, eq, scratch, Operand(zero_reg));
3648   lbu(scratch, MemOperand(src));
3649   Daddu(src, src, 1);
3650   sb(scratch, MemOperand(dst));
3651   Daddu(dst, dst, 1);
3652   Dsubu(length, length, Operand(1));
3653   Branch(&align_loop_1, ne, length, Operand(zero_reg));
3654
3655   // Copy bytes in word size chunks.
3656   bind(&word_loop);
3657   if (emit_debug_code()) {
3658     And(scratch, src, kPointerSize - 1);
3659     Assert(eq, kExpectingAlignmentForCopyBytes,
3660         scratch, Operand(zero_reg));
3661   }
3662   Branch(&byte_loop, lt, length, Operand(kPointerSize));
3663   ld(scratch, MemOperand(src));
3664   Daddu(src, src, kPointerSize);
3665
3666   // TODO(kalmard) check if this can be optimized to use sw in most cases.
3667   // Can't use unaligned access - copy byte by byte.
3668   sb(scratch, MemOperand(dst, 0));
3669   dsrl(scratch, scratch, 8);
3670   sb(scratch, MemOperand(dst, 1));
3671   dsrl(scratch, scratch, 8);
3672   sb(scratch, MemOperand(dst, 2));
3673   dsrl(scratch, scratch, 8);
3674   sb(scratch, MemOperand(dst, 3));
3675   dsrl(scratch, scratch, 8);
3676   sb(scratch, MemOperand(dst, 4));
3677   dsrl(scratch, scratch, 8);
3678   sb(scratch, MemOperand(dst, 5));
3679   dsrl(scratch, scratch, 8);
3680   sb(scratch, MemOperand(dst, 6));
3681   dsrl(scratch, scratch, 8);
3682   sb(scratch, MemOperand(dst, 7));
3683   Daddu(dst, dst, 8);
3684
3685   Dsubu(length, length, Operand(kPointerSize));
3686   Branch(&word_loop);
3687
3688   // Copy the last bytes if any left.
3689   bind(&byte_loop);
3690   Branch(&done, eq, length, Operand(zero_reg));
3691   bind(&byte_loop_1);
3692   lbu(scratch, MemOperand(src));
3693   Daddu(src, src, 1);
3694   sb(scratch, MemOperand(dst));
3695   Daddu(dst, dst, 1);
3696   Dsubu(length, length, Operand(1));
3697   Branch(&byte_loop_1, ne, length, Operand(zero_reg));
3698   bind(&done);
3699 }
3700
3701
3702 void MacroAssembler::InitializeFieldsWithFiller(Register start_offset,
3703                                                 Register end_offset,
3704                                                 Register filler) {
3705   Label loop, entry;
3706   Branch(&entry);
3707   bind(&loop);
3708   sd(filler, MemOperand(start_offset));
3709   Daddu(start_offset, start_offset, kPointerSize);
3710   bind(&entry);
3711   Branch(&loop, lt, start_offset, Operand(end_offset));
3712 }
3713
3714
3715 void MacroAssembler::CheckFastElements(Register map,
3716                                        Register scratch,
3717                                        Label* fail) {
3718   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3719   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3720   STATIC_ASSERT(FAST_ELEMENTS == 2);
3721   STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3722   lbu(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3723   Branch(fail, hi, scratch,
3724          Operand(Map::kMaximumBitField2FastHoleyElementValue));
3725 }
3726
3727
3728 void MacroAssembler::CheckFastObjectElements(Register map,
3729                                              Register scratch,
3730                                              Label* fail) {
3731   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3732   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3733   STATIC_ASSERT(FAST_ELEMENTS == 2);
3734   STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3735   lbu(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3736   Branch(fail, ls, scratch,
3737          Operand(Map::kMaximumBitField2FastHoleySmiElementValue));
3738   Branch(fail, hi, scratch,
3739          Operand(Map::kMaximumBitField2FastHoleyElementValue));
3740 }
3741
3742
3743 void MacroAssembler::CheckFastSmiElements(Register map,
3744                                           Register scratch,
3745                                           Label* fail) {
3746   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3747   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3748   lbu(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3749   Branch(fail, hi, scratch,
3750          Operand(Map::kMaximumBitField2FastHoleySmiElementValue));
3751 }
3752
3753
3754 void MacroAssembler::StoreNumberToDoubleElements(Register value_reg,
3755                                                  Register key_reg,
3756                                                  Register elements_reg,
3757                                                  Register scratch1,
3758                                                  Register scratch2,
3759                                                  Label* fail,
3760                                                  int elements_offset) {
3761   Label smi_value, done;
3762
3763   // Handle smi values specially.
3764   JumpIfSmi(value_reg, &smi_value);
3765
3766   // Ensure that the object is a heap number.
3767   CheckMap(value_reg,
3768            scratch1,
3769            Heap::kHeapNumberMapRootIndex,
3770            fail,
3771            DONT_DO_SMI_CHECK);
3772
3773   // Double value, turn potential sNaN into qNan.
3774   DoubleRegister double_result = f0;
3775   DoubleRegister double_scratch = f2;
3776
3777   ldc1(double_result, FieldMemOperand(value_reg, HeapNumber::kValueOffset));
3778   Branch(USE_DELAY_SLOT, &done);  // Canonicalization is one instruction.
3779   FPUCanonicalizeNaN(double_result, double_result);
3780
3781   bind(&smi_value);
3782   // scratch1 is now effective address of the double element.
3783   // Untag and transfer.
3784   dsrl32(at, value_reg, 0);
3785   mtc1(at, double_scratch);
3786   cvt_d_w(double_result, double_scratch);
3787
3788   bind(&done);
3789   Daddu(scratch1, elements_reg,
3790       Operand(FixedDoubleArray::kHeaderSize - kHeapObjectTag -
3791               elements_offset));
3792   dsra(scratch2, key_reg, 32 - kDoubleSizeLog2);
3793   Daddu(scratch1, scratch1, scratch2);
3794   sdc1(double_result, MemOperand(scratch1, 0));
3795 }
3796
3797
3798 void MacroAssembler::CompareMapAndBranch(Register obj,
3799                                          Register scratch,
3800                                          Handle<Map> map,
3801                                          Label* early_success,
3802                                          Condition cond,
3803                                          Label* branch_to) {
3804   ld(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
3805   CompareMapAndBranch(scratch, map, early_success, cond, branch_to);
3806 }
3807
3808
3809 void MacroAssembler::CompareMapAndBranch(Register obj_map,
3810                                          Handle<Map> map,
3811                                          Label* early_success,
3812                                          Condition cond,
3813                                          Label* branch_to) {
3814   Branch(branch_to, cond, obj_map, Operand(map));
3815 }
3816
3817
3818 void MacroAssembler::CheckMap(Register obj,
3819                               Register scratch,
3820                               Handle<Map> map,
3821                               Label* fail,
3822                               SmiCheckType smi_check_type) {
3823   if (smi_check_type == DO_SMI_CHECK) {
3824     JumpIfSmi(obj, fail);
3825   }
3826   Label success;
3827   CompareMapAndBranch(obj, scratch, map, &success, ne, fail);
3828   bind(&success);
3829 }
3830
3831
3832 void MacroAssembler::DispatchWeakMap(Register obj, Register scratch1,
3833                                      Register scratch2, Handle<WeakCell> cell,
3834                                      Handle<Code> success,
3835                                      SmiCheckType smi_check_type) {
3836   Label fail;
3837   if (smi_check_type == DO_SMI_CHECK) {
3838     JumpIfSmi(obj, &fail);
3839   }
3840   ld(scratch1, FieldMemOperand(obj, HeapObject::kMapOffset));
3841   GetWeakValue(scratch2, cell);
3842   Jump(success, RelocInfo::CODE_TARGET, eq, scratch1, Operand(scratch2));
3843   bind(&fail);
3844 }
3845
3846
3847 void MacroAssembler::CheckMap(Register obj,
3848                               Register scratch,
3849                               Heap::RootListIndex index,
3850                               Label* fail,
3851                               SmiCheckType smi_check_type) {
3852   if (smi_check_type == DO_SMI_CHECK) {
3853     JumpIfSmi(obj, fail);
3854   }
3855   ld(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
3856   LoadRoot(at, index);
3857   Branch(fail, ne, scratch, Operand(at));
3858 }
3859
3860
3861 void MacroAssembler::GetWeakValue(Register value, Handle<WeakCell> cell) {
3862   li(value, Operand(cell));
3863   ld(value, FieldMemOperand(value, WeakCell::kValueOffset));
3864 }
3865
3866 void MacroAssembler::FPUCanonicalizeNaN(const DoubleRegister dst,
3867                                         const DoubleRegister src) {
3868   sub_d(dst, src, kDoubleRegZero);
3869 }
3870
3871 void MacroAssembler::LoadWeakValue(Register value, Handle<WeakCell> cell,
3872                                    Label* miss) {
3873   GetWeakValue(value, cell);
3874   JumpIfSmi(value, miss);
3875 }
3876
3877
3878 void MacroAssembler::MovFromFloatResult(const DoubleRegister dst) {
3879   if (IsMipsSoftFloatABI) {
3880     Move(dst, v0, v1);
3881   } else {
3882     Move(dst, f0);  // Reg f0 is o32 ABI FP return value.
3883   }
3884 }
3885
3886
3887 void MacroAssembler::MovFromFloatParameter(const DoubleRegister dst) {
3888   if (IsMipsSoftFloatABI) {
3889     Move(dst, a0, a1);
3890   } else {
3891     Move(dst, f12);  // Reg f12 is o32 ABI FP first argument value.
3892   }
3893 }
3894
3895
3896 void MacroAssembler::MovToFloatParameter(DoubleRegister src) {
3897   if (!IsMipsSoftFloatABI) {
3898     Move(f12, src);
3899   } else {
3900     Move(a0, a1, src);
3901   }
3902 }
3903
3904
3905 void MacroAssembler::MovToFloatResult(DoubleRegister src) {
3906   if (!IsMipsSoftFloatABI) {
3907     Move(f0, src);
3908   } else {
3909     Move(v0, v1, src);
3910   }
3911 }
3912
3913
3914 void MacroAssembler::MovToFloatParameters(DoubleRegister src1,
3915                                           DoubleRegister src2) {
3916   if (!IsMipsSoftFloatABI) {
3917     const DoubleRegister fparg2 = (kMipsAbi == kN64) ? f13 : f14;
3918     if (src2.is(f12)) {
3919       DCHECK(!src1.is(fparg2));
3920       Move(fparg2, src2);
3921       Move(f12, src1);
3922     } else {
3923       Move(f12, src1);
3924       Move(fparg2, src2);
3925     }
3926   } else {
3927     Move(a0, a1, src1);
3928     Move(a2, a3, src2);
3929   }
3930 }
3931
3932
3933 // -----------------------------------------------------------------------------
3934 // JavaScript invokes.
3935
3936 void MacroAssembler::InvokePrologue(const ParameterCount& expected,
3937                                     const ParameterCount& actual,
3938                                     Handle<Code> code_constant,
3939                                     Register code_reg,
3940                                     Label* done,
3941                                     bool* definitely_mismatches,
3942                                     InvokeFlag flag,
3943                                     const CallWrapper& call_wrapper) {
3944   bool definitely_matches = false;
3945   *definitely_mismatches = false;
3946   Label regular_invoke;
3947
3948   // Check whether the expected and actual arguments count match. If not,
3949   // setup registers according to contract with ArgumentsAdaptorTrampoline:
3950   //  a0: actual arguments count
3951   //  a1: function (passed through to callee)
3952   //  a2: expected arguments count
3953
3954   // The code below is made a lot easier because the calling code already sets
3955   // up actual and expected registers according to the contract if values are
3956   // passed in registers.
3957   DCHECK(actual.is_immediate() || actual.reg().is(a0));
3958   DCHECK(expected.is_immediate() || expected.reg().is(a2));
3959   DCHECK((!code_constant.is_null() && code_reg.is(no_reg)) || code_reg.is(a3));
3960
3961   if (expected.is_immediate()) {
3962     DCHECK(actual.is_immediate());
3963     if (expected.immediate() == actual.immediate()) {
3964       definitely_matches = true;
3965     } else {
3966       li(a0, Operand(actual.immediate()));
3967       const int sentinel = SharedFunctionInfo::kDontAdaptArgumentsSentinel;
3968       if (expected.immediate() == sentinel) {
3969         // Don't worry about adapting arguments for builtins that
3970         // don't want that done. Skip adaption code by making it look
3971         // like we have a match between expected and actual number of
3972         // arguments.
3973         definitely_matches = true;
3974       } else {
3975         *definitely_mismatches = true;
3976         li(a2, Operand(expected.immediate()));
3977       }
3978     }
3979   } else if (actual.is_immediate()) {
3980     Branch(&regular_invoke, eq, expected.reg(), Operand(actual.immediate()));
3981     li(a0, Operand(actual.immediate()));
3982   } else {
3983     Branch(&regular_invoke, eq, expected.reg(), Operand(actual.reg()));
3984   }
3985
3986   if (!definitely_matches) {
3987     if (!code_constant.is_null()) {
3988       li(a3, Operand(code_constant));
3989       daddiu(a3, a3, Code::kHeaderSize - kHeapObjectTag);
3990     }
3991
3992     Handle<Code> adaptor =
3993         isolate()->builtins()->ArgumentsAdaptorTrampoline();
3994     if (flag == CALL_FUNCTION) {
3995       call_wrapper.BeforeCall(CallSize(adaptor));
3996       Call(adaptor);
3997       call_wrapper.AfterCall();
3998       if (!*definitely_mismatches) {
3999         Branch(done);
4000       }
4001     } else {
4002       Jump(adaptor, RelocInfo::CODE_TARGET);
4003     }
4004     bind(&regular_invoke);
4005   }
4006 }
4007
4008
4009 void MacroAssembler::InvokeCode(Register code,
4010                                 const ParameterCount& expected,
4011                                 const ParameterCount& actual,
4012                                 InvokeFlag flag,
4013                                 const CallWrapper& call_wrapper) {
4014   // You can't call a function without a valid frame.
4015   DCHECK(flag == JUMP_FUNCTION || has_frame());
4016
4017   Label done;
4018
4019   bool definitely_mismatches = false;
4020   InvokePrologue(expected, actual, Handle<Code>::null(), code,
4021                  &done, &definitely_mismatches, flag,
4022                  call_wrapper);
4023   if (!definitely_mismatches) {
4024     if (flag == CALL_FUNCTION) {
4025       call_wrapper.BeforeCall(CallSize(code));
4026       Call(code);
4027       call_wrapper.AfterCall();
4028     } else {
4029       DCHECK(flag == JUMP_FUNCTION);
4030       Jump(code);
4031     }
4032     // Continue here if InvokePrologue does handle the invocation due to
4033     // mismatched parameter counts.
4034     bind(&done);
4035   }
4036 }
4037
4038
4039 void MacroAssembler::InvokeFunction(Register function,
4040                                     const ParameterCount& actual,
4041                                     InvokeFlag flag,
4042                                     const CallWrapper& call_wrapper) {
4043   // You can't call a function without a valid frame.
4044   DCHECK(flag == JUMP_FUNCTION || has_frame());
4045
4046   // Contract with called JS functions requires that function is passed in a1.
4047   DCHECK(function.is(a1));
4048   Register expected_reg = a2;
4049   Register code_reg = a3;
4050   ld(code_reg, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
4051   ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4052   // The argument count is stored as int32_t on 64-bit platforms.
4053   // TODO(plind): Smi on 32-bit platforms.
4054   lw(expected_reg,
4055       FieldMemOperand(code_reg,
4056                       SharedFunctionInfo::kFormalParameterCountOffset));
4057   ld(code_reg, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4058   ParameterCount expected(expected_reg);
4059   InvokeCode(code_reg, expected, actual, flag, call_wrapper);
4060 }
4061
4062
4063 void MacroAssembler::InvokeFunction(Register function,
4064                                     const ParameterCount& expected,
4065                                     const ParameterCount& actual,
4066                                     InvokeFlag flag,
4067                                     const CallWrapper& call_wrapper) {
4068   // You can't call a function without a valid frame.
4069   DCHECK(flag == JUMP_FUNCTION || has_frame());
4070
4071   // Contract with called JS functions requires that function is passed in a1.
4072   DCHECK(function.is(a1));
4073
4074   // Get the function and setup the context.
4075   ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4076
4077   // We call indirectly through the code field in the function to
4078   // allow recompilation to take effect without changing any of the
4079   // call sites.
4080   ld(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4081   InvokeCode(a3, expected, actual, flag, call_wrapper);
4082 }
4083
4084
4085 void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
4086                                     const ParameterCount& expected,
4087                                     const ParameterCount& actual,
4088                                     InvokeFlag flag,
4089                                     const CallWrapper& call_wrapper) {
4090   li(a1, function);
4091   InvokeFunction(a1, expected, actual, flag, call_wrapper);
4092 }
4093
4094
4095 void MacroAssembler::IsObjectJSObjectType(Register heap_object,
4096                                           Register map,
4097                                           Register scratch,
4098                                           Label* fail) {
4099   ld(map, FieldMemOperand(heap_object, HeapObject::kMapOffset));
4100   IsInstanceJSObjectType(map, scratch, fail);
4101 }
4102
4103
4104 void MacroAssembler::IsInstanceJSObjectType(Register map,
4105                                             Register scratch,
4106                                             Label* fail) {
4107   lbu(scratch, FieldMemOperand(map, Map::kInstanceTypeOffset));
4108   Branch(fail, lt, scratch, Operand(FIRST_NONCALLABLE_SPEC_OBJECT_TYPE));
4109   Branch(fail, gt, scratch, Operand(LAST_NONCALLABLE_SPEC_OBJECT_TYPE));
4110 }
4111
4112
4113 void MacroAssembler::IsObjectJSStringType(Register object,
4114                                           Register scratch,
4115                                           Label* fail) {
4116   DCHECK(kNotStringTag != 0);
4117
4118   ld(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4119   lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4120   And(scratch, scratch, Operand(kIsNotStringMask));
4121   Branch(fail, ne, scratch, Operand(zero_reg));
4122 }
4123
4124
4125 void MacroAssembler::IsObjectNameType(Register object,
4126                                       Register scratch,
4127                                       Label* fail) {
4128   ld(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
4129   lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4130   Branch(fail, hi, scratch, Operand(LAST_NAME_TYPE));
4131 }
4132
4133
4134 // ---------------------------------------------------------------------------
4135 // Support functions.
4136
4137
4138 void MacroAssembler::GetMapConstructor(Register result, Register map,
4139                                        Register temp, Register temp2) {
4140   Label done, loop;
4141   ld(result, FieldMemOperand(map, Map::kConstructorOrBackPointerOffset));
4142   bind(&loop);
4143   JumpIfSmi(result, &done);
4144   GetObjectType(result, temp, temp2);
4145   Branch(&done, ne, temp2, Operand(MAP_TYPE));
4146   ld(result, FieldMemOperand(result, Map::kConstructorOrBackPointerOffset));
4147   Branch(&loop);
4148   bind(&done);
4149 }
4150
4151
4152 void MacroAssembler::TryGetFunctionPrototype(Register function,
4153                                              Register result,
4154                                              Register scratch,
4155                                              Label* miss,
4156                                              bool miss_on_bound_function) {
4157   Label non_instance;
4158   if (miss_on_bound_function) {
4159     // Check that the receiver isn't a smi.
4160     JumpIfSmi(function, miss);
4161
4162     // Check that the function really is a function.  Load map into result reg.
4163     GetObjectType(function, result, scratch);
4164     Branch(miss, ne, scratch, Operand(JS_FUNCTION_TYPE));
4165
4166     ld(scratch,
4167        FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
4168     lwu(scratch,
4169         FieldMemOperand(scratch, SharedFunctionInfo::kCompilerHintsOffset));
4170     And(scratch, scratch,
4171         Operand(1 << SharedFunctionInfo::kBoundFunction));
4172     Branch(miss, ne, scratch, Operand(zero_reg));
4173
4174     // Make sure that the function has an instance prototype.
4175     lbu(scratch, FieldMemOperand(result, Map::kBitFieldOffset));
4176     And(scratch, scratch, Operand(1 << Map::kHasNonInstancePrototype));
4177     Branch(&non_instance, ne, scratch, Operand(zero_reg));
4178   }
4179
4180   // Get the prototype or initial map from the function.
4181   ld(result,
4182      FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
4183
4184   // If the prototype or initial map is the hole, don't return it and
4185   // simply miss the cache instead. This will allow us to allocate a
4186   // prototype object on-demand in the runtime system.
4187   LoadRoot(t8, Heap::kTheHoleValueRootIndex);
4188   Branch(miss, eq, result, Operand(t8));
4189
4190   // If the function does not have an initial map, we're done.
4191   Label done;
4192   GetObjectType(result, scratch, scratch);
4193   Branch(&done, ne, scratch, Operand(MAP_TYPE));
4194
4195   // Get the prototype from the initial map.
4196   ld(result, FieldMemOperand(result, Map::kPrototypeOffset));
4197
4198   if (miss_on_bound_function) {
4199     jmp(&done);
4200
4201     // Non-instance prototype: Fetch prototype from constructor field
4202     // in initial map.
4203     bind(&non_instance);
4204     GetMapConstructor(result, result, scratch, scratch);
4205   }
4206
4207   // All done.
4208   bind(&done);
4209 }
4210
4211
4212 void MacroAssembler::GetObjectType(Register object,
4213                                    Register map,
4214                                    Register type_reg) {
4215   ld(map, FieldMemOperand(object, HeapObject::kMapOffset));
4216   lbu(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
4217 }
4218
4219
4220 // -----------------------------------------------------------------------------
4221 // Runtime calls.
4222
4223 void MacroAssembler::CallStub(CodeStub* stub,
4224                               TypeFeedbackId ast_id,
4225                               Condition cond,
4226                               Register r1,
4227                               const Operand& r2,
4228                               BranchDelaySlot bd) {
4229   DCHECK(AllowThisStubCall(stub));  // Stub calls are not allowed in some stubs.
4230   Call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id,
4231        cond, r1, r2, bd);
4232 }
4233
4234
4235 void MacroAssembler::TailCallStub(CodeStub* stub,
4236                                   Condition cond,
4237                                   Register r1,
4238                                   const Operand& r2,
4239                                   BranchDelaySlot bd) {
4240   Jump(stub->GetCode(), RelocInfo::CODE_TARGET, cond, r1, r2, bd);
4241 }
4242
4243
4244 bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
4245   return has_frame_ || !stub->SometimesSetsUpAFrame();
4246 }
4247
4248
4249 void MacroAssembler::IndexFromHash(Register hash, Register index) {
4250   // If the hash field contains an array index pick it out. The assert checks
4251   // that the constants for the maximum number of digits for an array index
4252   // cached in the hash field and the number of bits reserved for it does not
4253   // conflict.
4254   DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
4255          (1 << String::kArrayIndexValueBits));
4256   DecodeFieldToSmi<String::ArrayIndexValueBits>(index, hash);
4257 }
4258
4259
4260 void MacroAssembler::ObjectToDoubleFPURegister(Register object,
4261                                                FPURegister result,
4262                                                Register scratch1,
4263                                                Register scratch2,
4264                                                Register heap_number_map,
4265                                                Label* not_number,
4266                                                ObjectToDoubleFlags flags) {
4267   Label done;
4268   if ((flags & OBJECT_NOT_SMI) == 0) {
4269     Label not_smi;
4270     JumpIfNotSmi(object, &not_smi);
4271     // Remove smi tag and convert to double.
4272     // dsra(scratch1, object, kSmiTagSize);
4273     dsra32(scratch1, object, 0);
4274     mtc1(scratch1, result);
4275     cvt_d_w(result, result);
4276     Branch(&done);
4277     bind(&not_smi);
4278   }
4279   // Check for heap number and load double value from it.
4280   ld(scratch1, FieldMemOperand(object, HeapObject::kMapOffset));
4281   Branch(not_number, ne, scratch1, Operand(heap_number_map));
4282
4283   if ((flags & AVOID_NANS_AND_INFINITIES) != 0) {
4284     // If exponent is all ones the number is either a NaN or +/-Infinity.
4285     Register exponent = scratch1;
4286     Register mask_reg = scratch2;
4287     lwu(exponent, FieldMemOperand(object, HeapNumber::kExponentOffset));
4288     li(mask_reg, HeapNumber::kExponentMask);
4289
4290     And(exponent, exponent, mask_reg);
4291     Branch(not_number, eq, exponent, Operand(mask_reg));
4292   }
4293   ldc1(result, FieldMemOperand(object, HeapNumber::kValueOffset));
4294   bind(&done);
4295 }
4296
4297
4298 void MacroAssembler::SmiToDoubleFPURegister(Register smi,
4299                                             FPURegister value,
4300                                             Register scratch1) {
4301   // dsra(scratch1, smi, kSmiTagSize);
4302   dsra32(scratch1, smi, 0);
4303   mtc1(scratch1, value);
4304   cvt_d_w(value, value);
4305 }
4306
4307
4308 void MacroAssembler::AdduAndCheckForOverflow(Register dst, Register left,
4309                                              const Operand& right,
4310                                              Register overflow_dst,
4311                                              Register scratch) {
4312   if (right.is_reg()) {
4313     AdduAndCheckForOverflow(dst, left, right.rm(), overflow_dst, scratch);
4314   } else {
4315     if (dst.is(left)) {
4316       mov(scratch, left);                    // Preserve left.
4317       daddiu(dst, left, right.immediate());  // Left is overwritten.
4318       xor_(scratch, dst, scratch);           // Original left.
4319       // Load right since xori takes uint16 as immediate.
4320       daddiu(t9, zero_reg, right.immediate());
4321       xor_(overflow_dst, dst, t9);
4322       and_(overflow_dst, overflow_dst, scratch);
4323     } else {
4324       daddiu(dst, left, right.immediate());
4325       xor_(overflow_dst, dst, left);
4326       // Load right since xori takes uint16 as immediate.
4327       daddiu(t9, zero_reg, right.immediate());
4328       xor_(scratch, dst, t9);
4329       and_(overflow_dst, scratch, overflow_dst);
4330     }
4331   }
4332 }
4333
4334
4335 void MacroAssembler::AdduAndCheckForOverflow(Register dst,
4336                                              Register left,
4337                                              Register right,
4338                                              Register overflow_dst,
4339                                              Register scratch) {
4340   DCHECK(!dst.is(overflow_dst));
4341   DCHECK(!dst.is(scratch));
4342   DCHECK(!overflow_dst.is(scratch));
4343   DCHECK(!overflow_dst.is(left));
4344   DCHECK(!overflow_dst.is(right));
4345
4346   if (left.is(right) && dst.is(left)) {
4347     DCHECK(!dst.is(t9));
4348     DCHECK(!scratch.is(t9));
4349     DCHECK(!left.is(t9));
4350     DCHECK(!right.is(t9));
4351     DCHECK(!overflow_dst.is(t9));
4352     mov(t9, right);
4353     right = t9;
4354   }
4355
4356   if (dst.is(left)) {
4357     mov(scratch, left);  // Preserve left.
4358     daddu(dst, left, right);  // Left is overwritten.
4359     xor_(scratch, dst, scratch);  // Original left.
4360     xor_(overflow_dst, dst, right);
4361     and_(overflow_dst, overflow_dst, scratch);
4362   } else if (dst.is(right)) {
4363     mov(scratch, right);  // Preserve right.
4364     daddu(dst, left, right);  // Right is overwritten.
4365     xor_(scratch, dst, scratch);  // Original right.
4366     xor_(overflow_dst, dst, left);
4367     and_(overflow_dst, overflow_dst, scratch);
4368   } else {
4369     daddu(dst, left, right);
4370     xor_(overflow_dst, dst, left);
4371     xor_(scratch, dst, right);
4372     and_(overflow_dst, scratch, overflow_dst);
4373   }
4374 }
4375
4376
4377 void MacroAssembler::SubuAndCheckForOverflow(Register dst, Register left,
4378                                              const Operand& right,
4379                                              Register overflow_dst,
4380                                              Register scratch) {
4381   if (right.is_reg()) {
4382     SubuAndCheckForOverflow(dst, left, right.rm(), overflow_dst, scratch);
4383   } else {
4384     if (dst.is(left)) {
4385       mov(scratch, left);                       // Preserve left.
4386       daddiu(dst, left, -(right.immediate()));  // Left is overwritten.
4387       xor_(overflow_dst, dst, scratch);         // scratch is original left.
4388       // Load right since xori takes uint16 as immediate.
4389       daddiu(t9, zero_reg, right.immediate());
4390       xor_(scratch, scratch, t9);  // scratch is original left.
4391       and_(overflow_dst, scratch, overflow_dst);
4392     } else {
4393       daddiu(dst, left, -(right.immediate()));
4394       xor_(overflow_dst, dst, left);
4395       // Load right since xori takes uint16 as immediate.
4396       daddiu(t9, zero_reg, right.immediate());
4397       xor_(scratch, left, t9);
4398       and_(overflow_dst, scratch, overflow_dst);
4399     }
4400   }
4401 }
4402
4403
4404 void MacroAssembler::SubuAndCheckForOverflow(Register dst,
4405                                              Register left,
4406                                              Register right,
4407                                              Register overflow_dst,
4408                                              Register scratch) {
4409   DCHECK(!dst.is(overflow_dst));
4410   DCHECK(!dst.is(scratch));
4411   DCHECK(!overflow_dst.is(scratch));
4412   DCHECK(!overflow_dst.is(left));
4413   DCHECK(!overflow_dst.is(right));
4414   DCHECK(!scratch.is(left));
4415   DCHECK(!scratch.is(right));
4416
4417   // This happens with some crankshaft code. Since Subu works fine if
4418   // left == right, let's not make that restriction here.
4419   if (left.is(right)) {
4420     mov(dst, zero_reg);
4421     mov(overflow_dst, zero_reg);
4422     return;
4423   }
4424
4425   if (dst.is(left)) {
4426     mov(scratch, left);  // Preserve left.
4427     dsubu(dst, left, right);  // Left is overwritten.
4428     xor_(overflow_dst, dst, scratch);  // scratch is original left.
4429     xor_(scratch, scratch, right);  // scratch is original left.
4430     and_(overflow_dst, scratch, overflow_dst);
4431   } else if (dst.is(right)) {
4432     mov(scratch, right);  // Preserve right.
4433     dsubu(dst, left, right);  // Right is overwritten.
4434     xor_(overflow_dst, dst, left);
4435     xor_(scratch, left, scratch);  // Original right.
4436     and_(overflow_dst, scratch, overflow_dst);
4437   } else {
4438     dsubu(dst, left, right);
4439     xor_(overflow_dst, dst, left);
4440     xor_(scratch, left, right);
4441     and_(overflow_dst, scratch, overflow_dst);
4442   }
4443 }
4444
4445
4446 void MacroAssembler::CallRuntime(const Runtime::Function* f,
4447                                  int num_arguments,
4448                                  SaveFPRegsMode save_doubles) {
4449   // All parameters are on the stack. v0 has the return value after call.
4450
4451   // If the expected number of arguments of the runtime function is
4452   // constant, we check that the actual number of arguments match the
4453   // expectation.
4454   CHECK(f->nargs < 0 || f->nargs == num_arguments);
4455
4456   // TODO(1236192): Most runtime routines don't need the number of
4457   // arguments passed in because it is constant. At some point we
4458   // should remove this need and make the runtime routine entry code
4459   // smarter.
4460   PrepareCEntryArgs(num_arguments);
4461   PrepareCEntryFunction(ExternalReference(f, isolate()));
4462   CEntryStub stub(isolate(), 1, save_doubles);
4463   CallStub(&stub);
4464 }
4465
4466
4467 void MacroAssembler::CallExternalReference(const ExternalReference& ext,
4468                                            int num_arguments,
4469                                            BranchDelaySlot bd) {
4470   PrepareCEntryArgs(num_arguments);
4471   PrepareCEntryFunction(ext);
4472
4473   CEntryStub stub(isolate(), 1);
4474   CallStub(&stub, TypeFeedbackId::None(), al, zero_reg, Operand(zero_reg), bd);
4475 }
4476
4477
4478 void MacroAssembler::TailCallExternalReference(const ExternalReference& ext,
4479                                                int num_arguments,
4480                                                int result_size) {
4481   // TODO(1236192): Most runtime routines don't need the number of
4482   // arguments passed in because it is constant. At some point we
4483   // should remove this need and make the runtime routine entry code
4484   // smarter.
4485   PrepareCEntryArgs(num_arguments);
4486   JumpToExternalReference(ext);
4487 }
4488
4489
4490 void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid,
4491                                      int num_arguments,
4492                                      int result_size) {
4493   TailCallExternalReference(ExternalReference(fid, isolate()),
4494                             num_arguments,
4495                             result_size);
4496 }
4497
4498
4499 void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin,
4500                                              BranchDelaySlot bd) {
4501   PrepareCEntryFunction(builtin);
4502   CEntryStub stub(isolate(), 1);
4503   Jump(stub.GetCode(),
4504        RelocInfo::CODE_TARGET,
4505        al,
4506        zero_reg,
4507        Operand(zero_reg),
4508        bd);
4509 }
4510
4511
4512 void MacroAssembler::InvokeBuiltin(Builtins::JavaScript id,
4513                                    InvokeFlag flag,
4514                                    const CallWrapper& call_wrapper) {
4515   // You can't call a builtin without a valid frame.
4516   DCHECK(flag == JUMP_FUNCTION || has_frame());
4517
4518   GetBuiltinEntry(t9, id);
4519   if (flag == CALL_FUNCTION) {
4520     call_wrapper.BeforeCall(CallSize(t9));
4521     Call(t9);
4522     call_wrapper.AfterCall();
4523   } else {
4524     DCHECK(flag == JUMP_FUNCTION);
4525     Jump(t9);
4526   }
4527 }
4528
4529
4530 void MacroAssembler::GetBuiltinFunction(Register target,
4531                                         Builtins::JavaScript id) {
4532   // Load the builtins object into target register.
4533   ld(target, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4534   ld(target, FieldMemOperand(target, GlobalObject::kBuiltinsOffset));
4535   // Load the JavaScript builtin function from the builtins object.
4536   ld(target, FieldMemOperand(target,
4537                           JSBuiltinsObject::OffsetOfFunctionWithId(id)));
4538 }
4539
4540
4541 void MacroAssembler::GetBuiltinEntry(Register target, Builtins::JavaScript id) {
4542   DCHECK(!target.is(a1));
4543   GetBuiltinFunction(a1, id);
4544   // Load the code entry point from the builtins object.
4545   ld(target, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
4546 }
4547
4548
4549 void MacroAssembler::SetCounter(StatsCounter* counter, int value,
4550                                 Register scratch1, Register scratch2) {
4551   if (FLAG_native_code_counters && counter->Enabled()) {
4552     li(scratch1, Operand(value));
4553     li(scratch2, Operand(ExternalReference(counter)));
4554     sd(scratch1, MemOperand(scratch2));
4555   }
4556 }
4557
4558
4559 void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
4560                                       Register scratch1, Register scratch2) {
4561   DCHECK(value > 0);
4562   if (FLAG_native_code_counters && counter->Enabled()) {
4563     li(scratch2, Operand(ExternalReference(counter)));
4564     ld(scratch1, MemOperand(scratch2));
4565     Daddu(scratch1, scratch1, Operand(value));
4566     sd(scratch1, MemOperand(scratch2));
4567   }
4568 }
4569
4570
4571 void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
4572                                       Register scratch1, Register scratch2) {
4573   DCHECK(value > 0);
4574   if (FLAG_native_code_counters && counter->Enabled()) {
4575     li(scratch2, Operand(ExternalReference(counter)));
4576     ld(scratch1, MemOperand(scratch2));
4577     Dsubu(scratch1, scratch1, Operand(value));
4578     sd(scratch1, MemOperand(scratch2));
4579   }
4580 }
4581
4582
4583 // -----------------------------------------------------------------------------
4584 // Debugging.
4585
4586 void MacroAssembler::Assert(Condition cc, BailoutReason reason,
4587                             Register rs, Operand rt) {
4588   if (emit_debug_code())
4589     Check(cc, reason, rs, rt);
4590 }
4591
4592
4593 void MacroAssembler::AssertFastElements(Register elements) {
4594   if (emit_debug_code()) {
4595     DCHECK(!elements.is(at));
4596     Label ok;
4597     push(elements);
4598     ld(elements, FieldMemOperand(elements, HeapObject::kMapOffset));
4599     LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4600     Branch(&ok, eq, elements, Operand(at));
4601     LoadRoot(at, Heap::kFixedDoubleArrayMapRootIndex);
4602     Branch(&ok, eq, elements, Operand(at));
4603     LoadRoot(at, Heap::kFixedCOWArrayMapRootIndex);
4604     Branch(&ok, eq, elements, Operand(at));
4605     Abort(kJSObjectWithFastElementsMapHasSlowElements);
4606     bind(&ok);
4607     pop(elements);
4608   }
4609 }
4610
4611
4612 void MacroAssembler::Check(Condition cc, BailoutReason reason,
4613                            Register rs, Operand rt) {
4614   Label L;
4615   Branch(&L, cc, rs, rt);
4616   Abort(reason);
4617   // Will not return here.
4618   bind(&L);
4619 }
4620
4621
4622 void MacroAssembler::Abort(BailoutReason reason) {
4623   Label abort_start;
4624   bind(&abort_start);
4625 #ifdef DEBUG
4626   const char* msg = GetBailoutReason(reason);
4627   if (msg != NULL) {
4628     RecordComment("Abort message: ");
4629     RecordComment(msg);
4630   }
4631
4632   if (FLAG_trap_on_abort) {
4633     stop(msg);
4634     return;
4635   }
4636 #endif
4637
4638   li(a0, Operand(Smi::FromInt(reason)));
4639   push(a0);
4640   // Disable stub call restrictions to always allow calls to abort.
4641   if (!has_frame_) {
4642     // We don't actually want to generate a pile of code for this, so just
4643     // claim there is a stack frame, without generating one.
4644     FrameScope scope(this, StackFrame::NONE);
4645     CallRuntime(Runtime::kAbort, 1);
4646   } else {
4647     CallRuntime(Runtime::kAbort, 1);
4648   }
4649   // Will not return here.
4650   if (is_trampoline_pool_blocked()) {
4651     // If the calling code cares about the exact number of
4652     // instructions generated, we insert padding here to keep the size
4653     // of the Abort macro constant.
4654     // Currently in debug mode with debug_code enabled the number of
4655     // generated instructions is 10, so we use this as a maximum value.
4656     static const int kExpectedAbortInstructions = 10;
4657     int abort_instructions = InstructionsGeneratedSince(&abort_start);
4658     DCHECK(abort_instructions <= kExpectedAbortInstructions);
4659     while (abort_instructions++ < kExpectedAbortInstructions) {
4660       nop();
4661     }
4662   }
4663 }
4664
4665
4666 void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
4667   if (context_chain_length > 0) {
4668     // Move up the chain of contexts to the context containing the slot.
4669     ld(dst, MemOperand(cp, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4670     for (int i = 1; i < context_chain_length; i++) {
4671       ld(dst, MemOperand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4672     }
4673   } else {
4674     // Slot is in the current function context.  Move it into the
4675     // destination register in case we store into it (the write barrier
4676     // cannot be allowed to destroy the context in esi).
4677     Move(dst, cp);
4678   }
4679 }
4680
4681
4682 void MacroAssembler::LoadTransitionedArrayMapConditional(
4683     ElementsKind expected_kind,
4684     ElementsKind transitioned_kind,
4685     Register map_in_out,
4686     Register scratch,
4687     Label* no_map_match) {
4688   // Load the global or builtins object from the current context.
4689   ld(scratch,
4690      MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4691   ld(scratch, FieldMemOperand(scratch, GlobalObject::kNativeContextOffset));
4692
4693   // Check that the function's map is the same as the expected cached map.
4694   ld(scratch,
4695      MemOperand(scratch,
4696                 Context::SlotOffset(Context::JS_ARRAY_MAPS_INDEX)));
4697   size_t offset = expected_kind * kPointerSize +
4698       FixedArrayBase::kHeaderSize;
4699   ld(at, FieldMemOperand(scratch, offset));
4700   Branch(no_map_match, ne, map_in_out, Operand(at));
4701
4702   // Use the transitioned cached map.
4703   offset = transitioned_kind * kPointerSize +
4704       FixedArrayBase::kHeaderSize;
4705   ld(map_in_out, FieldMemOperand(scratch, offset));
4706 }
4707
4708
4709 void MacroAssembler::LoadGlobalFunction(int index, Register function) {
4710   // Load the global or builtins object from the current context.
4711   ld(function,
4712      MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
4713   // Load the native context from the global or builtins object.
4714   ld(function, FieldMemOperand(function,
4715                                GlobalObject::kNativeContextOffset));
4716   // Load the function from the native context.
4717   ld(function, MemOperand(function, Context::SlotOffset(index)));
4718 }
4719
4720
4721 void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
4722                                                   Register map,
4723                                                   Register scratch) {
4724   // Load the initial map. The global functions all have initial maps.
4725   ld(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
4726   if (emit_debug_code()) {
4727     Label ok, fail;
4728     CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
4729     Branch(&ok);
4730     bind(&fail);
4731     Abort(kGlobalFunctionsMustHaveInitialMap);
4732     bind(&ok);
4733   }
4734 }
4735
4736
4737 void MacroAssembler::StubPrologue() {
4738     Push(ra, fp, cp);
4739     Push(Smi::FromInt(StackFrame::STUB));
4740     // Adjust FP to point to saved FP.
4741     Daddu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
4742 }
4743
4744
4745 void MacroAssembler::Prologue(bool code_pre_aging) {
4746   PredictableCodeSizeScope predictible_code_size_scope(
4747       this, kNoCodeAgeSequenceLength);
4748   // The following three instructions must remain together and unmodified
4749   // for code aging to work properly.
4750   if (code_pre_aging) {
4751     // Pre-age the code.
4752     Code* stub = Code::GetPreAgedCodeAgeStub(isolate());
4753     nop(Assembler::CODE_AGE_MARKER_NOP);
4754     // Load the stub address to t9 and call it,
4755     // GetCodeAgeAndParity() extracts the stub address from this instruction.
4756     li(t9,
4757        Operand(reinterpret_cast<uint64_t>(stub->instruction_start())),
4758        ADDRESS_LOAD);
4759     nop();  // Prevent jalr to jal optimization.
4760     jalr(t9, a0);
4761     nop();  // Branch delay slot nop.
4762     nop();  // Pad the empty space.
4763   } else {
4764     Push(ra, fp, cp, a1);
4765     nop(Assembler::CODE_AGE_SEQUENCE_NOP);
4766     nop(Assembler::CODE_AGE_SEQUENCE_NOP);
4767     nop(Assembler::CODE_AGE_SEQUENCE_NOP);
4768     // Adjust fp to point to caller's fp.
4769     Daddu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
4770   }
4771 }
4772
4773
4774 void MacroAssembler::EnterFrame(StackFrame::Type type,
4775                                 bool load_constant_pool_pointer_reg) {
4776   // Out-of-line constant pool not implemented on mips64.
4777   UNREACHABLE();
4778 }
4779
4780
4781 void MacroAssembler::EnterFrame(StackFrame::Type type) {
4782   daddiu(sp, sp, -5 * kPointerSize);
4783   li(t8, Operand(Smi::FromInt(type)));
4784   li(t9, Operand(CodeObject()), CONSTANT_SIZE);
4785   sd(ra, MemOperand(sp, 4 * kPointerSize));
4786   sd(fp, MemOperand(sp, 3 * kPointerSize));
4787   sd(cp, MemOperand(sp, 2 * kPointerSize));
4788   sd(t8, MemOperand(sp, 1 * kPointerSize));
4789   sd(t9, MemOperand(sp, 0 * kPointerSize));
4790   // Adjust FP to point to saved FP.
4791   Daddu(fp, sp,
4792        Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
4793 }
4794
4795
4796 void MacroAssembler::LeaveFrame(StackFrame::Type type) {
4797   mov(sp, fp);
4798   ld(fp, MemOperand(sp, 0 * kPointerSize));
4799   ld(ra, MemOperand(sp, 1 * kPointerSize));
4800   daddiu(sp, sp, 2 * kPointerSize);
4801 }
4802
4803
4804 void MacroAssembler::EnterExitFrame(bool save_doubles,
4805                                     int stack_space) {
4806   // Set up the frame structure on the stack.
4807   STATIC_ASSERT(2 * kPointerSize == ExitFrameConstants::kCallerSPDisplacement);
4808   STATIC_ASSERT(1 * kPointerSize == ExitFrameConstants::kCallerPCOffset);
4809   STATIC_ASSERT(0 * kPointerSize == ExitFrameConstants::kCallerFPOffset);
4810
4811   // This is how the stack will look:
4812   // fp + 2 (==kCallerSPDisplacement) - old stack's end
4813   // [fp + 1 (==kCallerPCOffset)] - saved old ra
4814   // [fp + 0 (==kCallerFPOffset)] - saved old fp
4815   // [fp - 1 (==kSPOffset)] - sp of the called function
4816   // [fp - 2 (==kCodeOffset)] - CodeObject
4817   // fp - (2 + stack_space + alignment) == sp == [fp - kSPOffset] - top of the
4818   //   new stack (will contain saved ra)
4819
4820   // Save registers.
4821   daddiu(sp, sp, -4 * kPointerSize);
4822   sd(ra, MemOperand(sp, 3 * kPointerSize));
4823   sd(fp, MemOperand(sp, 2 * kPointerSize));
4824   daddiu(fp, sp, 2 * kPointerSize);  // Set up new frame pointer.
4825
4826   if (emit_debug_code()) {
4827     sd(zero_reg, MemOperand(fp, ExitFrameConstants::kSPOffset));
4828   }
4829
4830   // Accessed from ExitFrame::code_slot.
4831   li(t8, Operand(CodeObject()), CONSTANT_SIZE);
4832   sd(t8, MemOperand(fp, ExitFrameConstants::kCodeOffset));
4833
4834   // Save the frame pointer and the context in top.
4835   li(t8, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
4836   sd(fp, MemOperand(t8));
4837   li(t8, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
4838   sd(cp, MemOperand(t8));
4839
4840   const int frame_alignment = MacroAssembler::ActivationFrameAlignment();
4841   if (save_doubles) {
4842     // The stack is already aligned to 0 modulo 8 for stores with sdc1.
4843     int kNumOfSavedRegisters = FPURegister::kMaxNumRegisters / 2;
4844     int space = kNumOfSavedRegisters * kDoubleSize ;
4845     Dsubu(sp, sp, Operand(space));
4846     // Remember: we only need to save every 2nd double FPU value.
4847     for (int i = 0; i < kNumOfSavedRegisters; i++) {
4848       FPURegister reg = FPURegister::from_code(2 * i);
4849       sdc1(reg, MemOperand(sp, i * kDoubleSize));
4850     }
4851   }
4852
4853   // Reserve place for the return address, stack space and an optional slot
4854   // (used by the DirectCEntryStub to hold the return value if a struct is
4855   // returned) and align the frame preparing for calling the runtime function.
4856   DCHECK(stack_space >= 0);
4857   Dsubu(sp, sp, Operand((stack_space + 2) * kPointerSize));
4858   if (frame_alignment > 0) {
4859     DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4860     And(sp, sp, Operand(-frame_alignment));  // Align stack.
4861   }
4862
4863   // Set the exit frame sp value to point just before the return address
4864   // location.
4865   daddiu(at, sp, kPointerSize);
4866   sd(at, MemOperand(fp, ExitFrameConstants::kSPOffset));
4867 }
4868
4869
4870 void MacroAssembler::LeaveExitFrame(bool save_doubles, Register argument_count,
4871                                     bool restore_context, bool do_return,
4872                                     bool argument_count_is_length) {
4873   // Optionally restore all double registers.
4874   if (save_doubles) {
4875     // Remember: we only need to restore every 2nd double FPU value.
4876     int kNumOfSavedRegisters = FPURegister::kMaxNumRegisters / 2;
4877     Dsubu(t8, fp, Operand(ExitFrameConstants::kFrameSize +
4878         kNumOfSavedRegisters * kDoubleSize));
4879     for (int i = 0; i < kNumOfSavedRegisters; i++) {
4880       FPURegister reg = FPURegister::from_code(2 * i);
4881       ldc1(reg, MemOperand(t8, i  * kDoubleSize));
4882     }
4883   }
4884
4885   // Clear top frame.
4886   li(t8, Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
4887   sd(zero_reg, MemOperand(t8));
4888
4889   // Restore current context from top and clear it in debug mode.
4890   if (restore_context) {
4891     li(t8, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
4892     ld(cp, MemOperand(t8));
4893   }
4894 #ifdef DEBUG
4895   li(t8, Operand(ExternalReference(Isolate::kContextAddress, isolate())));
4896   sd(a3, MemOperand(t8));
4897 #endif
4898
4899   // Pop the arguments, restore registers, and return.
4900   mov(sp, fp);  // Respect ABI stack constraint.
4901   ld(fp, MemOperand(sp, ExitFrameConstants::kCallerFPOffset));
4902   ld(ra, MemOperand(sp, ExitFrameConstants::kCallerPCOffset));
4903
4904   if (argument_count.is_valid()) {
4905     if (argument_count_is_length) {
4906       daddu(sp, sp, argument_count);
4907     } else {
4908       dsll(t8, argument_count, kPointerSizeLog2);
4909       daddu(sp, sp, t8);
4910     }
4911   }
4912
4913   if (do_return) {
4914     Ret(USE_DELAY_SLOT);
4915     // If returning, the instruction in the delay slot will be the addiu below.
4916   }
4917   daddiu(sp, sp, 2 * kPointerSize);
4918 }
4919
4920
4921 void MacroAssembler::InitializeNewString(Register string,
4922                                          Register length,
4923                                          Heap::RootListIndex map_index,
4924                                          Register scratch1,
4925                                          Register scratch2) {
4926   // dsll(scratch1, length, kSmiTagSize);
4927   dsll32(scratch1, length, 0);
4928   LoadRoot(scratch2, map_index);
4929   sd(scratch1, FieldMemOperand(string, String::kLengthOffset));
4930   li(scratch1, Operand(String::kEmptyHashField));
4931   sd(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
4932   sd(scratch1, FieldMemOperand(string, String::kHashFieldOffset));
4933 }
4934
4935
4936 int MacroAssembler::ActivationFrameAlignment() {
4937 #if V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
4938   // Running on the real platform. Use the alignment as mandated by the local
4939   // environment.
4940   // Note: This will break if we ever start generating snapshots on one Mips
4941   // platform for another Mips platform with a different alignment.
4942   return base::OS::ActivationFrameAlignment();
4943 #else  // V8_HOST_ARCH_MIPS
4944   // If we are using the simulator then we should always align to the expected
4945   // alignment. As the simulator is used to generate snapshots we do not know
4946   // if the target platform will need alignment, so this is controlled from a
4947   // flag.
4948   return FLAG_sim_stack_alignment;
4949 #endif  // V8_HOST_ARCH_MIPS
4950 }
4951
4952
4953 void MacroAssembler::AssertStackIsAligned() {
4954   if (emit_debug_code()) {
4955       const int frame_alignment = ActivationFrameAlignment();
4956       const int frame_alignment_mask = frame_alignment - 1;
4957
4958       if (frame_alignment > kPointerSize) {
4959         Label alignment_as_expected;
4960         DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4961         andi(at, sp, frame_alignment_mask);
4962         Branch(&alignment_as_expected, eq, at, Operand(zero_reg));
4963         // Don't use Check here, as it will call Runtime_Abort re-entering here.
4964         stop("Unexpected stack alignment");
4965         bind(&alignment_as_expected);
4966       }
4967     }
4968 }
4969
4970
4971 void MacroAssembler::JumpIfNotPowerOfTwoOrZero(
4972     Register reg,
4973     Register scratch,
4974     Label* not_power_of_two_or_zero) {
4975   Dsubu(scratch, reg, Operand(1));
4976   Branch(USE_DELAY_SLOT, not_power_of_two_or_zero, lt,
4977          scratch, Operand(zero_reg));
4978   and_(at, scratch, reg);  // In the delay slot.
4979   Branch(not_power_of_two_or_zero, ne, at, Operand(zero_reg));
4980 }
4981
4982
4983 void MacroAssembler::SmiTagCheckOverflow(Register reg, Register overflow) {
4984   DCHECK(!reg.is(overflow));
4985   mov(overflow, reg);  // Save original value.
4986   SmiTag(reg);
4987   xor_(overflow, overflow, reg);  // Overflow if (value ^ 2 * value) < 0.
4988 }
4989
4990
4991 void MacroAssembler::SmiTagCheckOverflow(Register dst,
4992                                          Register src,
4993                                          Register overflow) {
4994   if (dst.is(src)) {
4995     // Fall back to slower case.
4996     SmiTagCheckOverflow(dst, overflow);
4997   } else {
4998     DCHECK(!dst.is(src));
4999     DCHECK(!dst.is(overflow));
5000     DCHECK(!src.is(overflow));
5001     SmiTag(dst, src);
5002     xor_(overflow, dst, src);  // Overflow if (value ^ 2 * value) < 0.
5003   }
5004 }
5005
5006
5007 void MacroAssembler::SmiLoadUntag(Register dst, MemOperand src) {
5008   if (SmiValuesAre32Bits()) {
5009     lw(dst, UntagSmiMemOperand(src.rm(), src.offset()));
5010   } else {
5011     lw(dst, src);
5012     SmiUntag(dst);
5013   }
5014 }
5015
5016
5017 void MacroAssembler::SmiLoadScale(Register dst, MemOperand src, int scale) {
5018   if (SmiValuesAre32Bits()) {
5019     // TODO(plind): not clear if lw or ld faster here, need micro-benchmark.
5020     lw(dst, UntagSmiMemOperand(src.rm(), src.offset()));
5021     dsll(dst, dst, scale);
5022   } else {
5023     lw(dst, src);
5024     DCHECK(scale >= kSmiTagSize);
5025     sll(dst, dst, scale - kSmiTagSize);
5026   }
5027 }
5028
5029
5030 // Returns 2 values: the Smi and a scaled version of the int within the Smi.
5031 void MacroAssembler::SmiLoadWithScale(Register d_smi,
5032                                       Register d_scaled,
5033                                       MemOperand src,
5034                                       int scale) {
5035   if (SmiValuesAre32Bits()) {
5036     ld(d_smi, src);
5037     dsra(d_scaled, d_smi, kSmiShift - scale);
5038   } else {
5039     lw(d_smi, src);
5040     DCHECK(scale >= kSmiTagSize);
5041     sll(d_scaled, d_smi, scale - kSmiTagSize);
5042   }
5043 }
5044
5045
5046 // Returns 2 values: the untagged Smi (int32) and scaled version of that int.
5047 void MacroAssembler::SmiLoadUntagWithScale(Register d_int,
5048                                            Register d_scaled,
5049                                            MemOperand src,
5050                                            int scale) {
5051   if (SmiValuesAre32Bits()) {
5052     lw(d_int, UntagSmiMemOperand(src.rm(), src.offset()));
5053     dsll(d_scaled, d_int, scale);
5054   } else {
5055     lw(d_int, src);
5056     // Need both the int and the scaled in, so use two instructions.
5057     SmiUntag(d_int);
5058     sll(d_scaled, d_int, scale);
5059   }
5060 }
5061
5062
5063 void MacroAssembler::UntagAndJumpIfSmi(Register dst,
5064                                        Register src,
5065                                        Label* smi_case) {
5066   // DCHECK(!dst.is(src));
5067   JumpIfSmi(src, smi_case, at, USE_DELAY_SLOT);
5068   SmiUntag(dst, src);
5069 }
5070
5071
5072 void MacroAssembler::UntagAndJumpIfNotSmi(Register dst,
5073                                           Register src,
5074                                           Label* non_smi_case) {
5075   // DCHECK(!dst.is(src));
5076   JumpIfNotSmi(src, non_smi_case, at, USE_DELAY_SLOT);
5077   SmiUntag(dst, src);
5078 }
5079
5080 void MacroAssembler::JumpIfSmi(Register value,
5081                                Label* smi_label,
5082                                Register scratch,
5083                                BranchDelaySlot bd) {
5084   DCHECK_EQ(0, kSmiTag);
5085   andi(scratch, value, kSmiTagMask);
5086   Branch(bd, smi_label, eq, scratch, Operand(zero_reg));
5087 }
5088
5089 void MacroAssembler::JumpIfNotSmi(Register value,
5090                                   Label* not_smi_label,
5091                                   Register scratch,
5092                                   BranchDelaySlot bd) {
5093   DCHECK_EQ(0, kSmiTag);
5094   andi(scratch, value, kSmiTagMask);
5095   Branch(bd, not_smi_label, ne, scratch, Operand(zero_reg));
5096 }
5097
5098
5099 void MacroAssembler::JumpIfNotBothSmi(Register reg1,
5100                                       Register reg2,
5101                                       Label* on_not_both_smi) {
5102   STATIC_ASSERT(kSmiTag == 0);
5103   // TODO(plind): Find some better to fix this assert issue.
5104 #if defined(__APPLE__)
5105   DCHECK_EQ(1, kSmiTagMask);
5106 #else
5107   DCHECK_EQ((int64_t)1, kSmiTagMask);
5108 #endif
5109   or_(at, reg1, reg2);
5110   JumpIfNotSmi(at, on_not_both_smi);
5111 }
5112
5113
5114 void MacroAssembler::JumpIfEitherSmi(Register reg1,
5115                                      Register reg2,
5116                                      Label* on_either_smi) {
5117   STATIC_ASSERT(kSmiTag == 0);
5118   // TODO(plind): Find some better to fix this assert issue.
5119 #if defined(__APPLE__)
5120   DCHECK_EQ(1, kSmiTagMask);
5121 #else
5122   DCHECK_EQ((int64_t)1, kSmiTagMask);
5123 #endif
5124   // Both Smi tags must be 1 (not Smi).
5125   and_(at, reg1, reg2);
5126   JumpIfSmi(at, on_either_smi);
5127 }
5128
5129
5130 void MacroAssembler::AssertNotSmi(Register object) {
5131   if (emit_debug_code()) {
5132     STATIC_ASSERT(kSmiTag == 0);
5133     andi(at, object, kSmiTagMask);
5134     Check(ne, kOperandIsASmi, at, Operand(zero_reg));
5135   }
5136 }
5137
5138
5139 void MacroAssembler::AssertSmi(Register object) {
5140   if (emit_debug_code()) {
5141     STATIC_ASSERT(kSmiTag == 0);
5142     andi(at, object, kSmiTagMask);
5143     Check(eq, kOperandIsASmi, at, Operand(zero_reg));
5144   }
5145 }
5146
5147
5148 void MacroAssembler::AssertString(Register object) {
5149   if (emit_debug_code()) {
5150     STATIC_ASSERT(kSmiTag == 0);
5151     SmiTst(object, a4);
5152     Check(ne, kOperandIsASmiAndNotAString, a4, Operand(zero_reg));
5153     push(object);
5154     ld(object, FieldMemOperand(object, HeapObject::kMapOffset));
5155     lbu(object, FieldMemOperand(object, Map::kInstanceTypeOffset));
5156     Check(lo, kOperandIsNotAString, object, Operand(FIRST_NONSTRING_TYPE));
5157     pop(object);
5158   }
5159 }
5160
5161
5162 void MacroAssembler::AssertName(Register object) {
5163   if (emit_debug_code()) {
5164     STATIC_ASSERT(kSmiTag == 0);
5165     SmiTst(object, a4);
5166     Check(ne, kOperandIsASmiAndNotAName, a4, Operand(zero_reg));
5167     push(object);
5168     ld(object, FieldMemOperand(object, HeapObject::kMapOffset));
5169     lbu(object, FieldMemOperand(object, Map::kInstanceTypeOffset));
5170     Check(le, kOperandIsNotAName, object, Operand(LAST_NAME_TYPE));
5171     pop(object);
5172   }
5173 }
5174
5175
5176 void MacroAssembler::AssertUndefinedOrAllocationSite(Register object,
5177                                                      Register scratch) {
5178   if (emit_debug_code()) {
5179     Label done_checking;
5180     AssertNotSmi(object);
5181     LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5182     Branch(&done_checking, eq, object, Operand(scratch));
5183     push(object);
5184     ld(object, FieldMemOperand(object, HeapObject::kMapOffset));
5185     LoadRoot(scratch, Heap::kAllocationSiteMapRootIndex);
5186     Assert(eq, kExpectedUndefinedOrCell, object, Operand(scratch));
5187     pop(object);
5188     bind(&done_checking);
5189   }
5190 }
5191
5192
5193 void MacroAssembler::AssertIsRoot(Register reg, Heap::RootListIndex index) {
5194   if (emit_debug_code()) {
5195     DCHECK(!reg.is(at));
5196     LoadRoot(at, index);
5197     Check(eq, kHeapNumberMapRegisterClobbered, reg, Operand(at));
5198   }
5199 }
5200
5201
5202 void MacroAssembler::JumpIfNotHeapNumber(Register object,
5203                                          Register heap_number_map,
5204                                          Register scratch,
5205                                          Label* on_not_heap_number) {
5206   ld(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
5207   AssertIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
5208   Branch(on_not_heap_number, ne, scratch, Operand(heap_number_map));
5209 }
5210
5211
5212 void MacroAssembler::LookupNumberStringCache(Register object,
5213                                              Register result,
5214                                              Register scratch1,
5215                                              Register scratch2,
5216                                              Register scratch3,
5217                                              Label* not_found) {
5218   // Use of registers. Register result is used as a temporary.
5219   Register number_string_cache = result;
5220   Register mask = scratch3;
5221
5222   // Load the number string cache.
5223   LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
5224
5225   // Make the hash mask from the length of the number string cache. It
5226   // contains two elements (number and string) for each cache entry.
5227   ld(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
5228   // Divide length by two (length is a smi).
5229   // dsra(mask, mask, kSmiTagSize + 1);
5230   dsra32(mask, mask, 1);
5231   Daddu(mask, mask, -1);  // Make mask.
5232
5233   // Calculate the entry in the number string cache. The hash value in the
5234   // number string cache for smis is just the smi value, and the hash for
5235   // doubles is the xor of the upper and lower words. See
5236   // Heap::GetNumberStringCache.
5237   Label is_smi;
5238   Label load_result_from_cache;
5239   JumpIfSmi(object, &is_smi);
5240   CheckMap(object,
5241            scratch1,
5242            Heap::kHeapNumberMapRootIndex,
5243            not_found,
5244            DONT_DO_SMI_CHECK);
5245
5246   STATIC_ASSERT(8 == kDoubleSize);
5247   Daddu(scratch1,
5248        object,
5249        Operand(HeapNumber::kValueOffset - kHeapObjectTag));
5250   ld(scratch2, MemOperand(scratch1, kPointerSize));
5251   ld(scratch1, MemOperand(scratch1, 0));
5252   Xor(scratch1, scratch1, Operand(scratch2));
5253   And(scratch1, scratch1, Operand(mask));
5254
5255   // Calculate address of entry in string cache: each entry consists
5256   // of two pointer sized fields.
5257   dsll(scratch1, scratch1, kPointerSizeLog2 + 1);
5258   Daddu(scratch1, number_string_cache, scratch1);
5259
5260   Register probe = mask;
5261   ld(probe, FieldMemOperand(scratch1, FixedArray::kHeaderSize));
5262   JumpIfSmi(probe, not_found);
5263   ldc1(f12, FieldMemOperand(object, HeapNumber::kValueOffset));
5264   ldc1(f14, FieldMemOperand(probe, HeapNumber::kValueOffset));
5265   BranchF(&load_result_from_cache, NULL, eq, f12, f14);
5266   Branch(not_found);
5267
5268   bind(&is_smi);
5269   Register scratch = scratch1;
5270   // dsra(scratch, object, 1);   // Shift away the tag.
5271   dsra32(scratch, scratch, 0);
5272   And(scratch, mask, Operand(scratch));
5273
5274   // Calculate address of entry in string cache: each entry consists
5275   // of two pointer sized fields.
5276   dsll(scratch, scratch, kPointerSizeLog2 + 1);
5277   Daddu(scratch, number_string_cache, scratch);
5278
5279   // Check if the entry is the smi we are looking for.
5280   ld(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
5281   Branch(not_found, ne, object, Operand(probe));
5282
5283   // Get the result from the cache.
5284   bind(&load_result_from_cache);
5285   ld(result, FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
5286
5287   IncrementCounter(isolate()->counters()->number_to_string_native(),
5288                    1,
5289                    scratch1,
5290                    scratch2);
5291 }
5292
5293
5294 void MacroAssembler::JumpIfNonSmisNotBothSequentialOneByteStrings(
5295     Register first, Register second, Register scratch1, Register scratch2,
5296     Label* failure) {
5297   // Test that both first and second are sequential one-byte strings.
5298   // Assume that they are non-smis.
5299   ld(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
5300   ld(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
5301   lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
5302   lbu(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
5303
5304   JumpIfBothInstanceTypesAreNotSequentialOneByte(scratch1, scratch2, scratch1,
5305                                                  scratch2, failure);
5306 }
5307
5308
5309 void MacroAssembler::JumpIfNotBothSequentialOneByteStrings(Register first,
5310                                                            Register second,
5311                                                            Register scratch1,
5312                                                            Register scratch2,
5313                                                            Label* failure) {
5314   // Check that neither is a smi.
5315   STATIC_ASSERT(kSmiTag == 0);
5316   And(scratch1, first, Operand(second));
5317   JumpIfSmi(scratch1, failure);
5318   JumpIfNonSmisNotBothSequentialOneByteStrings(first, second, scratch1,
5319                                                scratch2, failure);
5320 }
5321
5322
5323 void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialOneByte(
5324     Register first, Register second, Register scratch1, Register scratch2,
5325     Label* failure) {
5326   const int kFlatOneByteStringMask =
5327       kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
5328   const int kFlatOneByteStringTag =
5329       kStringTag | kOneByteStringTag | kSeqStringTag;
5330   DCHECK(kFlatOneByteStringTag <= 0xffff);  // Ensure this fits 16-bit immed.
5331   andi(scratch1, first, kFlatOneByteStringMask);
5332   Branch(failure, ne, scratch1, Operand(kFlatOneByteStringTag));
5333   andi(scratch2, second, kFlatOneByteStringMask);
5334   Branch(failure, ne, scratch2, Operand(kFlatOneByteStringTag));
5335 }
5336
5337
5338 void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(Register type,
5339                                                               Register scratch,
5340                                                               Label* failure) {
5341   const int kFlatOneByteStringMask =
5342       kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
5343   const int kFlatOneByteStringTag =
5344       kStringTag | kOneByteStringTag | kSeqStringTag;
5345   And(scratch, type, Operand(kFlatOneByteStringMask));
5346   Branch(failure, ne, scratch, Operand(kFlatOneByteStringTag));
5347 }
5348
5349
5350 static const int kRegisterPassedArguments = (kMipsAbi == kN64) ? 8 : 4;
5351
5352 int MacroAssembler::CalculateStackPassedWords(int num_reg_arguments,
5353                                               int num_double_arguments) {
5354   int stack_passed_words = 0;
5355   num_reg_arguments += 2 * num_double_arguments;
5356
5357   // O32: Up to four simple arguments are passed in registers a0..a3.
5358   // N64: Up to eight simple arguments are passed in registers a0..a7.
5359   if (num_reg_arguments > kRegisterPassedArguments) {
5360     stack_passed_words += num_reg_arguments - kRegisterPassedArguments;
5361   }
5362   stack_passed_words += kCArgSlotCount;
5363   return stack_passed_words;
5364 }
5365
5366
5367 void MacroAssembler::EmitSeqStringSetCharCheck(Register string,
5368                                                Register index,
5369                                                Register value,
5370                                                Register scratch,
5371                                                uint32_t encoding_mask) {
5372   Label is_object;
5373   SmiTst(string, at);
5374   Check(ne, kNonObject, at, Operand(zero_reg));
5375
5376   ld(at, FieldMemOperand(string, HeapObject::kMapOffset));
5377   lbu(at, FieldMemOperand(at, Map::kInstanceTypeOffset));
5378
5379   andi(at, at, kStringRepresentationMask | kStringEncodingMask);
5380   li(scratch, Operand(encoding_mask));
5381   Check(eq, kUnexpectedStringType, at, Operand(scratch));
5382
5383   // TODO(plind): requires Smi size check code for mips32.
5384
5385   ld(at, FieldMemOperand(string, String::kLengthOffset));
5386   Check(lt, kIndexIsTooLarge, index, Operand(at));
5387
5388   DCHECK(Smi::FromInt(0) == 0);
5389   Check(ge, kIndexIsNegative, index, Operand(zero_reg));
5390 }
5391
5392
5393 void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
5394                                           int num_double_arguments,
5395                                           Register scratch) {
5396   int frame_alignment = ActivationFrameAlignment();
5397
5398   // n64: Up to eight simple arguments in a0..a3, a4..a7, No argument slots.
5399   // O32: Up to four simple arguments are passed in registers a0..a3.
5400   // Those four arguments must have reserved argument slots on the stack for
5401   // mips, even though those argument slots are not normally used.
5402   // Both ABIs: Remaining arguments are pushed on the stack, above (higher
5403   // address than) the (O32) argument slots. (arg slot calculation handled by
5404   // CalculateStackPassedWords()).
5405   int stack_passed_arguments = CalculateStackPassedWords(
5406       num_reg_arguments, num_double_arguments);
5407   if (frame_alignment > kPointerSize) {
5408     // Make stack end at alignment and make room for num_arguments - 4 words
5409     // and the original value of sp.
5410     mov(scratch, sp);
5411     Dsubu(sp, sp, Operand((stack_passed_arguments + 1) * kPointerSize));
5412     DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
5413     And(sp, sp, Operand(-frame_alignment));
5414     sd(scratch, MemOperand(sp, stack_passed_arguments * kPointerSize));
5415   } else {
5416     Dsubu(sp, sp, Operand(stack_passed_arguments * kPointerSize));
5417   }
5418 }
5419
5420
5421 void MacroAssembler::PrepareCallCFunction(int num_reg_arguments,
5422                                           Register scratch) {
5423   PrepareCallCFunction(num_reg_arguments, 0, scratch);
5424 }
5425
5426
5427 void MacroAssembler::CallCFunction(ExternalReference function,
5428                                    int num_reg_arguments,
5429                                    int num_double_arguments) {
5430   li(t8, Operand(function));
5431   CallCFunctionHelper(t8, num_reg_arguments, num_double_arguments);
5432 }
5433
5434
5435 void MacroAssembler::CallCFunction(Register function,
5436                                    int num_reg_arguments,
5437                                    int num_double_arguments) {
5438   CallCFunctionHelper(function, num_reg_arguments, num_double_arguments);
5439 }
5440
5441
5442 void MacroAssembler::CallCFunction(ExternalReference function,
5443                                    int num_arguments) {
5444   CallCFunction(function, num_arguments, 0);
5445 }
5446
5447
5448 void MacroAssembler::CallCFunction(Register function,
5449                                    int num_arguments) {
5450   CallCFunction(function, num_arguments, 0);
5451 }
5452
5453
5454 void MacroAssembler::CallCFunctionHelper(Register function,
5455                                          int num_reg_arguments,
5456                                          int num_double_arguments) {
5457   DCHECK(has_frame());
5458   // Make sure that the stack is aligned before calling a C function unless
5459   // running in the simulator. The simulator has its own alignment check which
5460   // provides more information.
5461   // The argument stots are presumed to have been set up by
5462   // PrepareCallCFunction. The C function must be called via t9, for mips ABI.
5463
5464 #if V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
5465   if (emit_debug_code()) {
5466     int frame_alignment = base::OS::ActivationFrameAlignment();
5467     int frame_alignment_mask = frame_alignment - 1;
5468     if (frame_alignment > kPointerSize) {
5469       DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
5470       Label alignment_as_expected;
5471       And(at, sp, Operand(frame_alignment_mask));
5472       Branch(&alignment_as_expected, eq, at, Operand(zero_reg));
5473       // Don't use Check here, as it will call Runtime_Abort possibly
5474       // re-entering here.
5475       stop("Unexpected alignment in CallCFunction");
5476       bind(&alignment_as_expected);
5477     }
5478   }
5479 #endif  // V8_HOST_ARCH_MIPS
5480
5481   // Just call directly. The function called cannot cause a GC, or
5482   // allow preemption, so the return address in the link register
5483   // stays correct.
5484
5485   if (!function.is(t9)) {
5486     mov(t9, function);
5487     function = t9;
5488   }
5489
5490   Call(function);
5491
5492   int stack_passed_arguments = CalculateStackPassedWords(
5493       num_reg_arguments, num_double_arguments);
5494
5495   if (base::OS::ActivationFrameAlignment() > kPointerSize) {
5496     ld(sp, MemOperand(sp, stack_passed_arguments * kPointerSize));
5497   } else {
5498     Daddu(sp, sp, Operand(stack_passed_arguments * kPointerSize));
5499   }
5500 }
5501
5502
5503 #undef BRANCH_ARGS_CHECK
5504
5505
5506 void MacroAssembler::PatchRelocatedValue(Register li_location,
5507                                          Register scratch,
5508                                          Register new_value) {
5509   lwu(scratch, MemOperand(li_location));
5510   // At this point scratch is a lui(at, ...) instruction.
5511   if (emit_debug_code()) {
5512     And(scratch, scratch, kOpcodeMask);
5513     Check(eq, kTheInstructionToPatchShouldBeALui,
5514         scratch, Operand(LUI));
5515     lwu(scratch, MemOperand(li_location));
5516   }
5517   dsrl32(t9, new_value, 0);
5518   Ins(scratch, t9, 0, kImm16Bits);
5519   sw(scratch, MemOperand(li_location));
5520
5521   lwu(scratch, MemOperand(li_location, kInstrSize));
5522   // scratch is now ori(at, ...).
5523   if (emit_debug_code()) {
5524     And(scratch, scratch, kOpcodeMask);
5525     Check(eq, kTheInstructionToPatchShouldBeAnOri,
5526         scratch, Operand(ORI));
5527     lwu(scratch, MemOperand(li_location, kInstrSize));
5528   }
5529   dsrl(t9, new_value, kImm16Bits);
5530   Ins(scratch, t9, 0, kImm16Bits);
5531   sw(scratch, MemOperand(li_location, kInstrSize));
5532
5533   lwu(scratch, MemOperand(li_location, kInstrSize * 3));
5534   // scratch is now ori(at, ...).
5535   if (emit_debug_code()) {
5536     And(scratch, scratch, kOpcodeMask);
5537     Check(eq, kTheInstructionToPatchShouldBeAnOri,
5538         scratch, Operand(ORI));
5539     lwu(scratch, MemOperand(li_location, kInstrSize * 3));
5540   }
5541
5542   Ins(scratch, new_value, 0, kImm16Bits);
5543   sw(scratch, MemOperand(li_location, kInstrSize * 3));
5544
5545   // Update the I-cache so the new lui and ori can be executed.
5546   FlushICache(li_location, 4);
5547 }
5548
5549 void MacroAssembler::GetRelocatedValue(Register li_location,
5550                                        Register value,
5551                                        Register scratch) {
5552   lwu(value, MemOperand(li_location));
5553   if (emit_debug_code()) {
5554     And(value, value, kOpcodeMask);
5555     Check(eq, kTheInstructionShouldBeALui,
5556         value, Operand(LUI));
5557     lwu(value, MemOperand(li_location));
5558   }
5559
5560   // value now holds a lui instruction. Extract the immediate.
5561   andi(value, value, kImm16Mask);
5562   dsll32(value, value, kImm16Bits);
5563
5564   lwu(scratch, MemOperand(li_location, kInstrSize));
5565   if (emit_debug_code()) {
5566     And(scratch, scratch, kOpcodeMask);
5567     Check(eq, kTheInstructionShouldBeAnOri,
5568         scratch, Operand(ORI));
5569     lwu(scratch, MemOperand(li_location, kInstrSize));
5570   }
5571   // "scratch" now holds an ori instruction. Extract the immediate.
5572   andi(scratch, scratch, kImm16Mask);
5573   dsll32(scratch, scratch, 0);
5574
5575   or_(value, value, scratch);
5576
5577   lwu(scratch, MemOperand(li_location, kInstrSize * 3));
5578   if (emit_debug_code()) {
5579     And(scratch, scratch, kOpcodeMask);
5580     Check(eq, kTheInstructionShouldBeAnOri,
5581         scratch, Operand(ORI));
5582     lwu(scratch, MemOperand(li_location, kInstrSize * 3));
5583   }
5584   // "scratch" now holds an ori instruction. Extract the immediate.
5585   andi(scratch, scratch, kImm16Mask);
5586   dsll(scratch, scratch, kImm16Bits);
5587
5588   or_(value, value, scratch);
5589   // Sign extend extracted address.
5590   dsra(value, value, kImm16Bits);
5591 }
5592
5593
5594 void MacroAssembler::CheckPageFlag(
5595     Register object,
5596     Register scratch,
5597     int mask,
5598     Condition cc,
5599     Label* condition_met) {
5600   And(scratch, object, Operand(~Page::kPageAlignmentMask));
5601   ld(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
5602   And(scratch, scratch, Operand(mask));
5603   Branch(condition_met, cc, scratch, Operand(zero_reg));
5604 }
5605
5606
5607 void MacroAssembler::JumpIfBlack(Register object,
5608                                  Register scratch0,
5609                                  Register scratch1,
5610                                  Label* on_black) {
5611   HasColor(object, scratch0, scratch1, on_black, 1, 0);  // kBlackBitPattern.
5612   DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5613 }
5614
5615
5616 void MacroAssembler::HasColor(Register object,
5617                               Register bitmap_scratch,
5618                               Register mask_scratch,
5619                               Label* has_color,
5620                               int first_bit,
5621                               int second_bit) {
5622   DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, t8));
5623   DCHECK(!AreAliased(object, bitmap_scratch, mask_scratch, t9));
5624
5625   GetMarkBits(object, bitmap_scratch, mask_scratch);
5626
5627   Label other_color;
5628   // Note that we are using a 4-byte aligned 8-byte load.
5629   Uld(t9, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5630   And(t8, t9, Operand(mask_scratch));
5631   Branch(&other_color, first_bit == 1 ? eq : ne, t8, Operand(zero_reg));
5632   // Shift left 1 by adding.
5633   Daddu(mask_scratch, mask_scratch, Operand(mask_scratch));
5634   And(t8, t9, Operand(mask_scratch));
5635   Branch(has_color, second_bit == 1 ? ne : eq, t8, Operand(zero_reg));
5636
5637   bind(&other_color);
5638 }
5639
5640
5641 // Detect some, but not all, common pointer-free objects.  This is used by the
5642 // incremental write barrier which doesn't care about oddballs (they are always
5643 // marked black immediately so this code is not hit).
5644 void MacroAssembler::JumpIfDataObject(Register value,
5645                                       Register scratch,
5646                                       Label* not_data_object) {
5647   DCHECK(!AreAliased(value, scratch, t8, no_reg));
5648   Label is_data_object;
5649   ld(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5650   LoadRoot(t8, Heap::kHeapNumberMapRootIndex);
5651   Branch(&is_data_object, eq, t8, Operand(scratch));
5652   DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5653   DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
5654   // If it's a string and it's not a cons string then it's an object containing
5655   // no GC pointers.
5656   lbu(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
5657   And(t8, scratch, Operand(kIsIndirectStringMask | kIsNotStringMask));
5658   Branch(not_data_object, ne, t8, Operand(zero_reg));
5659   bind(&is_data_object);
5660 }
5661
5662
5663 void MacroAssembler::GetMarkBits(Register addr_reg,
5664                                  Register bitmap_reg,
5665                                  Register mask_reg) {
5666   DCHECK(!AreAliased(addr_reg, bitmap_reg, mask_reg, no_reg));
5667   // addr_reg is divided into fields:
5668   // |63        page base        20|19    high      8|7   shift   3|2  0|
5669   // 'high' gives the index of the cell holding color bits for the object.
5670   // 'shift' gives the offset in the cell for this object's color.
5671   And(bitmap_reg, addr_reg, Operand(~Page::kPageAlignmentMask));
5672   Ext(mask_reg, addr_reg, kPointerSizeLog2, Bitmap::kBitsPerCellLog2);
5673   const int kLowBits = kPointerSizeLog2 + Bitmap::kBitsPerCellLog2;
5674   Ext(t8, addr_reg, kLowBits, kPageSizeBits - kLowBits);
5675   dsll(t8, t8, Bitmap::kBytesPerCellLog2);
5676   Daddu(bitmap_reg, bitmap_reg, t8);
5677   li(t8, Operand(1));
5678   dsllv(mask_reg, t8, mask_reg);
5679 }
5680
5681
5682 void MacroAssembler::EnsureNotWhite(
5683     Register value,
5684     Register bitmap_scratch,
5685     Register mask_scratch,
5686     Register load_scratch,
5687     Label* value_is_white_and_not_data) {
5688   DCHECK(!AreAliased(value, bitmap_scratch, mask_scratch, t8));
5689   GetMarkBits(value, bitmap_scratch, mask_scratch);
5690
5691   // If the value is black or grey we don't need to do anything.
5692   DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
5693   DCHECK(strcmp(Marking::kBlackBitPattern, "10") == 0);
5694   DCHECK(strcmp(Marking::kGreyBitPattern, "11") == 0);
5695   DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
5696
5697   Label done;
5698
5699   // Since both black and grey have a 1 in the first position and white does
5700   // not have a 1 there we only need to check one bit.
5701   // Note that we are using a 4-byte aligned 8-byte load.
5702   Uld(load_scratch, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5703   And(t8, mask_scratch, load_scratch);
5704   Branch(&done, ne, t8, Operand(zero_reg));
5705
5706   if (emit_debug_code()) {
5707     // Check for impossible bit pattern.
5708     Label ok;
5709     // sll may overflow, making the check conservative.
5710     dsll(t8, mask_scratch, 1);
5711     And(t8, load_scratch, t8);
5712     Branch(&ok, eq, t8, Operand(zero_reg));
5713     stop("Impossible marking bit pattern");
5714     bind(&ok);
5715   }
5716
5717   // Value is white.  We check whether it is data that doesn't need scanning.
5718   // Currently only checks for HeapNumber and non-cons strings.
5719   Register map = load_scratch;  // Holds map while checking type.
5720   Register length = load_scratch;  // Holds length of object after testing type.
5721   Label is_data_object;
5722
5723   // Check for heap-number
5724   ld(map, FieldMemOperand(value, HeapObject::kMapOffset));
5725   LoadRoot(t8, Heap::kHeapNumberMapRootIndex);
5726   {
5727     Label skip;
5728     Branch(&skip, ne, t8, Operand(map));
5729     li(length, HeapNumber::kSize);
5730     Branch(&is_data_object);
5731     bind(&skip);
5732   }
5733
5734   // Check for strings.
5735   DCHECK(kIsIndirectStringTag == 1 && kIsIndirectStringMask == 1);
5736   DCHECK(kNotStringTag == 0x80 && kIsNotStringMask == 0x80);
5737   // If it's a string and it's not a cons string then it's an object containing
5738   // no GC pointers.
5739   Register instance_type = load_scratch;
5740   lbu(instance_type, FieldMemOperand(map, Map::kInstanceTypeOffset));
5741   And(t8, instance_type, Operand(kIsIndirectStringMask | kIsNotStringMask));
5742   Branch(value_is_white_and_not_data, ne, t8, Operand(zero_reg));
5743   // It's a non-indirect (non-cons and non-slice) string.
5744   // If it's external, the length is just ExternalString::kSize.
5745   // Otherwise it's String::kHeaderSize + string->length() * (1 or 2).
5746   // External strings are the only ones with the kExternalStringTag bit
5747   // set.
5748   DCHECK_EQ(0, kSeqStringTag & kExternalStringTag);
5749   DCHECK_EQ(0, kConsStringTag & kExternalStringTag);
5750   And(t8, instance_type, Operand(kExternalStringTag));
5751   {
5752     Label skip;
5753     Branch(&skip, eq, t8, Operand(zero_reg));
5754     li(length, ExternalString::kSize);
5755     Branch(&is_data_object);
5756     bind(&skip);
5757   }
5758
5759   // Sequential string, either Latin1 or UC16.
5760   // For Latin1 (char-size of 1) we shift the smi tag away to get the length.
5761   // For UC16 (char-size of 2) we just leave the smi tag in place, thereby
5762   // getting the length multiplied by 2.
5763   DCHECK(kOneByteStringTag == 4 && kStringEncodingMask == 4);
5764   DCHECK(kSmiTag == 0 && kSmiTagSize == 1);
5765   lw(t9, UntagSmiFieldMemOperand(value, String::kLengthOffset));
5766   And(t8, instance_type, Operand(kStringEncodingMask));
5767   {
5768     Label skip;
5769     Branch(&skip, ne, t8, Operand(zero_reg));
5770     // Adjust length for UC16.
5771     dsll(t9, t9, 1);
5772     bind(&skip);
5773   }
5774   Daddu(length, t9, Operand(SeqString::kHeaderSize + kObjectAlignmentMask));
5775   DCHECK(!length.is(t8));
5776   And(length, length, Operand(~kObjectAlignmentMask));
5777
5778   bind(&is_data_object);
5779   // Value is a data object, and it is white.  Mark it black.  Since we know
5780   // that the object is white we can make it black by flipping one bit.
5781   Uld(t8, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5782   Or(t8, t8, Operand(mask_scratch));
5783   Usd(t8, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
5784
5785   And(bitmap_scratch, bitmap_scratch, Operand(~Page::kPageAlignmentMask));
5786   Uld(t8, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
5787   Daddu(t8, t8, Operand(length));
5788   Usd(t8, MemOperand(bitmap_scratch, MemoryChunk::kLiveBytesOffset));
5789
5790   bind(&done);
5791 }
5792
5793
5794 void MacroAssembler::LoadInstanceDescriptors(Register map,
5795                                              Register descriptors) {
5796   ld(descriptors, FieldMemOperand(map, Map::kDescriptorsOffset));
5797 }
5798
5799
5800 void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
5801   ld(dst, FieldMemOperand(map, Map::kBitField3Offset));
5802   DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
5803 }
5804
5805
5806 void MacroAssembler::EnumLength(Register dst, Register map) {
5807   STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
5808   ld(dst, FieldMemOperand(map, Map::kBitField3Offset));
5809   And(dst, dst, Operand(Map::EnumLengthBits::kMask));
5810   SmiTag(dst);
5811 }
5812
5813
5814 void MacroAssembler::LoadAccessor(Register dst, Register holder,
5815                                   int accessor_index,
5816                                   AccessorComponent accessor) {
5817   ld(dst, FieldMemOperand(holder, HeapObject::kMapOffset));
5818   LoadInstanceDescriptors(dst, dst);
5819   ld(dst,
5820      FieldMemOperand(dst, DescriptorArray::GetValueOffset(accessor_index)));
5821   int offset = accessor == ACCESSOR_GETTER ? AccessorPair::kGetterOffset
5822                                            : AccessorPair::kSetterOffset;
5823   ld(dst, FieldMemOperand(dst, offset));
5824 }
5825
5826
5827 void MacroAssembler::CheckEnumCache(Register null_value, Label* call_runtime) {
5828   Register  empty_fixed_array_value = a6;
5829   LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
5830   Label next, start;
5831   mov(a2, a0);
5832
5833   // Check if the enum length field is properly initialized, indicating that
5834   // there is an enum cache.
5835   ld(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
5836
5837   EnumLength(a3, a1);
5838   Branch(
5839       call_runtime, eq, a3, Operand(Smi::FromInt(kInvalidEnumCacheSentinel)));
5840
5841   jmp(&start);
5842
5843   bind(&next);
5844   ld(a1, FieldMemOperand(a2, HeapObject::kMapOffset));
5845
5846   // For all objects but the receiver, check that the cache is empty.
5847   EnumLength(a3, a1);
5848   Branch(call_runtime, ne, a3, Operand(Smi::FromInt(0)));
5849
5850   bind(&start);
5851
5852   // Check that there are no elements. Register a2 contains the current JS
5853   // object we've reached through the prototype chain.
5854   Label no_elements;
5855   ld(a2, FieldMemOperand(a2, JSObject::kElementsOffset));
5856   Branch(&no_elements, eq, a2, Operand(empty_fixed_array_value));
5857
5858   // Second chance, the object may be using the empty slow element dictionary.
5859   LoadRoot(at, Heap::kEmptySlowElementDictionaryRootIndex);
5860   Branch(call_runtime, ne, a2, Operand(at));
5861
5862   bind(&no_elements);
5863   ld(a2, FieldMemOperand(a1, Map::kPrototypeOffset));
5864   Branch(&next, ne, a2, Operand(null_value));
5865 }
5866
5867
5868 void MacroAssembler::ClampUint8(Register output_reg, Register input_reg) {
5869   DCHECK(!output_reg.is(input_reg));
5870   Label done;
5871   li(output_reg, Operand(255));
5872   // Normal branch: nop in delay slot.
5873   Branch(&done, gt, input_reg, Operand(output_reg));
5874   // Use delay slot in this branch.
5875   Branch(USE_DELAY_SLOT, &done, lt, input_reg, Operand(zero_reg));
5876   mov(output_reg, zero_reg);  // In delay slot.
5877   mov(output_reg, input_reg);  // Value is in range 0..255.
5878   bind(&done);
5879 }
5880
5881
5882 void MacroAssembler::ClampDoubleToUint8(Register result_reg,
5883                                         DoubleRegister input_reg,
5884                                         DoubleRegister temp_double_reg) {
5885   Label above_zero;
5886   Label done;
5887   Label in_bounds;
5888
5889   Move(temp_double_reg, 0.0);
5890   BranchF(&above_zero, NULL, gt, input_reg, temp_double_reg);
5891
5892   // Double value is less than zero, NaN or Inf, return 0.
5893   mov(result_reg, zero_reg);
5894   Branch(&done);
5895
5896   // Double value is >= 255, return 255.
5897   bind(&above_zero);
5898   Move(temp_double_reg, 255.0);
5899   BranchF(&in_bounds, NULL, le, input_reg, temp_double_reg);
5900   li(result_reg, Operand(255));
5901   Branch(&done);
5902
5903   // In 0-255 range, round and truncate.
5904   bind(&in_bounds);
5905   cvt_w_d(temp_double_reg, input_reg);
5906   mfc1(result_reg, temp_double_reg);
5907   bind(&done);
5908 }
5909
5910
5911 void MacroAssembler::TestJSArrayForAllocationMemento(
5912     Register receiver_reg,
5913     Register scratch_reg,
5914     Label* no_memento_found,
5915     Condition cond,
5916     Label* allocation_memento_present) {
5917   ExternalReference new_space_start =
5918       ExternalReference::new_space_start(isolate());
5919   ExternalReference new_space_allocation_top =
5920       ExternalReference::new_space_allocation_top_address(isolate());
5921   Daddu(scratch_reg, receiver_reg,
5922        Operand(JSArray::kSize + AllocationMemento::kSize - kHeapObjectTag));
5923   Branch(no_memento_found, lt, scratch_reg, Operand(new_space_start));
5924   li(at, Operand(new_space_allocation_top));
5925   ld(at, MemOperand(at));
5926   Branch(no_memento_found, gt, scratch_reg, Operand(at));
5927   ld(scratch_reg, MemOperand(scratch_reg, -AllocationMemento::kSize));
5928   if (allocation_memento_present) {
5929     Branch(allocation_memento_present, cond, scratch_reg,
5930            Operand(isolate()->factory()->allocation_memento_map()));
5931   }
5932 }
5933
5934
5935 Register GetRegisterThatIsNotOneOf(Register reg1,
5936                                    Register reg2,
5937                                    Register reg3,
5938                                    Register reg4,
5939                                    Register reg5,
5940                                    Register reg6) {
5941   RegList regs = 0;
5942   if (reg1.is_valid()) regs |= reg1.bit();
5943   if (reg2.is_valid()) regs |= reg2.bit();
5944   if (reg3.is_valid()) regs |= reg3.bit();
5945   if (reg4.is_valid()) regs |= reg4.bit();
5946   if (reg5.is_valid()) regs |= reg5.bit();
5947   if (reg6.is_valid()) regs |= reg6.bit();
5948
5949   for (int i = 0; i < Register::NumAllocatableRegisters(); i++) {
5950     Register candidate = Register::FromAllocationIndex(i);
5951     if (regs & candidate.bit()) continue;
5952     return candidate;
5953   }
5954   UNREACHABLE();
5955   return no_reg;
5956 }
5957
5958
5959 void MacroAssembler::JumpIfDictionaryInPrototypeChain(
5960     Register object,
5961     Register scratch0,
5962     Register scratch1,
5963     Label* found) {
5964   DCHECK(!scratch1.is(scratch0));
5965   Factory* factory = isolate()->factory();
5966   Register current = scratch0;
5967   Label loop_again;
5968
5969   // Scratch contained elements pointer.
5970   Move(current, object);
5971
5972   // Loop based on the map going up the prototype chain.
5973   bind(&loop_again);
5974   ld(current, FieldMemOperand(current, HeapObject::kMapOffset));
5975   lb(scratch1, FieldMemOperand(current, Map::kBitField2Offset));
5976   DecodeField<Map::ElementsKindBits>(scratch1);
5977   Branch(found, eq, scratch1, Operand(DICTIONARY_ELEMENTS));
5978   ld(current, FieldMemOperand(current, Map::kPrototypeOffset));
5979   Branch(&loop_again, ne, current, Operand(factory->null_value()));
5980 }
5981
5982
5983 bool AreAliased(Register reg1,
5984                 Register reg2,
5985                 Register reg3,
5986                 Register reg4,
5987                 Register reg5,
5988                 Register reg6,
5989                 Register reg7,
5990                 Register reg8) {
5991   int n_of_valid_regs = reg1.is_valid() + reg2.is_valid() +
5992       reg3.is_valid() + reg4.is_valid() + reg5.is_valid() + reg6.is_valid() +
5993       reg7.is_valid() + reg8.is_valid();
5994
5995   RegList regs = 0;
5996   if (reg1.is_valid()) regs |= reg1.bit();
5997   if (reg2.is_valid()) regs |= reg2.bit();
5998   if (reg3.is_valid()) regs |= reg3.bit();
5999   if (reg4.is_valid()) regs |= reg4.bit();
6000   if (reg5.is_valid()) regs |= reg5.bit();
6001   if (reg6.is_valid()) regs |= reg6.bit();
6002   if (reg7.is_valid()) regs |= reg7.bit();
6003   if (reg8.is_valid()) regs |= reg8.bit();
6004   int n_of_non_aliasing_regs = NumRegs(regs);
6005
6006   return n_of_valid_regs != n_of_non_aliasing_regs;
6007 }
6008
6009
6010 CodePatcher::CodePatcher(byte* address,
6011                          int instructions,
6012                          FlushICache flush_cache)
6013     : address_(address),
6014       size_(instructions * Assembler::kInstrSize),
6015       masm_(NULL, address, size_ + Assembler::kGap),
6016       flush_cache_(flush_cache) {
6017   // Create a new macro assembler pointing to the address of the code to patch.
6018   // The size is adjusted with kGap on order for the assembler to generate size
6019   // bytes of instructions without failing with buffer size constraints.
6020   DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
6021 }
6022
6023
6024 CodePatcher::~CodePatcher() {
6025   // Indicate that code has changed.
6026   if (flush_cache_ == FLUSH) {
6027     CpuFeatures::FlushICache(address_, size_);
6028   }
6029   // Check that the code was patched as expected.
6030   DCHECK(masm_.pc_ == address_ + size_);
6031   DCHECK(masm_.reloc_info_writer.pos() == address_ + size_ + Assembler::kGap);
6032 }
6033
6034
6035 void CodePatcher::Emit(Instr instr) {
6036   masm()->emit(instr);
6037 }
6038
6039
6040 void CodePatcher::Emit(Address addr) {
6041   // masm()->emit(reinterpret_cast<Instr>(addr));
6042 }
6043
6044
6045 void CodePatcher::ChangeBranchCondition(Condition cond) {
6046   Instr instr = Assembler::instr_at(masm_.pc_);
6047   DCHECK(Assembler::IsBranch(instr));
6048   uint32_t opcode = Assembler::GetOpcodeField(instr);
6049   // Currently only the 'eq' and 'ne' cond values are supported and the simple
6050   // branch instructions (with opcode being the branch type).
6051   // There are some special cases (see Assembler::IsBranch()) so extending this
6052   // would be tricky.
6053   DCHECK(opcode == BEQ ||
6054          opcode == BNE ||
6055         opcode == BLEZ ||
6056         opcode == BGTZ ||
6057         opcode == BEQL ||
6058         opcode == BNEL ||
6059        opcode == BLEZL ||
6060        opcode == BGTZL);
6061   opcode = (cond == eq) ? BEQ : BNE;
6062   instr = (instr & ~kOpcodeMask) | opcode;
6063   masm_.emit(instr);
6064 }
6065
6066
6067 void MacroAssembler::TruncatingDiv(Register result,
6068                                    Register dividend,
6069                                    int32_t divisor) {
6070   DCHECK(!dividend.is(result));
6071   DCHECK(!dividend.is(at));
6072   DCHECK(!result.is(at));
6073   base::MagicNumbersForDivision<uint32_t> mag =
6074   base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
6075   li(at, Operand(static_cast<int32_t>(mag.multiplier)));
6076   Mulh(result, dividend, Operand(at));
6077   bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
6078   if (divisor > 0 && neg) {
6079     Addu(result, result, Operand(dividend));
6080   }
6081   if (divisor < 0 && !neg && mag.multiplier > 0) {
6082     Subu(result, result, Operand(dividend));
6083   }
6084   if (mag.shift > 0) sra(result, result, mag.shift);
6085   srl(at, dividend, 31);
6086   Addu(result, result, Operand(at));
6087 }
6088
6089
6090 } }  // namespace v8::internal
6091
6092 #endif  // V8_TARGET_ARCH_MIPS64