02845e2bbd9be0ecf82ab4265c9581aa8302a446
[platform/upstream/nodejs.git] / deps / v8 / src / mips / macro-assembler-mips.h
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 #ifndef V8_MIPS_MACRO_ASSEMBLER_MIPS_H_
6 #define V8_MIPS_MACRO_ASSEMBLER_MIPS_H_
7
8 #include "src/assembler.h"
9 #include "src/globals.h"
10 #include "src/mips/assembler-mips.h"
11
12 namespace v8 {
13 namespace internal {
14
15 // Forward declaration.
16 class JumpTarget;
17
18 // Reserved Register Usage Summary.
19 //
20 // Registers t8, t9, and at are reserved for use by the MacroAssembler.
21 //
22 // The programmer should know that the MacroAssembler may clobber these three,
23 // but won't touch other registers except in special cases.
24 //
25 // Per the MIPS ABI, register t9 must be used for indirect function call
26 // via 'jalr t9' or 'jr t9' instructions. This is relied upon by gcc when
27 // trying to update gp register for position-independent-code. Whenever
28 // MIPS generated code calls C code, it must be via t9 register.
29
30
31 // Flags used for LeaveExitFrame function.
32 enum LeaveExitFrameMode {
33   EMIT_RETURN = true,
34   NO_EMIT_RETURN = false
35 };
36
37 // Flags used for AllocateHeapNumber
38 enum TaggingMode {
39   // Tag the result.
40   TAG_RESULT,
41   // Don't tag
42   DONT_TAG_RESULT
43 };
44
45 // Flags used for the ObjectToDoubleFPURegister function.
46 enum ObjectToDoubleFlags {
47   // No special flags.
48   NO_OBJECT_TO_DOUBLE_FLAGS = 0,
49   // Object is known to be a non smi.
50   OBJECT_NOT_SMI = 1 << 0,
51   // Don't load NaNs or infinities, branch to the non number case instead.
52   AVOID_NANS_AND_INFINITIES = 1 << 1
53 };
54
55 // Allow programmer to use Branch Delay Slot of Branches, Jumps, Calls.
56 enum BranchDelaySlot {
57   USE_DELAY_SLOT,
58   PROTECT
59 };
60
61 // Flags used for the li macro-assembler function.
62 enum LiFlags {
63   // If the constant value can be represented in just 16 bits, then
64   // optimize the li to use a single instruction, rather than lui/ori pair.
65   OPTIMIZE_SIZE = 0,
66   // Always use 2 instructions (lui/ori pair), even if the constant could
67   // be loaded with just one, so that this value is patchable later.
68   CONSTANT_SIZE = 1
69 };
70
71
72 enum RememberedSetAction { EMIT_REMEMBERED_SET, OMIT_REMEMBERED_SET };
73 enum SmiCheck { INLINE_SMI_CHECK, OMIT_SMI_CHECK };
74 enum PointersToHereCheck {
75   kPointersToHereMaybeInteresting,
76   kPointersToHereAreAlwaysInteresting
77 };
78 enum RAStatus { kRAHasNotBeenSaved, kRAHasBeenSaved };
79
80 Register GetRegisterThatIsNotOneOf(Register reg1,
81                                    Register reg2 = no_reg,
82                                    Register reg3 = no_reg,
83                                    Register reg4 = no_reg,
84                                    Register reg5 = no_reg,
85                                    Register reg6 = no_reg);
86
87 bool AreAliased(Register reg1,
88                 Register reg2,
89                 Register reg3 = no_reg,
90                 Register reg4 = no_reg,
91                 Register reg5 = no_reg,
92                 Register reg6 = no_reg,
93                 Register reg7 = no_reg,
94                 Register reg8 = no_reg);
95
96
97 // -----------------------------------------------------------------------------
98 // Static helper functions.
99
100 inline MemOperand ContextOperand(Register context, int index) {
101   return MemOperand(context, Context::SlotOffset(index));
102 }
103
104
105 inline MemOperand GlobalObjectOperand()  {
106   return ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX);
107 }
108
109
110 // Generate a MemOperand for loading a field from an object.
111 inline MemOperand FieldMemOperand(Register object, int offset) {
112   return MemOperand(object, offset - kHeapObjectTag);
113 }
114
115
116 // Generate a MemOperand for storing arguments 5..N on the stack
117 // when calling CallCFunction().
118 inline MemOperand CFunctionArgumentOperand(int index) {
119   DCHECK(index > kCArgSlotCount);
120   // Argument 5 takes the slot just past the four Arg-slots.
121   int offset = (index - 5) * kPointerSize + kCArgsSlotsSize;
122   return MemOperand(sp, offset);
123 }
124
125
126 // MacroAssembler implements a collection of frequently used macros.
127 class MacroAssembler: public Assembler {
128  public:
129   // The isolate parameter can be NULL if the macro assembler should
130   // not use isolate-dependent functionality. In this case, it's the
131   // responsibility of the caller to never invoke such function on the
132   // macro assembler.
133   MacroAssembler(Isolate* isolate, void* buffer, int size);
134
135   // Arguments macros.
136 #define COND_TYPED_ARGS Condition cond, Register r1, const Operand& r2
137 #define COND_ARGS cond, r1, r2
138
139   // Cases when relocation is not needed.
140 #define DECLARE_NORELOC_PROTOTYPE(Name, target_type) \
141   void Name(target_type target, BranchDelaySlot bd = PROTECT); \
142   inline void Name(BranchDelaySlot bd, target_type target) { \
143     Name(target, bd); \
144   } \
145   void Name(target_type target, \
146             COND_TYPED_ARGS, \
147             BranchDelaySlot bd = PROTECT); \
148   inline void Name(BranchDelaySlot bd, \
149                    target_type target, \
150                    COND_TYPED_ARGS) { \
151     Name(target, COND_ARGS, bd); \
152   }
153
154 #define DECLARE_BRANCH_PROTOTYPES(Name) \
155   DECLARE_NORELOC_PROTOTYPE(Name, Label*) \
156   DECLARE_NORELOC_PROTOTYPE(Name, int16_t)
157
158   DECLARE_BRANCH_PROTOTYPES(Branch)
159   DECLARE_BRANCH_PROTOTYPES(BranchAndLink)
160   DECLARE_BRANCH_PROTOTYPES(BranchShort)
161
162 #undef DECLARE_BRANCH_PROTOTYPES
163 #undef COND_TYPED_ARGS
164 #undef COND_ARGS
165
166
167   // Jump, Call, and Ret pseudo instructions implementing inter-working.
168 #define COND_ARGS Condition cond = al, Register rs = zero_reg, \
169   const Operand& rt = Operand(zero_reg), BranchDelaySlot bd = PROTECT
170
171   void Jump(Register target, COND_ARGS);
172   void Jump(intptr_t target, RelocInfo::Mode rmode, COND_ARGS);
173   void Jump(Address target, RelocInfo::Mode rmode, COND_ARGS);
174   void Jump(Handle<Code> code, RelocInfo::Mode rmode, COND_ARGS);
175   static int CallSize(Register target, COND_ARGS);
176   void Call(Register target, COND_ARGS);
177   static int CallSize(Address target, RelocInfo::Mode rmode, COND_ARGS);
178   void Call(Address target, RelocInfo::Mode rmode, COND_ARGS);
179   int CallSize(Handle<Code> code,
180                RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
181                TypeFeedbackId ast_id = TypeFeedbackId::None(),
182                COND_ARGS);
183   void Call(Handle<Code> code,
184             RelocInfo::Mode rmode = RelocInfo::CODE_TARGET,
185             TypeFeedbackId ast_id = TypeFeedbackId::None(),
186             COND_ARGS);
187   void Ret(COND_ARGS);
188   inline void Ret(BranchDelaySlot bd, Condition cond = al,
189     Register rs = zero_reg, const Operand& rt = Operand(zero_reg)) {
190     Ret(cond, rs, rt, bd);
191   }
192
193   void Branch(Label* L,
194               Condition cond,
195               Register rs,
196               Heap::RootListIndex index,
197               BranchDelaySlot bdslot = PROTECT);
198
199 #undef COND_ARGS
200
201   // Emit code to discard a non-negative number of pointer-sized elements
202   // from the stack, clobbering only the sp register.
203   void Drop(int count,
204             Condition cond = cc_always,
205             Register reg = no_reg,
206             const Operand& op = Operand(no_reg));
207
208   // Trivial case of DropAndRet that utilizes the delay slot and only emits
209   // 2 instructions.
210   void DropAndRet(int drop);
211
212   void DropAndRet(int drop,
213                   Condition cond,
214                   Register reg,
215                   const Operand& op);
216
217   // Swap two registers.  If the scratch register is omitted then a slightly
218   // less efficient form using xor instead of mov is emitted.
219   void Swap(Register reg1, Register reg2, Register scratch = no_reg);
220
221   void Call(Label* target);
222
223   inline void Move(Register dst, Register src) {
224     if (!dst.is(src)) {
225       mov(dst, src);
226     }
227   }
228
229   inline void Move(FPURegister dst, FPURegister src) {
230     if (!dst.is(src)) {
231       mov_d(dst, src);
232     }
233   }
234
235   inline void Move(Register dst_low, Register dst_high, FPURegister src) {
236     mfc1(dst_low, src);
237     Mfhc1(dst_high, src);
238   }
239
240   inline void FmoveHigh(Register dst_high, FPURegister src) {
241     Mfhc1(dst_high, src);
242   }
243
244   inline void FmoveLow(Register dst_low, FPURegister src) {
245     mfc1(dst_low, src);
246   }
247
248   inline void Move(FPURegister dst, Register src_low, Register src_high) {
249     mtc1(src_low, dst);
250     Mthc1(src_high, dst);
251   }
252
253   void Move(FPURegister dst, float imm);
254   void Move(FPURegister dst, double imm);
255
256   // Conditional move.
257   void Movz(Register rd, Register rs, Register rt);
258   void Movn(Register rd, Register rs, Register rt);
259   void Movt(Register rd, Register rs, uint16_t cc = 0);
260   void Movf(Register rd, Register rs, uint16_t cc = 0);
261
262   void Clz(Register rd, Register rs);
263
264   // Jump unconditionally to given label.
265   // We NEED a nop in the branch delay slot, as it used by v8, for example in
266   // CodeGenerator::ProcessDeferred().
267   // Currently the branch delay slot is filled by the MacroAssembler.
268   // Use rather b(Label) for code generation.
269   void jmp(Label* L) {
270     Branch(L);
271   }
272
273   void Load(Register dst, const MemOperand& src, Representation r);
274   void Store(Register src, const MemOperand& dst, Representation r);
275
276   // Load an object from the root table.
277   void LoadRoot(Register destination,
278                 Heap::RootListIndex index);
279   void LoadRoot(Register destination,
280                 Heap::RootListIndex index,
281                 Condition cond, Register src1, const Operand& src2);
282
283   // Store an object to the root table.
284   void StoreRoot(Register source,
285                  Heap::RootListIndex index);
286   void StoreRoot(Register source,
287                  Heap::RootListIndex index,
288                  Condition cond, Register src1, const Operand& src2);
289
290   // ---------------------------------------------------------------------------
291   // GC Support
292
293   void IncrementalMarkingRecordWriteHelper(Register object,
294                                            Register value,
295                                            Register address);
296
297   enum RememberedSetFinalAction {
298     kReturnAtEnd,
299     kFallThroughAtEnd
300   };
301
302
303   // Record in the remembered set the fact that we have a pointer to new space
304   // at the address pointed to by the addr register.  Only works if addr is not
305   // in new space.
306   void RememberedSetHelper(Register object,  // Used for debug code.
307                            Register addr,
308                            Register scratch,
309                            SaveFPRegsMode save_fp,
310                            RememberedSetFinalAction and_then);
311
312   void CheckPageFlag(Register object,
313                      Register scratch,
314                      int mask,
315                      Condition cc,
316                      Label* condition_met);
317
318   // Check if object is in new space.  Jumps if the object is not in new space.
319   // The register scratch can be object itself, but it will be clobbered.
320   void JumpIfNotInNewSpace(Register object,
321                            Register scratch,
322                            Label* branch) {
323     InNewSpace(object, scratch, ne, branch);
324   }
325
326   // Check if object is in new space.  Jumps if the object is in new space.
327   // The register scratch can be object itself, but scratch will be clobbered.
328   void JumpIfInNewSpace(Register object,
329                         Register scratch,
330                         Label* branch) {
331     InNewSpace(object, scratch, eq, branch);
332   }
333
334   // Check if an object has a given incremental marking color.
335   void HasColor(Register object,
336                 Register scratch0,
337                 Register scratch1,
338                 Label* has_color,
339                 int first_bit,
340                 int second_bit);
341
342   void JumpIfBlack(Register object,
343                    Register scratch0,
344                    Register scratch1,
345                    Label* on_black);
346
347   // Checks the color of an object.  If the object is already grey or black
348   // then we just fall through, since it is already live.  If it is white and
349   // we can determine that it doesn't need to be scanned, then we just mark it
350   // black and fall through.  For the rest we jump to the label so the
351   // incremental marker can fix its assumptions.
352   void EnsureNotWhite(Register object,
353                       Register scratch1,
354                       Register scratch2,
355                       Register scratch3,
356                       Label* object_is_white_and_not_data);
357
358   // Detects conservatively whether an object is data-only, i.e. it does need to
359   // be scanned by the garbage collector.
360   void JumpIfDataObject(Register value,
361                         Register scratch,
362                         Label* not_data_object);
363
364   // Notify the garbage collector that we wrote a pointer into an object.
365   // |object| is the object being stored into, |value| is the object being
366   // stored.  value and scratch registers are clobbered by the operation.
367   // The offset is the offset from the start of the object, not the offset from
368   // the tagged HeapObject pointer.  For use with FieldOperand(reg, off).
369   void RecordWriteField(
370       Register object,
371       int offset,
372       Register value,
373       Register scratch,
374       RAStatus ra_status,
375       SaveFPRegsMode save_fp,
376       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
377       SmiCheck smi_check = INLINE_SMI_CHECK,
378       PointersToHereCheck pointers_to_here_check_for_value =
379           kPointersToHereMaybeInteresting);
380
381   // As above, but the offset has the tag presubtracted.  For use with
382   // MemOperand(reg, off).
383   inline void RecordWriteContextSlot(
384       Register context,
385       int offset,
386       Register value,
387       Register scratch,
388       RAStatus ra_status,
389       SaveFPRegsMode save_fp,
390       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
391       SmiCheck smi_check = INLINE_SMI_CHECK,
392       PointersToHereCheck pointers_to_here_check_for_value =
393           kPointersToHereMaybeInteresting) {
394     RecordWriteField(context,
395                      offset + kHeapObjectTag,
396                      value,
397                      scratch,
398                      ra_status,
399                      save_fp,
400                      remembered_set_action,
401                      smi_check,
402                      pointers_to_here_check_for_value);
403   }
404
405   void RecordWriteForMap(
406       Register object,
407       Register map,
408       Register dst,
409       RAStatus ra_status,
410       SaveFPRegsMode save_fp);
411
412   // For a given |object| notify the garbage collector that the slot |address|
413   // has been written.  |value| is the object being stored. The value and
414   // address registers are clobbered by the operation.
415   void RecordWrite(
416       Register object,
417       Register address,
418       Register value,
419       RAStatus ra_status,
420       SaveFPRegsMode save_fp,
421       RememberedSetAction remembered_set_action = EMIT_REMEMBERED_SET,
422       SmiCheck smi_check = INLINE_SMI_CHECK,
423       PointersToHereCheck pointers_to_here_check_for_value =
424           kPointersToHereMaybeInteresting);
425
426
427   // ---------------------------------------------------------------------------
428   // Inline caching support.
429
430   // Generate code for checking access rights - used for security checks
431   // on access to global objects across environments. The holder register
432   // is left untouched, whereas both scratch registers are clobbered.
433   void CheckAccessGlobalProxy(Register holder_reg,
434                               Register scratch,
435                               Label* miss);
436
437   void GetNumberHash(Register reg0, Register scratch);
438
439   void LoadFromNumberDictionary(Label* miss,
440                                 Register elements,
441                                 Register key,
442                                 Register result,
443                                 Register reg0,
444                                 Register reg1,
445                                 Register reg2);
446
447
448   inline void MarkCode(NopMarkerTypes type) {
449     nop(type);
450   }
451
452   // Check if the given instruction is a 'type' marker.
453   // i.e. check if it is a sll zero_reg, zero_reg, <type> (referenced as
454   // nop(type)). These instructions are generated to mark special location in
455   // the code, like some special IC code.
456   static inline bool IsMarkedCode(Instr instr, int type) {
457     DCHECK((FIRST_IC_MARKER <= type) && (type < LAST_CODE_MARKER));
458     return IsNop(instr, type);
459   }
460
461
462   static inline int GetCodeMarker(Instr instr) {
463     uint32_t opcode = ((instr & kOpcodeMask));
464     uint32_t rt = ((instr & kRtFieldMask) >> kRtShift);
465     uint32_t rs = ((instr & kRsFieldMask) >> kRsShift);
466     uint32_t sa = ((instr & kSaFieldMask) >> kSaShift);
467
468     // Return <n> if we have a sll zero_reg, zero_reg, n
469     // else return -1.
470     bool sllzz = (opcode == SLL &&
471                   rt == static_cast<uint32_t>(ToNumber(zero_reg)) &&
472                   rs == static_cast<uint32_t>(ToNumber(zero_reg)));
473     int type =
474         (sllzz && FIRST_IC_MARKER <= sa && sa < LAST_CODE_MARKER) ? sa : -1;
475     DCHECK((type == -1) ||
476            ((FIRST_IC_MARKER <= type) && (type < LAST_CODE_MARKER)));
477     return type;
478   }
479
480
481
482   // ---------------------------------------------------------------------------
483   // Allocation support.
484
485   // Allocate an object in new space or old pointer space. The object_size is
486   // specified either in bytes or in words if the allocation flag SIZE_IN_WORDS
487   // is passed. If the space is exhausted control continues at the gc_required
488   // label. The allocated object is returned in result. If the flag
489   // tag_allocated_object is true the result is tagged as as a heap object.
490   // All registers are clobbered also when control continues at the gc_required
491   // label.
492   void Allocate(int object_size,
493                 Register result,
494                 Register scratch1,
495                 Register scratch2,
496                 Label* gc_required,
497                 AllocationFlags flags);
498
499   void Allocate(Register object_size,
500                 Register result,
501                 Register scratch1,
502                 Register scratch2,
503                 Label* gc_required,
504                 AllocationFlags flags);
505
506   // Undo allocation in new space. The object passed and objects allocated after
507   // it will no longer be allocated. The caller must make sure that no pointers
508   // are left to the object(s) no longer allocated as they would be invalid when
509   // allocation is undone.
510   void UndoAllocationInNewSpace(Register object, Register scratch);
511
512
513   void AllocateTwoByteString(Register result,
514                              Register length,
515                              Register scratch1,
516                              Register scratch2,
517                              Register scratch3,
518                              Label* gc_required);
519   void AllocateOneByteString(Register result, Register length,
520                              Register scratch1, Register scratch2,
521                              Register scratch3, Label* gc_required);
522   void AllocateTwoByteConsString(Register result,
523                                  Register length,
524                                  Register scratch1,
525                                  Register scratch2,
526                                  Label* gc_required);
527   void AllocateOneByteConsString(Register result, Register length,
528                                  Register scratch1, Register scratch2,
529                                  Label* gc_required);
530   void AllocateTwoByteSlicedString(Register result,
531                                    Register length,
532                                    Register scratch1,
533                                    Register scratch2,
534                                    Label* gc_required);
535   void AllocateOneByteSlicedString(Register result, Register length,
536                                    Register scratch1, Register scratch2,
537                                    Label* gc_required);
538
539   // Allocates a heap number or jumps to the gc_required label if the young
540   // space is full and a scavenge is needed. All registers are clobbered also
541   // when control continues at the gc_required label.
542   void AllocateHeapNumber(Register result,
543                           Register scratch1,
544                           Register scratch2,
545                           Register heap_number_map,
546                           Label* gc_required,
547                           TaggingMode tagging_mode = TAG_RESULT,
548                           MutableMode mode = IMMUTABLE);
549   void AllocateHeapNumberWithValue(Register result,
550                                    FPURegister value,
551                                    Register scratch1,
552                                    Register scratch2,
553                                    Label* gc_required);
554
555   // ---------------------------------------------------------------------------
556   // Instruction macros.
557
558 #define DEFINE_INSTRUCTION(instr)                                              \
559   void instr(Register rd, Register rs, const Operand& rt);                     \
560   void instr(Register rd, Register rs, Register rt) {                          \
561     instr(rd, rs, Operand(rt));                                                \
562   }                                                                            \
563   void instr(Register rs, Register rt, int32_t j) {                            \
564     instr(rs, rt, Operand(j));                                                 \
565   }
566
567 #define DEFINE_INSTRUCTION2(instr)                                             \
568   void instr(Register rs, const Operand& rt);                                  \
569   void instr(Register rs, Register rt) {                                       \
570     instr(rs, Operand(rt));                                                    \
571   }                                                                            \
572   void instr(Register rs, int32_t j) {                                         \
573     instr(rs, Operand(j));                                                     \
574   }
575
576 #define DEFINE_INSTRUCTION3(instr)                                             \
577   void instr(Register rd_hi, Register rd_lo, Register rs, const Operand& rt);  \
578   void instr(Register rd_hi, Register rd_lo, Register rs, Register rt) {       \
579     instr(rd_hi, rd_lo, rs, Operand(rt));                                      \
580   }                                                                            \
581   void instr(Register rd_hi, Register rd_lo, Register rs, int32_t j) {         \
582     instr(rd_hi, rd_lo, rs, Operand(j));                                       \
583   }
584
585   DEFINE_INSTRUCTION(Addu);
586   DEFINE_INSTRUCTION(Subu);
587   DEFINE_INSTRUCTION(Mul);
588   DEFINE_INSTRUCTION(Div);
589   DEFINE_INSTRUCTION(Divu);
590   DEFINE_INSTRUCTION(Mod);
591   DEFINE_INSTRUCTION(Modu);
592   DEFINE_INSTRUCTION(Mulh);
593   DEFINE_INSTRUCTION2(Mult);
594   DEFINE_INSTRUCTION(Mulhu);
595   DEFINE_INSTRUCTION2(Multu);
596   DEFINE_INSTRUCTION2(Div);
597   DEFINE_INSTRUCTION2(Divu);
598
599   DEFINE_INSTRUCTION3(Div);
600   DEFINE_INSTRUCTION3(Mul);
601
602   DEFINE_INSTRUCTION(And);
603   DEFINE_INSTRUCTION(Or);
604   DEFINE_INSTRUCTION(Xor);
605   DEFINE_INSTRUCTION(Nor);
606   DEFINE_INSTRUCTION2(Neg);
607
608   DEFINE_INSTRUCTION(Slt);
609   DEFINE_INSTRUCTION(Sltu);
610
611   // MIPS32 R2 instruction macro.
612   DEFINE_INSTRUCTION(Ror);
613
614 #undef DEFINE_INSTRUCTION
615 #undef DEFINE_INSTRUCTION2
616
617   void Pref(int32_t hint, const MemOperand& rs);
618
619
620   // ---------------------------------------------------------------------------
621   // Pseudo-instructions.
622
623   void mov(Register rd, Register rt) { or_(rd, rt, zero_reg); }
624
625   void Ulw(Register rd, const MemOperand& rs);
626   void Usw(Register rd, const MemOperand& rs);
627
628   // Load int32 in the rd register.
629   void li(Register rd, Operand j, LiFlags mode = OPTIMIZE_SIZE);
630   inline void li(Register rd, int32_t j, LiFlags mode = OPTIMIZE_SIZE) {
631     li(rd, Operand(j), mode);
632   }
633   void li(Register dst, Handle<Object> value, LiFlags mode = OPTIMIZE_SIZE);
634
635   // Push multiple registers on the stack.
636   // Registers are saved in numerical order, with higher numbered registers
637   // saved in higher memory addresses.
638   void MultiPush(RegList regs);
639   void MultiPushReversed(RegList regs);
640
641   void MultiPushFPU(RegList regs);
642   void MultiPushReversedFPU(RegList regs);
643
644   void push(Register src) {
645     Addu(sp, sp, Operand(-kPointerSize));
646     sw(src, MemOperand(sp, 0));
647   }
648   void Push(Register src) { push(src); }
649
650   // Push a handle.
651   void Push(Handle<Object> handle);
652   void Push(Smi* smi) { Push(Handle<Smi>(smi, isolate())); }
653
654   // Push two registers. Pushes leftmost register first (to highest address).
655   void Push(Register src1, Register src2) {
656     Subu(sp, sp, Operand(2 * kPointerSize));
657     sw(src1, MemOperand(sp, 1 * kPointerSize));
658     sw(src2, MemOperand(sp, 0 * kPointerSize));
659   }
660
661   // Push three registers. Pushes leftmost register first (to highest address).
662   void Push(Register src1, Register src2, Register src3) {
663     Subu(sp, sp, Operand(3 * kPointerSize));
664     sw(src1, MemOperand(sp, 2 * kPointerSize));
665     sw(src2, MemOperand(sp, 1 * kPointerSize));
666     sw(src3, MemOperand(sp, 0 * kPointerSize));
667   }
668
669   // Push four registers. Pushes leftmost register first (to highest address).
670   void Push(Register src1, Register src2, Register src3, Register src4) {
671     Subu(sp, sp, Operand(4 * kPointerSize));
672     sw(src1, MemOperand(sp, 3 * kPointerSize));
673     sw(src2, MemOperand(sp, 2 * kPointerSize));
674     sw(src3, MemOperand(sp, 1 * kPointerSize));
675     sw(src4, MemOperand(sp, 0 * kPointerSize));
676   }
677
678   void Push(Register src, Condition cond, Register tst1, Register tst2) {
679     // Since we don't have conditional execution we use a Branch.
680     Branch(3, cond, tst1, Operand(tst2));
681     Subu(sp, sp, Operand(kPointerSize));
682     sw(src, MemOperand(sp, 0));
683   }
684
685   // Pops multiple values from the stack and load them in the
686   // registers specified in regs. Pop order is the opposite as in MultiPush.
687   void MultiPop(RegList regs);
688   void MultiPopReversed(RegList regs);
689
690   void MultiPopFPU(RegList regs);
691   void MultiPopReversedFPU(RegList regs);
692
693   void pop(Register dst) {
694     lw(dst, MemOperand(sp, 0));
695     Addu(sp, sp, Operand(kPointerSize));
696   }
697   void Pop(Register dst) { pop(dst); }
698
699   // Pop two registers. Pops rightmost register first (from lower address).
700   void Pop(Register src1, Register src2) {
701     DCHECK(!src1.is(src2));
702     lw(src2, MemOperand(sp, 0 * kPointerSize));
703     lw(src1, MemOperand(sp, 1 * kPointerSize));
704     Addu(sp, sp, 2 * kPointerSize);
705   }
706
707   // Pop three registers. Pops rightmost register first (from lower address).
708   void Pop(Register src1, Register src2, Register src3) {
709     lw(src3, MemOperand(sp, 0 * kPointerSize));
710     lw(src2, MemOperand(sp, 1 * kPointerSize));
711     lw(src1, MemOperand(sp, 2 * kPointerSize));
712     Addu(sp, sp, 3 * kPointerSize);
713   }
714
715   void Pop(uint32_t count = 1) {
716     Addu(sp, sp, Operand(count * kPointerSize));
717   }
718
719   // Push and pop the registers that can hold pointers, as defined by the
720   // RegList constant kSafepointSavedRegisters.
721   void PushSafepointRegisters();
722   void PopSafepointRegisters();
723   // Store value in register src in the safepoint stack slot for
724   // register dst.
725   void StoreToSafepointRegisterSlot(Register src, Register dst);
726   // Load the value of the src register from its safepoint stack slot
727   // into register dst.
728   void LoadFromSafepointRegisterSlot(Register dst, Register src);
729
730   // Flush the I-cache from asm code. You should use CpuFeatures::FlushICache
731   // from C.
732   // Does not handle errors.
733   void FlushICache(Register address, unsigned instructions);
734
735   // MIPS32 R2 instruction macro.
736   void Ins(Register rt, Register rs, uint16_t pos, uint16_t size);
737   void Ext(Register rt, Register rs, uint16_t pos, uint16_t size);
738
739   // ---------------------------------------------------------------------------
740   // FPU macros. These do not handle special cases like NaN or +- inf.
741
742   // Convert unsigned word to double.
743   void Cvt_d_uw(FPURegister fd, FPURegister fs, FPURegister scratch);
744   void Cvt_d_uw(FPURegister fd, Register rs, FPURegister scratch);
745
746   // Convert double to unsigned word.
747   void Trunc_uw_d(FPURegister fd, FPURegister fs, FPURegister scratch);
748   void Trunc_uw_d(FPURegister fd, Register rs, FPURegister scratch);
749
750   void Trunc_w_d(FPURegister fd, FPURegister fs);
751   void Round_w_d(FPURegister fd, FPURegister fs);
752   void Floor_w_d(FPURegister fd, FPURegister fs);
753   void Ceil_w_d(FPURegister fd, FPURegister fs);
754
755   // FP32 mode: Move the general purpose register into
756   // the high part of the double-register pair.
757   // FP64 mode: Move the general-purpose register into
758   // the higher 32 bits of the 64-bit coprocessor register,
759   // while leaving the low bits unchanged.
760   void Mthc1(Register rt, FPURegister fs);
761
762   // FP32 mode: move the high part of the double-register pair into
763   // general purpose register.
764   // FP64 mode: Move the higher 32 bits of the 64-bit coprocessor register into
765   // general-purpose register.
766   void Mfhc1(Register rt, FPURegister fs);
767
768   // Wrapper function for the different cmp/branch types.
769   void BranchF(Label* target,
770                Label* nan,
771                Condition cc,
772                FPURegister cmp1,
773                FPURegister cmp2,
774                BranchDelaySlot bd = PROTECT);
775
776   // Alternate (inline) version for better readability with USE_DELAY_SLOT.
777   inline void BranchF(BranchDelaySlot bd,
778                       Label* target,
779                       Label* nan,
780                       Condition cc,
781                       FPURegister cmp1,
782                       FPURegister cmp2) {
783     BranchF(target, nan, cc, cmp1, cmp2, bd);
784   }
785
786   // Truncates a double using a specific rounding mode, and writes the value
787   // to the result register.
788   // The except_flag will contain any exceptions caused by the instruction.
789   // If check_inexact is kDontCheckForInexactConversion, then the inexact
790   // exception is masked.
791   void EmitFPUTruncate(FPURoundingMode rounding_mode,
792                        Register result,
793                        DoubleRegister double_input,
794                        Register scratch,
795                        DoubleRegister double_scratch,
796                        Register except_flag,
797                        CheckForInexactConversion check_inexact
798                            = kDontCheckForInexactConversion);
799
800   // Performs a truncating conversion of a floating point number as used by
801   // the JS bitwise operations. See ECMA-262 9.5: ToInt32. Goes to 'done' if it
802   // succeeds, otherwise falls through if result is saturated. On return
803   // 'result' either holds answer, or is clobbered on fall through.
804   //
805   // Only public for the test code in test-code-stubs-arm.cc.
806   void TryInlineTruncateDoubleToI(Register result,
807                                   DoubleRegister input,
808                                   Label* done);
809
810   // Performs a truncating conversion of a floating point number as used by
811   // the JS bitwise operations. See ECMA-262 9.5: ToInt32.
812   // Exits with 'result' holding the answer.
813   void TruncateDoubleToI(Register result, DoubleRegister double_input);
814
815   // Performs a truncating conversion of a heap number as used by
816   // the JS bitwise operations. See ECMA-262 9.5: ToInt32. 'result' and 'input'
817   // must be different registers. Exits with 'result' holding the answer.
818   void TruncateHeapNumberToI(Register result, Register object);
819
820   // Converts the smi or heap number in object to an int32 using the rules
821   // for ToInt32 as described in ECMAScript 9.5.: the value is truncated
822   // and brought into the range -2^31 .. +2^31 - 1. 'result' and 'input' must be
823   // different registers.
824   void TruncateNumberToI(Register object,
825                          Register result,
826                          Register heap_number_map,
827                          Register scratch,
828                          Label* not_int32);
829
830   // Loads the number from object into dst register.
831   // If |object| is neither smi nor heap number, |not_number| is jumped to
832   // with |object| still intact.
833   void LoadNumber(Register object,
834                   FPURegister dst,
835                   Register heap_number_map,
836                   Register scratch,
837                   Label* not_number);
838
839   // Loads the number from object into double_dst in the double format.
840   // Control will jump to not_int32 if the value cannot be exactly represented
841   // by a 32-bit integer.
842   // Floating point value in the 32-bit integer range that are not exact integer
843   // won't be loaded.
844   void LoadNumberAsInt32Double(Register object,
845                                DoubleRegister double_dst,
846                                Register heap_number_map,
847                                Register scratch1,
848                                Register scratch2,
849                                FPURegister double_scratch,
850                                Label* not_int32);
851
852   // Loads the number from object into dst as a 32-bit integer.
853   // Control will jump to not_int32 if the object cannot be exactly represented
854   // by a 32-bit integer.
855   // Floating point value in the 32-bit integer range that are not exact integer
856   // won't be converted.
857   void LoadNumberAsInt32(Register object,
858                          Register dst,
859                          Register heap_number_map,
860                          Register scratch1,
861                          Register scratch2,
862                          FPURegister double_scratch0,
863                          FPURegister double_scratch1,
864                          Label* not_int32);
865
866   // Enter exit frame.
867   // argc - argument count to be dropped by LeaveExitFrame.
868   // save_doubles - saves FPU registers on stack, currently disabled.
869   // stack_space - extra stack space.
870   void EnterExitFrame(bool save_doubles,
871                       int stack_space = 0);
872
873   // Leave the current exit frame.
874   void LeaveExitFrame(bool save_doubles, Register arg_count,
875                       bool restore_context, bool do_return = NO_EMIT_RETURN,
876                       bool argument_count_is_length = false);
877
878   // Get the actual activation frame alignment for target environment.
879   static int ActivationFrameAlignment();
880
881   // Make sure the stack is aligned. Only emits code in debug mode.
882   void AssertStackIsAligned();
883
884   void LoadContext(Register dst, int context_chain_length);
885
886   // Conditionally load the cached Array transitioned map of type
887   // transitioned_kind from the native context if the map in register
888   // map_in_out is the cached Array map in the native context of
889   // expected_kind.
890   void LoadTransitionedArrayMapConditional(
891       ElementsKind expected_kind,
892       ElementsKind transitioned_kind,
893       Register map_in_out,
894       Register scratch,
895       Label* no_map_match);
896
897   void LoadGlobalFunction(int index, Register function);
898
899   // Load the initial map from the global function. The registers
900   // function and map can be the same, function is then overwritten.
901   void LoadGlobalFunctionInitialMap(Register function,
902                                     Register map,
903                                     Register scratch);
904
905   void InitializeRootRegister() {
906     ExternalReference roots_array_start =
907         ExternalReference::roots_array_start(isolate());
908     li(kRootRegister, Operand(roots_array_start));
909   }
910
911   // -------------------------------------------------------------------------
912   // JavaScript invokes.
913
914   // Invoke the JavaScript function code by either calling or jumping.
915   void InvokeCode(Register code,
916                   const ParameterCount& expected,
917                   const ParameterCount& actual,
918                   InvokeFlag flag,
919                   const CallWrapper& call_wrapper);
920
921   // Invoke the JavaScript function in the given register. Changes the
922   // current context to the context in the function before invoking.
923   void InvokeFunction(Register function,
924                       const ParameterCount& actual,
925                       InvokeFlag flag,
926                       const CallWrapper& call_wrapper);
927
928   void InvokeFunction(Register function,
929                       const ParameterCount& expected,
930                       const ParameterCount& actual,
931                       InvokeFlag flag,
932                       const CallWrapper& call_wrapper);
933
934   void InvokeFunction(Handle<JSFunction> function,
935                       const ParameterCount& expected,
936                       const ParameterCount& actual,
937                       InvokeFlag flag,
938                       const CallWrapper& call_wrapper);
939
940
941   void IsObjectJSObjectType(Register heap_object,
942                             Register map,
943                             Register scratch,
944                             Label* fail);
945
946   void IsInstanceJSObjectType(Register map,
947                               Register scratch,
948                               Label* fail);
949
950   void IsObjectJSStringType(Register object,
951                             Register scratch,
952                             Label* fail);
953
954   void IsObjectNameType(Register object,
955                         Register scratch,
956                         Label* fail);
957
958   // -------------------------------------------------------------------------
959   // Debugger Support.
960
961   void DebugBreak();
962
963   // -------------------------------------------------------------------------
964   // Exception handling.
965
966   // Push a new try handler and link into try handler chain.
967   void PushTryHandler(StackHandler::Kind kind, int handler_index);
968
969   // Unlink the stack handler on top of the stack from the try handler chain.
970   // Must preserve the result register.
971   void PopTryHandler();
972
973   // Passes thrown value to the handler of top of the try handler chain.
974   void Throw(Register value);
975
976   // Propagates an uncatchable exception to the top of the current JS stack's
977   // handler chain.
978   void ThrowUncatchable(Register value);
979
980   // Copies a fixed number of fields of heap objects from src to dst.
981   void CopyFields(Register dst, Register src, RegList temps, int field_count);
982
983   // Copies a number of bytes from src to dst. All registers are clobbered. On
984   // exit src and dst will point to the place just after where the last byte was
985   // read or written and length will be zero.
986   void CopyBytes(Register src,
987                  Register dst,
988                  Register length,
989                  Register scratch);
990
991   // Initialize fields with filler values.  Fields starting at |start_offset|
992   // not including end_offset are overwritten with the value in |filler|.  At
993   // the end the loop, |start_offset| takes the value of |end_offset|.
994   void InitializeFieldsWithFiller(Register start_offset,
995                                   Register end_offset,
996                                   Register filler);
997
998   // -------------------------------------------------------------------------
999   // Support functions.
1000
1001   // Try to get function prototype of a function and puts the value in
1002   // the result register. Checks that the function really is a
1003   // function and jumps to the miss label if the fast checks fail. The
1004   // function register will be untouched; the other registers may be
1005   // clobbered.
1006   void TryGetFunctionPrototype(Register function,
1007                                Register result,
1008                                Register scratch,
1009                                Label* miss,
1010                                bool miss_on_bound_function = false);
1011
1012   void GetObjectType(Register function,
1013                      Register map,
1014                      Register type_reg);
1015
1016   // Check if a map for a JSObject indicates that the object has fast elements.
1017   // Jump to the specified label if it does not.
1018   void CheckFastElements(Register map,
1019                          Register scratch,
1020                          Label* fail);
1021
1022   // Check if a map for a JSObject indicates that the object can have both smi
1023   // and HeapObject elements.  Jump to the specified label if it does not.
1024   void CheckFastObjectElements(Register map,
1025                                Register scratch,
1026                                Label* fail);
1027
1028   // Check if a map for a JSObject indicates that the object has fast smi only
1029   // elements.  Jump to the specified label if it does not.
1030   void CheckFastSmiElements(Register map,
1031                             Register scratch,
1032                             Label* fail);
1033
1034   // Check to see if maybe_number can be stored as a double in
1035   // FastDoubleElements. If it can, store it at the index specified by key in
1036   // the FastDoubleElements array elements. Otherwise jump to fail.
1037   void StoreNumberToDoubleElements(Register value_reg,
1038                                    Register key_reg,
1039                                    Register elements_reg,
1040                                    Register scratch1,
1041                                    Register scratch2,
1042                                    Register scratch3,
1043                                    Label* fail,
1044                                    int elements_offset = 0);
1045
1046   // Compare an object's map with the specified map and its transitioned
1047   // elements maps if mode is ALLOW_ELEMENT_TRANSITION_MAPS. Jumps to
1048   // "branch_to" if the result of the comparison is "cond". If multiple map
1049   // compares are required, the compare sequences branches to early_success.
1050   void CompareMapAndBranch(Register obj,
1051                            Register scratch,
1052                            Handle<Map> map,
1053                            Label* early_success,
1054                            Condition cond,
1055                            Label* branch_to);
1056
1057   // As above, but the map of the object is already loaded into the register
1058   // which is preserved by the code generated.
1059   void CompareMapAndBranch(Register obj_map,
1060                            Handle<Map> map,
1061                            Label* early_success,
1062                            Condition cond,
1063                            Label* branch_to);
1064
1065   // Check if the map of an object is equal to a specified map and branch to
1066   // label if not. Skip the smi check if not required (object is known to be a
1067   // heap object). If mode is ALLOW_ELEMENT_TRANSITION_MAPS, then also match
1068   // against maps that are ElementsKind transition maps of the specificed map.
1069   void CheckMap(Register obj,
1070                 Register scratch,
1071                 Handle<Map> map,
1072                 Label* fail,
1073                 SmiCheckType smi_check_type);
1074
1075
1076   void CheckMap(Register obj,
1077                 Register scratch,
1078                 Heap::RootListIndex index,
1079                 Label* fail,
1080                 SmiCheckType smi_check_type);
1081
1082   // Check if the map of an object is equal to a specified weak map and branch
1083   // to a specified target if equal. Skip the smi check if not required
1084   // (object is known to be a heap object)
1085   void DispatchWeakMap(Register obj, Register scratch1, Register scratch2,
1086                        Handle<WeakCell> cell, Handle<Code> success,
1087                        SmiCheckType smi_check_type);
1088
1089   // Get value of the weak cell.
1090   void GetWeakValue(Register value, Handle<WeakCell> cell);
1091
1092   // Load the value of the weak cell in the value register. Branch to the
1093   // given miss label is the weak cell was cleared.
1094   void LoadWeakValue(Register value, Handle<WeakCell> cell, Label* miss);
1095
1096   // Load and check the instance type of an object for being a string.
1097   // Loads the type into the second argument register.
1098   // Returns a condition that will be enabled if the object was a string.
1099   Condition IsObjectStringType(Register obj,
1100                                Register type,
1101                                Register result) {
1102     lw(type, FieldMemOperand(obj, HeapObject::kMapOffset));
1103     lbu(type, FieldMemOperand(type, Map::kInstanceTypeOffset));
1104     And(type, type, Operand(kIsNotStringMask));
1105     DCHECK_EQ(0u, kStringTag);
1106     return eq;
1107   }
1108
1109
1110   // Picks out an array index from the hash field.
1111   // Register use:
1112   //   hash - holds the index's hash. Clobbered.
1113   //   index - holds the overwritten index on exit.
1114   void IndexFromHash(Register hash, Register index);
1115
1116   // Get the number of least significant bits from a register.
1117   void GetLeastBitsFromSmi(Register dst, Register src, int num_least_bits);
1118   void GetLeastBitsFromInt32(Register dst, Register src, int mun_least_bits);
1119
1120   // Load the value of a number object into a FPU double register. If the
1121   // object is not a number a jump to the label not_number is performed
1122   // and the FPU double register is unchanged.
1123   void ObjectToDoubleFPURegister(
1124       Register object,
1125       FPURegister value,
1126       Register scratch1,
1127       Register scratch2,
1128       Register heap_number_map,
1129       Label* not_number,
1130       ObjectToDoubleFlags flags = NO_OBJECT_TO_DOUBLE_FLAGS);
1131
1132   // Load the value of a smi object into a FPU double register. The register
1133   // scratch1 can be the same register as smi in which case smi will hold the
1134   // untagged value afterwards.
1135   void SmiToDoubleFPURegister(Register smi,
1136                               FPURegister value,
1137                               Register scratch1);
1138
1139   // -------------------------------------------------------------------------
1140   // Overflow handling functions.
1141   // Usage: first call the appropriate arithmetic function, then call one of the
1142   // jump functions with the overflow_dst register as the second parameter.
1143
1144   void AdduAndCheckForOverflow(Register dst,
1145                                Register left,
1146                                Register right,
1147                                Register overflow_dst,
1148                                Register scratch = at);
1149
1150   void AdduAndCheckForOverflow(Register dst, Register left,
1151                                const Operand& right, Register overflow_dst,
1152                                Register scratch = at);
1153
1154   void SubuAndCheckForOverflow(Register dst,
1155                                Register left,
1156                                Register right,
1157                                Register overflow_dst,
1158                                Register scratch = at);
1159
1160   void SubuAndCheckForOverflow(Register dst, Register left,
1161                                const Operand& right, Register overflow_dst,
1162                                Register scratch = at);
1163
1164   void BranchOnOverflow(Label* label,
1165                         Register overflow_check,
1166                         BranchDelaySlot bd = PROTECT) {
1167     Branch(label, lt, overflow_check, Operand(zero_reg), bd);
1168   }
1169
1170   void BranchOnNoOverflow(Label* label,
1171                           Register overflow_check,
1172                           BranchDelaySlot bd = PROTECT) {
1173     Branch(label, ge, overflow_check, Operand(zero_reg), bd);
1174   }
1175
1176   void RetOnOverflow(Register overflow_check, BranchDelaySlot bd = PROTECT) {
1177     Ret(lt, overflow_check, Operand(zero_reg), bd);
1178   }
1179
1180   void RetOnNoOverflow(Register overflow_check, BranchDelaySlot bd = PROTECT) {
1181     Ret(ge, overflow_check, Operand(zero_reg), bd);
1182   }
1183
1184   // -------------------------------------------------------------------------
1185   // Runtime calls.
1186
1187   // See comments at the beginning of CEntryStub::Generate.
1188   inline void PrepareCEntryArgs(int num_args) { li(a0, num_args); }
1189
1190   inline void PrepareCEntryFunction(const ExternalReference& ref) {
1191     li(a1, Operand(ref));
1192   }
1193
1194 #define COND_ARGS Condition cond = al, Register rs = zero_reg, \
1195 const Operand& rt = Operand(zero_reg), BranchDelaySlot bd = PROTECT
1196
1197   // Call a code stub.
1198   void CallStub(CodeStub* stub,
1199                 TypeFeedbackId ast_id = TypeFeedbackId::None(),
1200                 COND_ARGS);
1201
1202   // Tail call a code stub (jump).
1203   void TailCallStub(CodeStub* stub, COND_ARGS);
1204
1205 #undef COND_ARGS
1206
1207   void CallJSExitStub(CodeStub* stub);
1208
1209   // Call a runtime routine.
1210   void CallRuntime(const Runtime::Function* f,
1211                    int num_arguments,
1212                    SaveFPRegsMode save_doubles = kDontSaveFPRegs);
1213   void CallRuntimeSaveDoubles(Runtime::FunctionId id) {
1214     const Runtime::Function* function = Runtime::FunctionForId(id);
1215     CallRuntime(function, function->nargs, kSaveFPRegs);
1216   }
1217
1218   // Convenience function: Same as above, but takes the fid instead.
1219   void CallRuntime(Runtime::FunctionId id,
1220                    int num_arguments,
1221                    SaveFPRegsMode save_doubles = kDontSaveFPRegs) {
1222     CallRuntime(Runtime::FunctionForId(id), num_arguments, save_doubles);
1223   }
1224
1225   // Convenience function: call an external reference.
1226   void CallExternalReference(const ExternalReference& ext,
1227                              int num_arguments,
1228                              BranchDelaySlot bd = PROTECT);
1229
1230   // Tail call of a runtime routine (jump).
1231   // Like JumpToExternalReference, but also takes care of passing the number
1232   // of parameters.
1233   void TailCallExternalReference(const ExternalReference& ext,
1234                                  int num_arguments,
1235                                  int result_size);
1236
1237   // Convenience function: tail call a runtime routine (jump).
1238   void TailCallRuntime(Runtime::FunctionId fid,
1239                        int num_arguments,
1240                        int result_size);
1241
1242   int CalculateStackPassedWords(int num_reg_arguments,
1243                                 int num_double_arguments);
1244
1245   // Before calling a C-function from generated code, align arguments on stack
1246   // and add space for the four mips argument slots.
1247   // After aligning the frame, non-register arguments must be stored on the
1248   // stack, after the argument-slots using helper: CFunctionArgumentOperand().
1249   // The argument count assumes all arguments are word sized.
1250   // Some compilers/platforms require the stack to be aligned when calling
1251   // C++ code.
1252   // Needs a scratch register to do some arithmetic. This register will be
1253   // trashed.
1254   void PrepareCallCFunction(int num_reg_arguments,
1255                             int num_double_registers,
1256                             Register scratch);
1257   void PrepareCallCFunction(int num_reg_arguments,
1258                             Register scratch);
1259
1260   // Arguments 1-4 are placed in registers a0 thru a3 respectively.
1261   // Arguments 5..n are stored to stack using following:
1262   //  sw(t0, CFunctionArgumentOperand(5));
1263
1264   // Calls a C function and cleans up the space for arguments allocated
1265   // by PrepareCallCFunction. The called function is not allowed to trigger a
1266   // garbage collection, since that might move the code and invalidate the
1267   // return address (unless this is somehow accounted for by the called
1268   // function).
1269   void CallCFunction(ExternalReference function, int num_arguments);
1270   void CallCFunction(Register function, int num_arguments);
1271   void CallCFunction(ExternalReference function,
1272                      int num_reg_arguments,
1273                      int num_double_arguments);
1274   void CallCFunction(Register function,
1275                      int num_reg_arguments,
1276                      int num_double_arguments);
1277   void MovFromFloatResult(DoubleRegister dst);
1278   void MovFromFloatParameter(DoubleRegister dst);
1279
1280   // There are two ways of passing double arguments on MIPS, depending on
1281   // whether soft or hard floating point ABI is used. These functions
1282   // abstract parameter passing for the three different ways we call
1283   // C functions from generated code.
1284   void MovToFloatParameter(DoubleRegister src);
1285   void MovToFloatParameters(DoubleRegister src1, DoubleRegister src2);
1286   void MovToFloatResult(DoubleRegister src);
1287
1288   // Jump to the builtin routine.
1289   void JumpToExternalReference(const ExternalReference& builtin,
1290                                BranchDelaySlot bd = PROTECT);
1291
1292   // Invoke specified builtin JavaScript function. Adds an entry to
1293   // the unresolved list if the name does not resolve.
1294   void InvokeBuiltin(Builtins::JavaScript id,
1295                      InvokeFlag flag,
1296                      const CallWrapper& call_wrapper = NullCallWrapper());
1297
1298   // Store the code object for the given builtin in the target register and
1299   // setup the function in a1.
1300   void GetBuiltinEntry(Register target, Builtins::JavaScript id);
1301
1302   // Store the function for the given builtin in the target register.
1303   void GetBuiltinFunction(Register target, Builtins::JavaScript id);
1304
1305   struct Unresolved {
1306     int pc;
1307     uint32_t flags;  // See Bootstrapper::FixupFlags decoders/encoders.
1308     const char* name;
1309   };
1310
1311   Handle<Object> CodeObject() {
1312     DCHECK(!code_object_.is_null());
1313     return code_object_;
1314   }
1315
1316   // Emit code for a truncating division by a constant. The dividend register is
1317   // unchanged and at gets clobbered. Dividend and result must be different.
1318   void TruncatingDiv(Register result, Register dividend, int32_t divisor);
1319
1320   // -------------------------------------------------------------------------
1321   // StatsCounter support.
1322
1323   void SetCounter(StatsCounter* counter, int value,
1324                   Register scratch1, Register scratch2);
1325   void IncrementCounter(StatsCounter* counter, int value,
1326                         Register scratch1, Register scratch2);
1327   void DecrementCounter(StatsCounter* counter, int value,
1328                         Register scratch1, Register scratch2);
1329
1330
1331   // -------------------------------------------------------------------------
1332   // Debugging.
1333
1334   // Calls Abort(msg) if the condition cc is not satisfied.
1335   // Use --debug_code to enable.
1336   void Assert(Condition cc, BailoutReason reason, Register rs, Operand rt);
1337   void AssertFastElements(Register elements);
1338
1339   // Like Assert(), but always enabled.
1340   void Check(Condition cc, BailoutReason reason, Register rs, Operand rt);
1341
1342   // Print a message to stdout and abort execution.
1343   void Abort(BailoutReason msg);
1344
1345   // Verify restrictions about code generated in stubs.
1346   void set_generating_stub(bool value) { generating_stub_ = value; }
1347   bool generating_stub() { return generating_stub_; }
1348   void set_has_frame(bool value) { has_frame_ = value; }
1349   bool has_frame() { return has_frame_; }
1350   inline bool AllowThisStubCall(CodeStub* stub);
1351
1352   // ---------------------------------------------------------------------------
1353   // Number utilities.
1354
1355   // Check whether the value of reg is a power of two and not zero. If not
1356   // control continues at the label not_power_of_two. If reg is a power of two
1357   // the register scratch contains the value of (reg - 1) when control falls
1358   // through.
1359   void JumpIfNotPowerOfTwoOrZero(Register reg,
1360                                  Register scratch,
1361                                  Label* not_power_of_two_or_zero);
1362
1363   // -------------------------------------------------------------------------
1364   // Smi utilities.
1365
1366   void SmiTag(Register reg) {
1367     Addu(reg, reg, reg);
1368   }
1369
1370   // Test for overflow < 0: use BranchOnOverflow() or BranchOnNoOverflow().
1371   void SmiTagCheckOverflow(Register reg, Register overflow);
1372   void SmiTagCheckOverflow(Register dst, Register src, Register overflow);
1373
1374   void SmiTag(Register dst, Register src) {
1375     Addu(dst, src, src);
1376   }
1377
1378   // Try to convert int32 to smi. If the value is to large, preserve
1379   // the original value and jump to not_a_smi. Destroys scratch and
1380   // sets flags.
1381   void TrySmiTag(Register reg, Register scratch, Label* not_a_smi) {
1382     TrySmiTag(reg, reg, scratch, not_a_smi);
1383   }
1384   void TrySmiTag(Register dst,
1385                  Register src,
1386                  Register scratch,
1387                  Label* not_a_smi) {
1388     SmiTagCheckOverflow(at, src, scratch);
1389     BranchOnOverflow(not_a_smi, scratch);
1390     mov(dst, at);
1391   }
1392
1393   void SmiUntag(Register reg) {
1394     sra(reg, reg, kSmiTagSize);
1395   }
1396
1397   void SmiUntag(Register dst, Register src) {
1398     sra(dst, src, kSmiTagSize);
1399   }
1400
1401   // Test if the register contains a smi.
1402   inline void SmiTst(Register value, Register scratch) {
1403     And(scratch, value, Operand(kSmiTagMask));
1404   }
1405   inline void NonNegativeSmiTst(Register value, Register scratch) {
1406     And(scratch, value, Operand(kSmiTagMask | kSmiSignMask));
1407   }
1408
1409   // Untag the source value into destination and jump if source is a smi.
1410   // Souce and destination can be the same register.
1411   void UntagAndJumpIfSmi(Register dst, Register src, Label* smi_case);
1412
1413   // Untag the source value into destination and jump if source is not a smi.
1414   // Souce and destination can be the same register.
1415   void UntagAndJumpIfNotSmi(Register dst, Register src, Label* non_smi_case);
1416
1417   // Jump the register contains a smi.
1418   void JumpIfSmi(Register value,
1419                  Label* smi_label,
1420                  Register scratch = at,
1421                  BranchDelaySlot bd = PROTECT);
1422
1423   // Jump if the register contains a non-smi.
1424   void JumpIfNotSmi(Register value,
1425                     Label* not_smi_label,
1426                     Register scratch = at,
1427                     BranchDelaySlot bd = PROTECT);
1428
1429   // Jump if either of the registers contain a non-smi.
1430   void JumpIfNotBothSmi(Register reg1, Register reg2, Label* on_not_both_smi);
1431   // Jump if either of the registers contain a smi.
1432   void JumpIfEitherSmi(Register reg1, Register reg2, Label* on_either_smi);
1433
1434   // Abort execution if argument is a smi, enabled via --debug-code.
1435   void AssertNotSmi(Register object);
1436   void AssertSmi(Register object);
1437
1438   // Abort execution if argument is not a string, enabled via --debug-code.
1439   void AssertString(Register object);
1440
1441   // Abort execution if argument is not a name, enabled via --debug-code.
1442   void AssertName(Register object);
1443
1444   // Abort execution if argument is not undefined or an AllocationSite, enabled
1445   // via --debug-code.
1446   void AssertUndefinedOrAllocationSite(Register object, Register scratch);
1447
1448   // Abort execution if reg is not the root value with the given index,
1449   // enabled via --debug-code.
1450   void AssertIsRoot(Register reg, Heap::RootListIndex index);
1451
1452   // ---------------------------------------------------------------------------
1453   // HeapNumber utilities.
1454
1455   void JumpIfNotHeapNumber(Register object,
1456                            Register heap_number_map,
1457                            Register scratch,
1458                            Label* on_not_heap_number);
1459
1460   // -------------------------------------------------------------------------
1461   // String utilities.
1462
1463   // Generate code to do a lookup in the number string cache. If the number in
1464   // the register object is found in the cache the generated code falls through
1465   // with the result in the result register. The object and the result register
1466   // can be the same. If the number is not found in the cache the code jumps to
1467   // the label not_found with only the content of register object unchanged.
1468   void LookupNumberStringCache(Register object,
1469                                Register result,
1470                                Register scratch1,
1471                                Register scratch2,
1472                                Register scratch3,
1473                                Label* not_found);
1474
1475   // Checks if both instance types are sequential ASCII strings and jumps to
1476   // label if either is not.
1477   void JumpIfBothInstanceTypesAreNotSequentialOneByte(
1478       Register first_object_instance_type, Register second_object_instance_type,
1479       Register scratch1, Register scratch2, Label* failure);
1480
1481   // Check if instance type is sequential one-byte string and jump to label if
1482   // it is not.
1483   void JumpIfInstanceTypeIsNotSequentialOneByte(Register type, Register scratch,
1484                                                 Label* failure);
1485
1486   void JumpIfNotUniqueNameInstanceType(Register reg, Label* not_unique_name);
1487
1488   void EmitSeqStringSetCharCheck(Register string,
1489                                  Register index,
1490                                  Register value,
1491                                  Register scratch,
1492                                  uint32_t encoding_mask);
1493
1494   // Checks if both objects are sequential one-byte strings and jumps to label
1495   // if either is not. Assumes that neither object is a smi.
1496   void JumpIfNonSmisNotBothSequentialOneByteStrings(Register first,
1497                                                     Register second,
1498                                                     Register scratch1,
1499                                                     Register scratch2,
1500                                                     Label* failure);
1501
1502   // Checks if both objects are sequential one-byte strings and jumps to label
1503   // if either is not.
1504   void JumpIfNotBothSequentialOneByteStrings(Register first, Register second,
1505                                              Register scratch1,
1506                                              Register scratch2,
1507                                              Label* not_flat_one_byte_strings);
1508
1509   void ClampUint8(Register output_reg, Register input_reg);
1510
1511   void ClampDoubleToUint8(Register result_reg,
1512                           DoubleRegister input_reg,
1513                           DoubleRegister temp_double_reg);
1514
1515
1516   void LoadInstanceDescriptors(Register map, Register descriptors);
1517   void EnumLength(Register dst, Register map);
1518   void NumberOfOwnDescriptors(Register dst, Register map);
1519   void LoadAccessor(Register dst, Register holder, int accessor_index,
1520                     AccessorComponent accessor);
1521
1522   template<typename Field>
1523   void DecodeField(Register dst, Register src) {
1524     Ext(dst, src, Field::kShift, Field::kSize);
1525   }
1526
1527   template<typename Field>
1528   void DecodeField(Register reg) {
1529     DecodeField<Field>(reg, reg);
1530   }
1531
1532   template<typename Field>
1533   void DecodeFieldToSmi(Register dst, Register src) {
1534     static const int shift = Field::kShift;
1535     static const int mask = Field::kMask >> shift << kSmiTagSize;
1536     STATIC_ASSERT((mask & (0x80000000u >> (kSmiTagSize - 1))) == 0);
1537     STATIC_ASSERT(kSmiTag == 0);
1538     if (shift < kSmiTagSize) {
1539       sll(dst, src, kSmiTagSize - shift);
1540       And(dst, dst, Operand(mask));
1541     } else if (shift > kSmiTagSize) {
1542       srl(dst, src, shift - kSmiTagSize);
1543       And(dst, dst, Operand(mask));
1544     } else {
1545       And(dst, src, Operand(mask));
1546     }
1547   }
1548
1549   template<typename Field>
1550   void DecodeFieldToSmi(Register reg) {
1551     DecodeField<Field>(reg, reg);
1552   }
1553
1554   // Generates function and stub prologue code.
1555   void StubPrologue();
1556   void Prologue(bool code_pre_aging);
1557
1558   // Activation support.
1559   void EnterFrame(StackFrame::Type type);
1560   void EnterFrame(StackFrame::Type type, bool load_constant_pool_pointer_reg);
1561   void LeaveFrame(StackFrame::Type type);
1562
1563   // Patch the relocated value (lui/ori pair).
1564   void PatchRelocatedValue(Register li_location,
1565                            Register scratch,
1566                            Register new_value);
1567   // Get the relocatad value (loaded data) from the lui/ori pair.
1568   void GetRelocatedValue(Register li_location,
1569                          Register value,
1570                          Register scratch);
1571
1572   // Expects object in a0 and returns map with validated enum cache
1573   // in a0.  Assumes that any other register can be used as a scratch.
1574   void CheckEnumCache(Register null_value, Label* call_runtime);
1575
1576   // AllocationMemento support. Arrays may have an associated
1577   // AllocationMemento object that can be checked for in order to pretransition
1578   // to another type.
1579   // On entry, receiver_reg should point to the array object.
1580   // scratch_reg gets clobbered.
1581   // If allocation info is present, jump to allocation_memento_present.
1582   void TestJSArrayForAllocationMemento(
1583       Register receiver_reg,
1584       Register scratch_reg,
1585       Label* no_memento_found,
1586       Condition cond = al,
1587       Label* allocation_memento_present = NULL);
1588
1589   void JumpIfJSArrayHasAllocationMemento(Register receiver_reg,
1590                                          Register scratch_reg,
1591                                          Label* memento_found) {
1592     Label no_memento_found;
1593     TestJSArrayForAllocationMemento(receiver_reg, scratch_reg,
1594                                     &no_memento_found, eq, memento_found);
1595     bind(&no_memento_found);
1596   }
1597
1598   // Jumps to found label if a prototype map has dictionary elements.
1599   void JumpIfDictionaryInPrototypeChain(Register object, Register scratch0,
1600                                         Register scratch1, Label* found);
1601
1602  private:
1603   void CallCFunctionHelper(Register function,
1604                            int num_reg_arguments,
1605                            int num_double_arguments);
1606
1607   void BranchAndLinkShort(int16_t offset, BranchDelaySlot bdslot = PROTECT);
1608   void BranchAndLinkShort(int16_t offset, Condition cond, Register rs,
1609                           const Operand& rt,
1610                           BranchDelaySlot bdslot = PROTECT);
1611   void BranchAndLinkShort(Label* L, BranchDelaySlot bdslot = PROTECT);
1612   void BranchAndLinkShort(Label* L, Condition cond, Register rs,
1613                           const Operand& rt,
1614                           BranchDelaySlot bdslot = PROTECT);
1615   void J(Label* L, BranchDelaySlot bdslot);
1616   void Jr(Label* L, BranchDelaySlot bdslot);
1617   void Jalr(Label* L, BranchDelaySlot bdslot);
1618
1619   // Helper functions for generating invokes.
1620   void InvokePrologue(const ParameterCount& expected,
1621                       const ParameterCount& actual,
1622                       Handle<Code> code_constant,
1623                       Register code_reg,
1624                       Label* done,
1625                       bool* definitely_mismatches,
1626                       InvokeFlag flag,
1627                       const CallWrapper& call_wrapper);
1628
1629   // Get the code for the given builtin. Returns if able to resolve
1630   // the function in the 'resolved' flag.
1631   Handle<Code> ResolveBuiltin(Builtins::JavaScript id, bool* resolved);
1632
1633   void InitializeNewString(Register string,
1634                            Register length,
1635                            Heap::RootListIndex map_index,
1636                            Register scratch1,
1637                            Register scratch2);
1638
1639   // Helper for implementing JumpIfNotInNewSpace and JumpIfInNewSpace.
1640   void InNewSpace(Register object,
1641                   Register scratch,
1642                   Condition cond,  // eq for new space, ne otherwise.
1643                   Label* branch);
1644
1645   // Helper for finding the mark bits for an address.  Afterwards, the
1646   // bitmap register points at the word with the mark bits and the mask
1647   // the position of the first bit.  Leaves addr_reg unchanged.
1648   inline void GetMarkBits(Register addr_reg,
1649                           Register bitmap_reg,
1650                           Register mask_reg);
1651
1652   // Helper for throwing exceptions.  Compute a handler address and jump to
1653   // it.  See the implementation for register usage.
1654   void JumpToHandlerEntry();
1655
1656   // Compute memory operands for safepoint stack slots.
1657   static int SafepointRegisterStackIndex(int reg_code);
1658   MemOperand SafepointRegisterSlot(Register reg);
1659   MemOperand SafepointRegistersAndDoublesSlot(Register reg);
1660
1661   bool generating_stub_;
1662   bool has_frame_;
1663   bool has_double_zero_reg_set_;
1664   // This handle will be patched with the code object on installation.
1665   Handle<Object> code_object_;
1666
1667   // Needs access to SafepointRegisterStackIndex for compiled frame
1668   // traversal.
1669   friend class StandardFrame;
1670 };
1671
1672
1673 // The code patcher is used to patch (typically) small parts of code e.g. for
1674 // debugging and other types of instrumentation. When using the code patcher
1675 // the exact number of bytes specified must be emitted. It is not legal to emit
1676 // relocation information. If any of these constraints are violated it causes
1677 // an assertion to fail.
1678 class CodePatcher {
1679  public:
1680   enum FlushICache {
1681     FLUSH,
1682     DONT_FLUSH
1683   };
1684
1685   CodePatcher(byte* address,
1686               int instructions,
1687               FlushICache flush_cache = FLUSH);
1688   virtual ~CodePatcher();
1689
1690   // Macro assembler to emit code.
1691   MacroAssembler* masm() { return &masm_; }
1692
1693   // Emit an instruction directly.
1694   void Emit(Instr instr);
1695
1696   // Emit an address directly.
1697   void Emit(Address addr);
1698
1699   // Change the condition part of an instruction leaving the rest of the current
1700   // instruction unchanged.
1701   void ChangeBranchCondition(Condition cond);
1702
1703  private:
1704   byte* address_;  // The address of the code being patched.
1705   int size_;  // Number of bytes of the expected patch size.
1706   MacroAssembler masm_;  // Macro assembler used to generate the code.
1707   FlushICache flush_cache_;  // Whether to flush the I cache after patching.
1708 };
1709
1710
1711
1712 #ifdef GENERATED_CODE_COVERAGE
1713 #define CODE_COVERAGE_STRINGIFY(x) #x
1714 #define CODE_COVERAGE_TOSTRING(x) CODE_COVERAGE_STRINGIFY(x)
1715 #define __FILE_LINE__ __FILE__ ":" CODE_COVERAGE_TOSTRING(__LINE__)
1716 #define ACCESS_MASM(masm) masm->stop(__FILE_LINE__); masm->
1717 #else
1718 #define ACCESS_MASM(masm) masm->
1719 #endif
1720
1721 } }  // namespace v8::internal
1722
1723 #endif  // V8_MIPS_MACRO_ASSEMBLER_MIPS_H_