c47f6d3abe2760997039c195e584ce6276a9440d
[platform/upstream/v8.git] / src / mips / assembler-mips.h
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // - Redistribution in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // - Neither the name of Sun Microsystems or the names of contributors may
16 // be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // The original source code covered by the above license above has been
32 // modified significantly by Google Inc.
33 // Copyright 2012 the V8 project authors. All rights reserved.
34
35
36 #ifndef V8_MIPS_ASSEMBLER_MIPS_H_
37 #define V8_MIPS_ASSEMBLER_MIPS_H_
38
39 #include <stdio.h>
40
41 #include <set>
42
43 #include "src/assembler.h"
44 #include "src/compiler.h"
45 #include "src/mips/constants-mips.h"
46
47 namespace v8 {
48 namespace internal {
49
50 // CPU Registers.
51 //
52 // 1) We would prefer to use an enum, but enum values are assignment-
53 // compatible with int, which has caused code-generation bugs.
54 //
55 // 2) We would prefer to use a class instead of a struct but we don't like
56 // the register initialization to depend on the particular initialization
57 // order (which appears to be different on OS X, Linux, and Windows for the
58 // installed versions of C++ we tried). Using a struct permits C-style
59 // "initialization". Also, the Register objects cannot be const as this
60 // forces initialization stubs in MSVC, making us dependent on initialization
61 // order.
62 //
63 // 3) By not using an enum, we are possibly preventing the compiler from
64 // doing certain constant folds, which may significantly reduce the
65 // code generated for some assembly instructions (because they boil down
66 // to a few constants). If this is a problem, we could change the code
67 // such that we use an enum in optimized mode, and the struct in debug
68 // mode. This way we get the compile-time error checking in debug mode
69 // and best performance in optimized code.
70
71
72 // -----------------------------------------------------------------------------
73 // Implementation of Register and FPURegister.
74
75 // Core register.
76 struct Register {
77   static const int kNumRegisters = v8::internal::kNumRegisters;
78   static const int kMaxNumAllocatableRegisters = 14;  // v0 through t6 and cp.
79   static const int kSizeInBytes = 4;
80   static const int kCpRegister = 23;  // cp (s7) is the 23rd register.
81
82 #if defined(V8_TARGET_LITTLE_ENDIAN)
83   static const int kMantissaOffset = 0;
84   static const int kExponentOffset = 4;
85 #elif defined(V8_TARGET_BIG_ENDIAN)
86   static const int kMantissaOffset = 4;
87   static const int kExponentOffset = 0;
88 #else
89 #error Unknown endianness
90 #endif
91
92   inline static int NumAllocatableRegisters();
93
94   static int ToAllocationIndex(Register reg) {
95     DCHECK((reg.code() - 2) < (kMaxNumAllocatableRegisters - 1) ||
96            reg.is(from_code(kCpRegister)));
97     return reg.is(from_code(kCpRegister)) ?
98            kMaxNumAllocatableRegisters - 1 :  // Return last index for 'cp'.
99            reg.code() - 2;  // zero_reg and 'at' are skipped.
100   }
101
102   static Register FromAllocationIndex(int index) {
103     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
104     return index == kMaxNumAllocatableRegisters - 1 ?
105            from_code(kCpRegister) :  // Last index is always the 'cp' register.
106            from_code(index + 2);  // zero_reg and 'at' are skipped.
107   }
108
109   static const char* AllocationIndexToString(int index) {
110     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
111     const char* const names[] = {
112       "v0",
113       "v1",
114       "a0",
115       "a1",
116       "a2",
117       "a3",
118       "t0",
119       "t1",
120       "t2",
121       "t3",
122       "t4",
123       "t5",
124       "t6",
125       "s7",
126     };
127     return names[index];
128   }
129
130   static Register from_code(int code) {
131     Register r = { code };
132     return r;
133   }
134
135   bool is_valid() const { return 0 <= code_ && code_ < kNumRegisters; }
136   bool is(Register reg) const { return code_ == reg.code_; }
137   int code() const {
138     DCHECK(is_valid());
139     return code_;
140   }
141   int bit() const {
142     DCHECK(is_valid());
143     return 1 << code_;
144   }
145
146   // Unfortunately we can't make this private in a struct.
147   int code_;
148 };
149
150 #define REGISTER(N, C) \
151   const int kRegister_ ## N ## _Code = C; \
152   const Register N = { C }
153
154 REGISTER(no_reg, -1);
155 // Always zero.
156 REGISTER(zero_reg, 0);
157 // at: Reserved for synthetic instructions.
158 REGISTER(at, 1);
159 // v0, v1: Used when returning multiple values from subroutines.
160 REGISTER(v0, 2);
161 REGISTER(v1, 3);
162 // a0 - a4: Used to pass non-FP parameters.
163 REGISTER(a0, 4);
164 REGISTER(a1, 5);
165 REGISTER(a2, 6);
166 REGISTER(a3, 7);
167 // t0 - t9: Can be used without reservation, act as temporary registers and are
168 // allowed to be destroyed by subroutines.
169 REGISTER(t0, 8);
170 REGISTER(t1, 9);
171 REGISTER(t2, 10);
172 REGISTER(t3, 11);
173 REGISTER(t4, 12);
174 REGISTER(t5, 13);
175 REGISTER(t6, 14);
176 REGISTER(t7, 15);
177 // s0 - s7: Subroutine register variables. Subroutines that write to these
178 // registers must restore their values before exiting so that the caller can
179 // expect the values to be preserved.
180 REGISTER(s0, 16);
181 REGISTER(s1, 17);
182 REGISTER(s2, 18);
183 REGISTER(s3, 19);
184 REGISTER(s4, 20);
185 REGISTER(s5, 21);
186 REGISTER(s6, 22);
187 REGISTER(s7, 23);
188 REGISTER(t8, 24);
189 REGISTER(t9, 25);
190 // k0, k1: Reserved for system calls and interrupt handlers.
191 REGISTER(k0, 26);
192 REGISTER(k1, 27);
193 // gp: Reserved.
194 REGISTER(gp, 28);
195 // sp: Stack pointer.
196 REGISTER(sp, 29);
197 // fp: Frame pointer.
198 REGISTER(fp, 30);
199 // ra: Return address pointer.
200 REGISTER(ra, 31);
201
202 #undef REGISTER
203
204
205 int ToNumber(Register reg);
206
207 Register ToRegister(int num);
208
209 // Coprocessor register.
210 struct FPURegister {
211   static const int kMaxNumRegisters = v8::internal::kNumFPURegisters;
212
213   // TODO(plind): Warning, inconsistent numbering here. kNumFPURegisters refers
214   // to number of 32-bit FPU regs, but kNumAllocatableRegisters refers to
215   // number of Double regs (64-bit regs, or FPU-reg-pairs).
216
217   // A few double registers are reserved: one as a scratch register and one to
218   // hold 0.0.
219   //  f28: 0.0
220   //  f30: scratch register.
221   static const int kNumReservedRegisters = 2;
222   static const int kMaxNumAllocatableRegisters = kMaxNumRegisters / 2 -
223       kNumReservedRegisters;
224
225   inline static int NumRegisters();
226   inline static int NumAllocatableRegisters();
227
228   // TODO(turbofan): Proper support for float32.
229   inline static int NumAllocatableAliasedRegisters();
230
231   inline static int ToAllocationIndex(FPURegister reg);
232   static const char* AllocationIndexToString(int index);
233
234   static FPURegister FromAllocationIndex(int index) {
235     DCHECK(index >= 0 && index < kMaxNumAllocatableRegisters);
236     return from_code(index * 2);
237   }
238
239   static FPURegister from_code(int code) {
240     FPURegister r = { code };
241     return r;
242   }
243
244   bool is_valid() const { return 0 <= code_ && code_ < kMaxNumRegisters ; }
245   bool is(FPURegister creg) const { return code_ == creg.code_; }
246   FPURegister low() const {
247     // Find low reg of a Double-reg pair, which is the reg itself.
248     DCHECK(code_ % 2 == 0);  // Specified Double reg must be even.
249     FPURegister reg;
250     reg.code_ = code_;
251     DCHECK(reg.is_valid());
252     return reg;
253   }
254   FPURegister high() const {
255     // Find high reg of a Doubel-reg pair, which is reg + 1.
256     DCHECK(code_ % 2 == 0);  // Specified Double reg must be even.
257     FPURegister reg;
258     reg.code_ = code_ + 1;
259     DCHECK(reg.is_valid());
260     return reg;
261   }
262
263   int code() const {
264     DCHECK(is_valid());
265     return code_;
266   }
267   int bit() const {
268     DCHECK(is_valid());
269     return 1 << code_;
270   }
271   void setcode(int f) {
272     code_ = f;
273     DCHECK(is_valid());
274   }
275   // Unfortunately we can't make this private in a struct.
276   int code_;
277 };
278
279 // V8 now supports the O32 ABI, and the FPU Registers are organized as 32
280 // 32-bit registers, f0 through f31. When used as 'double' they are used
281 // in pairs, starting with the even numbered register. So a double operation
282 // on f0 really uses f0 and f1.
283 // (Modern mips hardware also supports 32 64-bit registers, via setting
284 // (priviledged) Status Register FR bit to 1. This is used by the N32 ABI,
285 // but it is not in common use. Someday we will want to support this in v8.)
286
287 // For O32 ABI, Floats and Doubles refer to same set of 32 32-bit registers.
288 typedef FPURegister DoubleRegister;
289 typedef FPURegister FloatRegister;
290
291 const FPURegister no_freg = { -1 };
292
293 const FPURegister f0 = { 0 };  // Return value in hard float mode.
294 const FPURegister f1 = { 1 };
295 const FPURegister f2 = { 2 };
296 const FPURegister f3 = { 3 };
297 const FPURegister f4 = { 4 };
298 const FPURegister f5 = { 5 };
299 const FPURegister f6 = { 6 };
300 const FPURegister f7 = { 7 };
301 const FPURegister f8 = { 8 };
302 const FPURegister f9 = { 9 };
303 const FPURegister f10 = { 10 };
304 const FPURegister f11 = { 11 };
305 const FPURegister f12 = { 12 };  // Arg 0 in hard float mode.
306 const FPURegister f13 = { 13 };
307 const FPURegister f14 = { 14 };  // Arg 1 in hard float mode.
308 const FPURegister f15 = { 15 };
309 const FPURegister f16 = { 16 };
310 const FPURegister f17 = { 17 };
311 const FPURegister f18 = { 18 };
312 const FPURegister f19 = { 19 };
313 const FPURegister f20 = { 20 };
314 const FPURegister f21 = { 21 };
315 const FPURegister f22 = { 22 };
316 const FPURegister f23 = { 23 };
317 const FPURegister f24 = { 24 };
318 const FPURegister f25 = { 25 };
319 const FPURegister f26 = { 26 };
320 const FPURegister f27 = { 27 };
321 const FPURegister f28 = { 28 };
322 const FPURegister f29 = { 29 };
323 const FPURegister f30 = { 30 };
324 const FPURegister f31 = { 31 };
325
326 // Register aliases.
327 // cp is assumed to be a callee saved register.
328 // Defined using #define instead of "static const Register&" because Clang
329 // complains otherwise when a compilation unit that includes this header
330 // doesn't use the variables.
331 #define kRootRegister s6
332 #define cp s7
333 #define kLithiumScratchReg s3
334 #define kLithiumScratchReg2 s4
335 #define kLithiumScratchDouble f30
336 #define kDoubleRegZero f28
337 // Used on mips32r6 for compare operations.
338 // We use the last non-callee saved odd register for O32 ABI
339 #define kDoubleCompareReg f19
340
341 // FPU (coprocessor 1) control registers.
342 // Currently only FCSR (#31) is implemented.
343 struct FPUControlRegister {
344   bool is_valid() const { return code_ == kFCSRRegister; }
345   bool is(FPUControlRegister creg) const { return code_ == creg.code_; }
346   int code() const {
347     DCHECK(is_valid());
348     return code_;
349   }
350   int bit() const {
351     DCHECK(is_valid());
352     return 1 << code_;
353   }
354   void setcode(int f) {
355     code_ = f;
356     DCHECK(is_valid());
357   }
358   // Unfortunately we can't make this private in a struct.
359   int code_;
360 };
361
362 const FPUControlRegister no_fpucreg = { kInvalidFPUControlRegister };
363 const FPUControlRegister FCSR = { kFCSRRegister };
364
365
366 // -----------------------------------------------------------------------------
367 // Machine instruction Operands.
368
369 // Class Operand represents a shifter operand in data processing instructions.
370 class Operand BASE_EMBEDDED {
371  public:
372   // Immediate.
373   INLINE(explicit Operand(int32_t immediate,
374          RelocInfo::Mode rmode = RelocInfo::NONE32));
375   INLINE(explicit Operand(const ExternalReference& f));
376   INLINE(explicit Operand(const char* s));
377   INLINE(explicit Operand(Object** opp));
378   INLINE(explicit Operand(Context** cpp));
379   explicit Operand(Handle<Object> handle);
380   INLINE(explicit Operand(Smi* value));
381
382   // Register.
383   INLINE(explicit Operand(Register rm));
384
385   // Return true if this is a register operand.
386   INLINE(bool is_reg() const);
387
388   inline int32_t immediate() const {
389     DCHECK(!is_reg());
390     return imm32_;
391   }
392
393   Register rm() const { return rm_; }
394
395  private:
396   Register rm_;
397   int32_t imm32_;  // Valid if rm_ == no_reg.
398   RelocInfo::Mode rmode_;
399
400   friend class Assembler;
401   friend class MacroAssembler;
402 };
403
404
405 // On MIPS we have only one adressing mode with base_reg + offset.
406 // Class MemOperand represents a memory operand in load and store instructions.
407 class MemOperand : public Operand {
408  public:
409   // Immediate value attached to offset.
410   enum OffsetAddend {
411     offset_minus_one = -1,
412     offset_zero = 0
413   };
414
415   explicit MemOperand(Register rn, int32_t offset = 0);
416   explicit MemOperand(Register rn, int32_t unit, int32_t multiplier,
417                       OffsetAddend offset_addend = offset_zero);
418   int32_t offset() const { return offset_; }
419
420   bool OffsetIsInt16Encodable() const {
421     return is_int16(offset_);
422   }
423
424  private:
425   int32_t offset_;
426
427   friend class Assembler;
428 };
429
430
431 class Assembler : public AssemblerBase {
432  public:
433   // Create an assembler. Instructions and relocation information are emitted
434   // into a buffer, with the instructions starting from the beginning and the
435   // relocation information starting from the end of the buffer. See CodeDesc
436   // for a detailed comment on the layout (globals.h).
437   //
438   // If the provided buffer is NULL, the assembler allocates and grows its own
439   // buffer, and buffer_size determines the initial buffer size. The buffer is
440   // owned by the assembler and deallocated upon destruction of the assembler.
441   //
442   // If the provided buffer is not NULL, the assembler uses the provided buffer
443   // for code generation and assumes its size to be buffer_size. If the buffer
444   // is too small, a fatal error occurs. No deallocation of the buffer is done
445   // upon destruction of the assembler.
446   Assembler(Isolate* isolate, void* buffer, int buffer_size);
447   virtual ~Assembler() { }
448
449   // GetCode emits any pending (non-emitted) code and fills the descriptor
450   // desc. GetCode() is idempotent; it returns the same result if no other
451   // Assembler functions are invoked in between GetCode() calls.
452   void GetCode(CodeDesc* desc);
453
454   // Label operations & relative jumps (PPUM Appendix D).
455   //
456   // Takes a branch opcode (cc) and a label (L) and generates
457   // either a backward branch or a forward branch and links it
458   // to the label fixup chain. Usage:
459   //
460   // Label L;    // unbound label
461   // j(cc, &L);  // forward branch to unbound label
462   // bind(&L);   // bind label to the current pc
463   // j(cc, &L);  // backward branch to bound label
464   // bind(&L);   // illegal: a label may be bound only once
465   //
466   // Note: The same Label can be used for forward and backward branches
467   // but it may be bound only once.
468   void bind(Label* L);  // Binds an unbound label L to current code position.
469   // Determines if Label is bound and near enough so that branch instruction
470   // can be used to reach it, instead of jump instruction.
471   bool is_near(Label* L);
472
473   // Returns the branch offset to the given label from the current code
474   // position. Links the label to the current position if it is still unbound.
475   // Manages the jump elimination optimization if the second parameter is true.
476   int32_t branch_offset(Label* L, bool jump_elimination_allowed);
477   int32_t branch_offset_compact(Label* L, bool jump_elimination_allowed);
478   int32_t branch_offset21(Label* L, bool jump_elimination_allowed);
479   int32_t branch_offset21_compact(Label* L, bool jump_elimination_allowed);
480   int32_t shifted_branch_offset(Label* L, bool jump_elimination_allowed) {
481     int32_t o = branch_offset(L, jump_elimination_allowed);
482     DCHECK((o & 3) == 0);   // Assert the offset is aligned.
483     return o >> 2;
484   }
485   int32_t shifted_branch_offset_compact(Label* L,
486       bool jump_elimination_allowed) {
487     int32_t o = branch_offset_compact(L, jump_elimination_allowed);
488     DCHECK((o & 3) == 0);   // Assert the offset is aligned.
489     return o >> 2;
490   }
491   uint32_t jump_address(Label* L);
492
493   // Puts a labels target address at the given position.
494   // The high 8 bits are set to zero.
495   void label_at_put(Label* L, int at_offset);
496
497   // Read/Modify the code target address in the branch/call instruction at pc.
498   static Address target_address_at(Address pc);
499   static void set_target_address_at(Address pc,
500                                     Address target,
501                                     ICacheFlushMode icache_flush_mode =
502                                         FLUSH_ICACHE_IF_NEEDED);
503   // On MIPS there is no Constant Pool so we skip that parameter.
504   INLINE(static Address target_address_at(Address pc, Address constant_pool)) {
505     return target_address_at(pc);
506   }
507   INLINE(static void set_target_address_at(
508       Address pc, Address constant_pool, Address target,
509       ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) {
510     set_target_address_at(pc, target, icache_flush_mode);
511   }
512   INLINE(static Address target_address_at(Address pc, Code* code)) {
513     Address constant_pool = code ? code->constant_pool() : NULL;
514     return target_address_at(pc, constant_pool);
515   }
516   INLINE(static void set_target_address_at(Address pc,
517                                            Code* code,
518                                            Address target,
519                                            ICacheFlushMode icache_flush_mode =
520                                                FLUSH_ICACHE_IF_NEEDED)) {
521     Address constant_pool = code ? code->constant_pool() : NULL;
522     set_target_address_at(pc, constant_pool, target, icache_flush_mode);
523   }
524
525   // Return the code target address at a call site from the return address
526   // of that call in the instruction stream.
527   inline static Address target_address_from_return_address(Address pc);
528
529   static void JumpToJumpRegister(Address pc);
530
531   static void QuietNaN(HeapObject* nan);
532
533   // This sets the branch destination (which gets loaded at the call address).
534   // This is for calls and branches within generated code.  The serializer
535   // has already deserialized the lui/ori instructions etc.
536   inline static void deserialization_set_special_target_at(
537       Address instruction_payload, Code* code, Address target) {
538     set_target_address_at(
539         instruction_payload - kInstructionsFor32BitConstant * kInstrSize,
540         code,
541         target);
542   }
543
544   // This sets the internal reference at the pc.
545   inline static void deserialization_set_target_internal_reference_at(
546       Address pc, Address target,
547       RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
548
549   // Size of an instruction.
550   static const int kInstrSize = sizeof(Instr);
551
552   // Difference between address of current opcode and target address offset.
553   static const int kBranchPCOffset = 4;
554
555   // Here we are patching the address in the LUI/ORI instruction pair.
556   // These values are used in the serialization process and must be zero for
557   // MIPS platform, as Code, Embedded Object or External-reference pointers
558   // are split across two consecutive instructions and don't exist separately
559   // in the code, so the serializer should not step forwards in memory after
560   // a target is resolved and written.
561   static const int kSpecialTargetSize = 0;
562
563   // Number of consecutive instructions used to store 32bit constant.
564   // Before jump-optimizations, this constant was used in
565   // RelocInfo::target_address_address() function to tell serializer address of
566   // the instruction that follows LUI/ORI instruction pair. Now, with new jump
567   // optimization, where jump-through-register instruction that usually
568   // follows LUI/ORI pair is substituted with J/JAL, this constant equals
569   // to 3 instructions (LUI+ORI+J/JAL/JR/JALR).
570   static const int kInstructionsFor32BitConstant = 3;
571
572   // Distance between the instruction referring to the address of the call
573   // target and the return address.
574   static const int kCallTargetAddressOffset = 4 * kInstrSize;
575
576   // Distance between start of patched debug break slot and the emitted address
577   // to jump to.
578   static const int kPatchDebugBreakSlotAddressOffset = 4 * kInstrSize;
579
580   // Difference between address of current opcode and value read from pc
581   // register.
582   static const int kPcLoadDelta = 4;
583
584   static const int kDebugBreakSlotInstructions = 4;
585   static const int kDebugBreakSlotLength =
586       kDebugBreakSlotInstructions * kInstrSize;
587
588
589   // ---------------------------------------------------------------------------
590   // Code generation.
591
592   // Insert the smallest number of nop instructions
593   // possible to align the pc offset to a multiple
594   // of m. m must be a power of 2 (>= 4).
595   void Align(int m);
596   // Insert the smallest number of zero bytes possible to align the pc offset
597   // to a mulitple of m. m must be a power of 2 (>= 2).
598   void DataAlign(int m);
599   // Aligns code to something that's optimal for a jump target for the platform.
600   void CodeTargetAlign();
601
602   // Different nop operations are used by the code generator to detect certain
603   // states of the generated code.
604   enum NopMarkerTypes {
605     NON_MARKING_NOP = 0,
606     DEBUG_BREAK_NOP,
607     // IC markers.
608     PROPERTY_ACCESS_INLINED,
609     PROPERTY_ACCESS_INLINED_CONTEXT,
610     PROPERTY_ACCESS_INLINED_CONTEXT_DONT_DELETE,
611     // Helper values.
612     LAST_CODE_MARKER,
613     FIRST_IC_MARKER = PROPERTY_ACCESS_INLINED,
614     // Code aging
615     CODE_AGE_MARKER_NOP = 6,
616     CODE_AGE_SEQUENCE_NOP
617   };
618
619   // Type == 0 is the default non-marking nop. For mips this is a
620   // sll(zero_reg, zero_reg, 0). We use rt_reg == at for non-zero
621   // marking, to avoid conflict with ssnop and ehb instructions.
622   void nop(unsigned int type = 0) {
623     DCHECK(type < 32);
624     Register nop_rt_reg = (type == 0) ? zero_reg : at;
625     sll(zero_reg, nop_rt_reg, type, true);
626   }
627
628
629   // --------Branch-and-jump-instructions----------
630   // We don't use likely variant of instructions.
631   void b(int16_t offset);
632   void b(Label* L) { b(branch_offset(L, false)>>2); }
633   void bal(int16_t offset);
634   void bal(Label* L) { bal(branch_offset(L, false)>>2); }
635   void bc(int32_t offset);
636   void bc(Label* L) { bc(branch_offset(L, false) >> 2); }
637   void balc(int32_t offset);
638   void balc(Label* L) { balc(branch_offset(L, false) >> 2); }
639
640   void beq(Register rs, Register rt, int16_t offset);
641   void beq(Register rs, Register rt, Label* L) {
642     beq(rs, rt, branch_offset(L, false) >> 2);
643   }
644   void bgez(Register rs, int16_t offset);
645   void bgezc(Register rt, int16_t offset);
646   void bgezc(Register rt, Label* L) {
647     bgezc(rt, branch_offset_compact(L, false)>>2);
648   }
649   void bgeuc(Register rs, Register rt, int16_t offset);
650   void bgeuc(Register rs, Register rt, Label* L) {
651     bgeuc(rs, rt, branch_offset_compact(L, false)>>2);
652   }
653   void bgec(Register rs, Register rt, int16_t offset);
654   void bgec(Register rs, Register rt, Label* L) {
655     bgec(rs, rt, branch_offset_compact(L, false)>>2);
656   }
657   void bgezal(Register rs, int16_t offset);
658   void bgezalc(Register rt, int16_t offset);
659   void bgezalc(Register rt, Label* L) {
660     bgezalc(rt, branch_offset_compact(L, false)>>2);
661   }
662   void bgezall(Register rs, int16_t offset);
663   void bgezall(Register rs, Label* L) {
664     bgezall(rs, branch_offset(L, false)>>2);
665   }
666   void bgtz(Register rs, int16_t offset);
667   void bgtzc(Register rt, int16_t offset);
668   void bgtzc(Register rt, Label* L) {
669     bgtzc(rt, branch_offset_compact(L, false)>>2);
670   }
671   void blez(Register rs, int16_t offset);
672   void blezc(Register rt, int16_t offset);
673   void blezc(Register rt, Label* L) {
674     blezc(rt, branch_offset_compact(L, false)>>2);
675   }
676   void bltz(Register rs, int16_t offset);
677   void bltzc(Register rt, int16_t offset);
678   void bltzc(Register rt, Label* L) {
679     bltzc(rt, branch_offset_compact(L, false)>>2);
680   }
681   void bltuc(Register rs, Register rt, int16_t offset);
682   void bltuc(Register rs, Register rt, Label* L) {
683     bltuc(rs, rt, branch_offset_compact(L, false)>>2);
684   }
685   void bltc(Register rs, Register rt, int16_t offset);
686   void bltc(Register rs, Register rt, Label* L) {
687     bltc(rs, rt, branch_offset_compact(L, false)>>2);
688   }
689   void bltzal(Register rs, int16_t offset);
690   void blezalc(Register rt, int16_t offset);
691   void blezalc(Register rt, Label* L) {
692     blezalc(rt, branch_offset_compact(L, false)>>2);
693   }
694   void bltzalc(Register rt, int16_t offset);
695   void bltzalc(Register rt, Label* L) {
696     bltzalc(rt, branch_offset_compact(L, false)>>2);
697   }
698   void bgtzalc(Register rt, int16_t offset);
699   void bgtzalc(Register rt, Label* L) {
700     bgtzalc(rt, branch_offset_compact(L, false)>>2);
701   }
702   void beqzalc(Register rt, int16_t offset);
703   void beqzalc(Register rt, Label* L) {
704     beqzalc(rt, branch_offset_compact(L, false)>>2);
705   }
706   void beqc(Register rs, Register rt, int16_t offset);
707   void beqc(Register rs, Register rt, Label* L) {
708     beqc(rs, rt, branch_offset_compact(L, false)>>2);
709   }
710   void beqzc(Register rs, int32_t offset);
711   void beqzc(Register rs, Label* L) {
712     beqzc(rs, branch_offset21_compact(L, false)>>2);
713   }
714   void bnezalc(Register rt, int16_t offset);
715   void bnezalc(Register rt, Label* L) {
716     bnezalc(rt, branch_offset_compact(L, false)>>2);
717   }
718   void bnec(Register rs, Register rt, int16_t offset);
719   void bnec(Register rs, Register rt, Label* L) {
720     bnec(rs, rt, branch_offset_compact(L, false)>>2);
721   }
722   void bnezc(Register rt, int32_t offset);
723   void bnezc(Register rt, Label* L) {
724     bnezc(rt, branch_offset21_compact(L, false)>>2);
725   }
726   void bne(Register rs, Register rt, int16_t offset);
727   void bne(Register rs, Register rt, Label* L) {
728     bne(rs, rt, branch_offset(L, false)>>2);
729   }
730   void bovc(Register rs, Register rt, int16_t offset);
731   void bovc(Register rs, Register rt, Label* L) {
732     bovc(rs, rt, branch_offset_compact(L, false)>>2);
733   }
734   void bnvc(Register rs, Register rt, int16_t offset);
735   void bnvc(Register rs, Register rt, Label* L) {
736     bnvc(rs, rt, branch_offset_compact(L, false)>>2);
737   }
738
739   // Never use the int16_t b(l)cond version with a branch offset
740   // instead of using the Label* version.
741
742   // Jump targets must be in the current 256 MB-aligned region. i.e. 28 bits.
743   void j(int32_t target);
744   void jal(int32_t target);
745   void jalr(Register rs, Register rd = ra);
746   void jr(Register target);
747   void jic(Register rt, int16_t offset);
748   void jialc(Register rt, int16_t offset);
749
750
751   // -------Data-processing-instructions---------
752
753   // Arithmetic.
754   void addu(Register rd, Register rs, Register rt);
755   void subu(Register rd, Register rs, Register rt);
756   void mult(Register rs, Register rt);
757   void multu(Register rs, Register rt);
758   void div(Register rs, Register rt);
759   void divu(Register rs, Register rt);
760   void div(Register rd, Register rs, Register rt);
761   void divu(Register rd, Register rs, Register rt);
762   void mod(Register rd, Register rs, Register rt);
763   void modu(Register rd, Register rs, Register rt);
764   void mul(Register rd, Register rs, Register rt);
765   void muh(Register rd, Register rs, Register rt);
766   void mulu(Register rd, Register rs, Register rt);
767   void muhu(Register rd, Register rs, Register rt);
768
769   void addiu(Register rd, Register rs, int32_t j);
770
771   // Logical.
772   void and_(Register rd, Register rs, Register rt);
773   void or_(Register rd, Register rs, Register rt);
774   void xor_(Register rd, Register rs, Register rt);
775   void nor(Register rd, Register rs, Register rt);
776
777   void andi(Register rd, Register rs, int32_t j);
778   void ori(Register rd, Register rs, int32_t j);
779   void xori(Register rd, Register rs, int32_t j);
780   void lui(Register rd, int32_t j);
781   void aui(Register rs, Register rt, int32_t j);
782
783   // Shifts.
784   // Please note: sll(zero_reg, zero_reg, x) instructions are reserved as nop
785   // and may cause problems in normal code. coming_from_nop makes sure this
786   // doesn't happen.
787   void sll(Register rd, Register rt, uint16_t sa, bool coming_from_nop = false);
788   void sllv(Register rd, Register rt, Register rs);
789   void srl(Register rd, Register rt, uint16_t sa);
790   void srlv(Register rd, Register rt, Register rs);
791   void sra(Register rt, Register rd, uint16_t sa);
792   void srav(Register rt, Register rd, Register rs);
793   void rotr(Register rd, Register rt, uint16_t sa);
794   void rotrv(Register rd, Register rt, Register rs);
795
796
797   // ------------Memory-instructions-------------
798
799   void lb(Register rd, const MemOperand& rs);
800   void lbu(Register rd, const MemOperand& rs);
801   void lh(Register rd, const MemOperand& rs);
802   void lhu(Register rd, const MemOperand& rs);
803   void lw(Register rd, const MemOperand& rs);
804   void lwl(Register rd, const MemOperand& rs);
805   void lwr(Register rd, const MemOperand& rs);
806   void sb(Register rd, const MemOperand& rs);
807   void sh(Register rd, const MemOperand& rs);
808   void sw(Register rd, const MemOperand& rs);
809   void swl(Register rd, const MemOperand& rs);
810   void swr(Register rd, const MemOperand& rs);
811
812
813   // ---------PC-Relative-instructions-----------
814
815   void addiupc(Register rs, int32_t imm19);
816   void lwpc(Register rs, int32_t offset19);
817   void auipc(Register rs, int16_t imm16);
818   void aluipc(Register rs, int16_t imm16);
819
820
821   // ----------------Prefetch--------------------
822
823   void pref(int32_t hint, const MemOperand& rs);
824
825
826   // -------------Misc-instructions--------------
827
828   // Break / Trap instructions.
829   void break_(uint32_t code, bool break_as_stop = false);
830   void stop(const char* msg, uint32_t code = kMaxStopCode);
831   void tge(Register rs, Register rt, uint16_t code);
832   void tgeu(Register rs, Register rt, uint16_t code);
833   void tlt(Register rs, Register rt, uint16_t code);
834   void tltu(Register rs, Register rt, uint16_t code);
835   void teq(Register rs, Register rt, uint16_t code);
836   void tne(Register rs, Register rt, uint16_t code);
837
838   // Move from HI/LO register.
839   void mfhi(Register rd);
840   void mflo(Register rd);
841
842   // Set on less than.
843   void slt(Register rd, Register rs, Register rt);
844   void sltu(Register rd, Register rs, Register rt);
845   void slti(Register rd, Register rs, int32_t j);
846   void sltiu(Register rd, Register rs, int32_t j);
847
848   // Conditional move.
849   void movz(Register rd, Register rs, Register rt);
850   void movn(Register rd, Register rs, Register rt);
851   void movt(Register rd, Register rs, uint16_t cc = 0);
852   void movf(Register rd, Register rs, uint16_t cc = 0);
853
854   void sel(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
855   void sel_s(FPURegister fd, FPURegister fs, FPURegister ft);
856   void sel_d(FPURegister fd, FPURegister fs, FPURegister ft);
857   void seleqz(Register rd, Register rs, Register rt);
858   void seleqz(SecondaryField fmt, FPURegister fd, FPURegister fs,
859               FPURegister ft);
860   void selnez(Register rd, Register rs, Register rt);
861   void selnez(SecondaryField fmt, FPURegister fd, FPURegister fs,
862               FPURegister ft);
863   void seleqz_d(FPURegister fd, FPURegister fs, FPURegister ft);
864   void seleqz_s(FPURegister fd, FPURegister fs, FPURegister ft);
865   void selnez_d(FPURegister fd, FPURegister fs, FPURegister ft);
866   void selnez_s(FPURegister fd, FPURegister fs, FPURegister ft);
867
868   void movz_s(FPURegister fd, FPURegister fs, Register rt);
869   void movz_d(FPURegister fd, FPURegister fs, Register rt);
870   void movt_s(FPURegister fd, FPURegister fs, uint16_t cc);
871   void movt_d(FPURegister fd, FPURegister fs, uint16_t cc);
872   void movf_s(FPURegister fd, FPURegister fs, uint16_t cc);
873   void movf_d(FPURegister fd, FPURegister fs, uint16_t cc);
874   void movn_s(FPURegister fd, FPURegister fs, Register rt);
875   void movn_d(FPURegister fd, FPURegister fs, Register rt);
876   // Bit twiddling.
877   void clz(Register rd, Register rs);
878   void ins_(Register rt, Register rs, uint16_t pos, uint16_t size);
879   void ext_(Register rt, Register rs, uint16_t pos, uint16_t size);
880   void bitswap(Register rd, Register rt);
881   void align(Register rd, Register rs, Register rt, uint8_t bp);
882
883   // --------Coprocessor-instructions----------------
884
885   // Load, store, and move.
886   void lwc1(FPURegister fd, const MemOperand& src);
887   void ldc1(FPURegister fd, const MemOperand& src);
888
889   void swc1(FPURegister fs, const MemOperand& dst);
890   void sdc1(FPURegister fs, const MemOperand& dst);
891
892   void mtc1(Register rt, FPURegister fs);
893   void mthc1(Register rt, FPURegister fs);
894
895   void mfc1(Register rt, FPURegister fs);
896   void mfhc1(Register rt, FPURegister fs);
897
898   void ctc1(Register rt, FPUControlRegister fs);
899   void cfc1(Register rt, FPUControlRegister fs);
900
901   // Arithmetic.
902   void add_s(FPURegister fd, FPURegister fs, FPURegister ft);
903   void add_d(FPURegister fd, FPURegister fs, FPURegister ft);
904   void sub_s(FPURegister fd, FPURegister fs, FPURegister ft);
905   void sub_d(FPURegister fd, FPURegister fs, FPURegister ft);
906   void mul_s(FPURegister fd, FPURegister fs, FPURegister ft);
907   void mul_d(FPURegister fd, FPURegister fs, FPURegister ft);
908   void madd_d(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft);
909   void div_s(FPURegister fd, FPURegister fs, FPURegister ft);
910   void div_d(FPURegister fd, FPURegister fs, FPURegister ft);
911   void abs_s(FPURegister fd, FPURegister fs);
912   void abs_d(FPURegister fd, FPURegister fs);
913   void mov_d(FPURegister fd, FPURegister fs);
914   void mov_s(FPURegister fd, FPURegister fs);
915   void neg_s(FPURegister fd, FPURegister fs);
916   void neg_d(FPURegister fd, FPURegister fs);
917   void sqrt_s(FPURegister fd, FPURegister fs);
918   void sqrt_d(FPURegister fd, FPURegister fs);
919   void rsqrt_s(FPURegister fd, FPURegister fs);
920   void rsqrt_d(FPURegister fd, FPURegister fs);
921   void recip_d(FPURegister fd, FPURegister fs);
922   void recip_s(FPURegister fd, FPURegister fs);
923
924   // Conversion.
925   void cvt_w_s(FPURegister fd, FPURegister fs);
926   void cvt_w_d(FPURegister fd, FPURegister fs);
927   void trunc_w_s(FPURegister fd, FPURegister fs);
928   void trunc_w_d(FPURegister fd, FPURegister fs);
929   void round_w_s(FPURegister fd, FPURegister fs);
930   void round_w_d(FPURegister fd, FPURegister fs);
931   void floor_w_s(FPURegister fd, FPURegister fs);
932   void floor_w_d(FPURegister fd, FPURegister fs);
933   void ceil_w_s(FPURegister fd, FPURegister fs);
934   void ceil_w_d(FPURegister fd, FPURegister fs);
935   void rint_s(FPURegister fd, FPURegister fs);
936   void rint_d(FPURegister fd, FPURegister fs);
937   void rint(SecondaryField fmt, FPURegister fd, FPURegister fs);
938
939   void cvt_l_s(FPURegister fd, FPURegister fs);
940   void cvt_l_d(FPURegister fd, FPURegister fs);
941   void trunc_l_s(FPURegister fd, FPURegister fs);
942   void trunc_l_d(FPURegister fd, FPURegister fs);
943   void round_l_s(FPURegister fd, FPURegister fs);
944   void round_l_d(FPURegister fd, FPURegister fs);
945   void floor_l_s(FPURegister fd, FPURegister fs);
946   void floor_l_d(FPURegister fd, FPURegister fs);
947   void ceil_l_s(FPURegister fd, FPURegister fs);
948   void ceil_l_d(FPURegister fd, FPURegister fs);
949
950   void class_s(FPURegister fd, FPURegister fs);
951   void class_d(FPURegister fd, FPURegister fs);
952
953   void min(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
954   void mina(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
955   void max(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
956   void maxa(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
957   void min_s(FPURegister fd, FPURegister fs, FPURegister ft);
958   void min_d(FPURegister fd, FPURegister fs, FPURegister ft);
959   void max_s(FPURegister fd, FPURegister fs, FPURegister ft);
960   void max_d(FPURegister fd, FPURegister fs, FPURegister ft);
961   void mina_s(FPURegister fd, FPURegister fs, FPURegister ft);
962   void mina_d(FPURegister fd, FPURegister fs, FPURegister ft);
963   void maxa_s(FPURegister fd, FPURegister fs, FPURegister ft);
964   void maxa_d(FPURegister fd, FPURegister fs, FPURegister ft);
965
966   void cvt_s_w(FPURegister fd, FPURegister fs);
967   void cvt_s_l(FPURegister fd, FPURegister fs);
968   void cvt_s_d(FPURegister fd, FPURegister fs);
969
970   void cvt_d_w(FPURegister fd, FPURegister fs);
971   void cvt_d_l(FPURegister fd, FPURegister fs);
972   void cvt_d_s(FPURegister fd, FPURegister fs);
973
974   // Conditions and branches for MIPSr6.
975   void cmp(FPUCondition cond, SecondaryField fmt,
976          FPURegister fd, FPURegister ft, FPURegister fs);
977   void cmp_s(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft);
978   void cmp_d(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft);
979
980   void bc1eqz(int16_t offset, FPURegister ft);
981   void bc1eqz(Label* L, FPURegister ft) {
982     bc1eqz(branch_offset(L, false)>>2, ft);
983   }
984   void bc1nez(int16_t offset, FPURegister ft);
985   void bc1nez(Label* L, FPURegister ft) {
986     bc1nez(branch_offset(L, false)>>2, ft);
987   }
988
989   // Conditions and branches for non MIPSr6.
990   void c(FPUCondition cond, SecondaryField fmt,
991          FPURegister ft, FPURegister fs, uint16_t cc = 0);
992   void c_s(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0);
993   void c_d(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0);
994
995   void bc1f(int16_t offset, uint16_t cc = 0);
996   void bc1f(Label* L, uint16_t cc = 0) { bc1f(branch_offset(L, false)>>2, cc); }
997   void bc1t(int16_t offset, uint16_t cc = 0);
998   void bc1t(Label* L, uint16_t cc = 0) { bc1t(branch_offset(L, false)>>2, cc); }
999   void fcmp(FPURegister src1, const double src2, FPUCondition cond);
1000
1001   // Check the code size generated from label to here.
1002   int SizeOfCodeGeneratedSince(Label* label) {
1003     return pc_offset() - label->pos();
1004   }
1005
1006   // Check the number of instructions generated from label to here.
1007   int InstructionsGeneratedSince(Label* label) {
1008     return SizeOfCodeGeneratedSince(label) / kInstrSize;
1009   }
1010
1011   // Class for scoping postponing the trampoline pool generation.
1012   class BlockTrampolinePoolScope {
1013    public:
1014     explicit BlockTrampolinePoolScope(Assembler* assem) : assem_(assem) {
1015       assem_->StartBlockTrampolinePool();
1016     }
1017     ~BlockTrampolinePoolScope() {
1018       assem_->EndBlockTrampolinePool();
1019     }
1020
1021    private:
1022     Assembler* assem_;
1023
1024     DISALLOW_IMPLICIT_CONSTRUCTORS(BlockTrampolinePoolScope);
1025   };
1026
1027   // Class for postponing the assembly buffer growth. Typically used for
1028   // sequences of instructions that must be emitted as a unit, before
1029   // buffer growth (and relocation) can occur.
1030   // This blocking scope is not nestable.
1031   class BlockGrowBufferScope {
1032    public:
1033     explicit BlockGrowBufferScope(Assembler* assem) : assem_(assem) {
1034       assem_->StartBlockGrowBuffer();
1035     }
1036     ~BlockGrowBufferScope() {
1037       assem_->EndBlockGrowBuffer();
1038     }
1039
1040    private:
1041     Assembler* assem_;
1042
1043     DISALLOW_IMPLICIT_CONSTRUCTORS(BlockGrowBufferScope);
1044   };
1045
1046   // Debugging.
1047
1048   // Mark generator continuation.
1049   void RecordGeneratorContinuation();
1050
1051   // Mark address of a debug break slot.
1052   void RecordDebugBreakSlot(RelocInfo::Mode mode, int argc = 0);
1053
1054   // Record the AST id of the CallIC being compiled, so that it can be placed
1055   // in the relocation information.
1056   void SetRecordedAstId(TypeFeedbackId ast_id) {
1057     DCHECK(recorded_ast_id_.IsNone());
1058     recorded_ast_id_ = ast_id;
1059   }
1060
1061   TypeFeedbackId RecordedAstId() {
1062     DCHECK(!recorded_ast_id_.IsNone());
1063     return recorded_ast_id_;
1064   }
1065
1066   void ClearRecordedAstId() { recorded_ast_id_ = TypeFeedbackId::None(); }
1067
1068   // Record a comment relocation entry that can be used by a disassembler.
1069   // Use --code-comments to enable.
1070   void RecordComment(const char* msg);
1071
1072   // Record a deoptimization reason that can be used by a log or cpu profiler.
1073   // Use --trace-deopt to enable.
1074   void RecordDeoptReason(const int reason, const SourcePosition position);
1075
1076
1077   static int RelocateInternalReference(RelocInfo::Mode rmode, byte* pc,
1078                                        intptr_t pc_delta);
1079
1080   // Writes a single byte or word of data in the code stream.  Used for
1081   // inline tables, e.g., jump-tables.
1082   void db(uint8_t data);
1083   void dd(uint32_t data);
1084   void dq(uint64_t data);
1085   void dp(uintptr_t data) { dd(data); }
1086   void dd(Label* label);
1087
1088   // Emits the address of the code stub's first instruction.
1089   void emit_code_stub_address(Code* stub);
1090
1091   PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1092
1093   // Postpone the generation of the trampoline pool for the specified number of
1094   // instructions.
1095   void BlockTrampolinePoolFor(int instructions);
1096
1097   // Check if there is less than kGap bytes available in the buffer.
1098   // If this is the case, we need to grow the buffer before emitting
1099   // an instruction or relocation information.
1100   inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
1101
1102   // Get the number of bytes available in the buffer.
1103   inline int available_space() const { return reloc_info_writer.pos() - pc_; }
1104
1105   // Read/patch instructions.
1106   static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
1107   static void instr_at_put(byte* pc, Instr instr) {
1108     *reinterpret_cast<Instr*>(pc) = instr;
1109   }
1110   Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
1111   void instr_at_put(int pos, Instr instr) {
1112     *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
1113   }
1114
1115   // Check if an instruction is a branch of some kind.
1116   static bool IsBranch(Instr instr);
1117   static bool IsBeq(Instr instr);
1118   static bool IsBne(Instr instr);
1119
1120   static bool IsJump(Instr instr);
1121   static bool IsJ(Instr instr);
1122   static bool IsLui(Instr instr);
1123   static bool IsOri(Instr instr);
1124
1125   static bool IsJal(Instr instr);
1126   static bool IsJr(Instr instr);
1127   static bool IsJalr(Instr instr);
1128
1129   static bool IsNop(Instr instr, unsigned int type);
1130   static bool IsPop(Instr instr);
1131   static bool IsPush(Instr instr);
1132   static bool IsLwRegFpOffset(Instr instr);
1133   static bool IsSwRegFpOffset(Instr instr);
1134   static bool IsLwRegFpNegOffset(Instr instr);
1135   static bool IsSwRegFpNegOffset(Instr instr);
1136
1137   static Register GetRtReg(Instr instr);
1138   static Register GetRsReg(Instr instr);
1139   static Register GetRdReg(Instr instr);
1140
1141   static uint32_t GetRt(Instr instr);
1142   static uint32_t GetRtField(Instr instr);
1143   static uint32_t GetRs(Instr instr);
1144   static uint32_t GetRsField(Instr instr);
1145   static uint32_t GetRd(Instr instr);
1146   static uint32_t GetRdField(Instr instr);
1147   static uint32_t GetSa(Instr instr);
1148   static uint32_t GetSaField(Instr instr);
1149   static uint32_t GetOpcodeField(Instr instr);
1150   static uint32_t GetFunction(Instr instr);
1151   static uint32_t GetFunctionField(Instr instr);
1152   static uint32_t GetImmediate16(Instr instr);
1153   static uint32_t GetLabelConst(Instr instr);
1154
1155   static int32_t GetBranchOffset(Instr instr);
1156   static bool IsLw(Instr instr);
1157   static int16_t GetLwOffset(Instr instr);
1158   static Instr SetLwOffset(Instr instr, int16_t offset);
1159
1160   static bool IsSw(Instr instr);
1161   static Instr SetSwOffset(Instr instr, int16_t offset);
1162   static bool IsAddImmediate(Instr instr);
1163   static Instr SetAddImmediateOffset(Instr instr, int16_t offset);
1164
1165   static bool IsAndImmediate(Instr instr);
1166   static bool IsEmittedConstant(Instr instr);
1167
1168   void CheckTrampolinePool();
1169
1170   void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
1171                                           ConstantPoolEntry::Access access,
1172                                           ConstantPoolEntry::Type type) {
1173     // No embedded constant pool support.
1174     UNREACHABLE();
1175   }
1176
1177  protected:
1178   // Relocation for a type-recording IC has the AST id added to it.  This
1179   // member variable is a way to pass the information from the call site to
1180   // the relocation info.
1181   TypeFeedbackId recorded_ast_id_;
1182
1183   int32_t buffer_space() const { return reloc_info_writer.pos() - pc_; }
1184
1185   // Decode branch instruction at pos and return branch target pos.
1186   int target_at(int pos, bool is_internal);
1187
1188   // Patch branch instruction at pos to branch to given branch target pos.
1189   void target_at_put(int pos, int target_pos, bool is_internal);
1190
1191   // Say if we need to relocate with this mode.
1192   bool MustUseReg(RelocInfo::Mode rmode);
1193
1194   // Record reloc info for current pc_.
1195   void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1196
1197   // Block the emission of the trampoline pool before pc_offset.
1198   void BlockTrampolinePoolBefore(int pc_offset) {
1199     if (no_trampoline_pool_before_ < pc_offset)
1200       no_trampoline_pool_before_ = pc_offset;
1201   }
1202
1203   void StartBlockTrampolinePool() {
1204     trampoline_pool_blocked_nesting_++;
1205   }
1206
1207   void EndBlockTrampolinePool() {
1208     trampoline_pool_blocked_nesting_--;
1209   }
1210
1211   bool is_trampoline_pool_blocked() const {
1212     return trampoline_pool_blocked_nesting_ > 0;
1213   }
1214
1215   bool has_exception() const {
1216     return internal_trampoline_exception_;
1217   }
1218
1219   void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi);
1220
1221   bool is_trampoline_emitted() const {
1222     return trampoline_emitted_;
1223   }
1224
1225   // Temporarily block automatic assembly buffer growth.
1226   void StartBlockGrowBuffer() {
1227     DCHECK(!block_buffer_growth_);
1228     block_buffer_growth_ = true;
1229   }
1230
1231   void EndBlockGrowBuffer() {
1232     DCHECK(block_buffer_growth_);
1233     block_buffer_growth_ = false;
1234   }
1235
1236   bool is_buffer_growth_blocked() const {
1237     return block_buffer_growth_;
1238   }
1239
1240  private:
1241   inline static void set_target_internal_reference_encoded_at(Address pc,
1242                                                               Address target);
1243
1244   // Buffer size and constant pool distance are checked together at regular
1245   // intervals of kBufferCheckInterval emitted bytes.
1246   static const int kBufferCheckInterval = 1*KB/2;
1247
1248   // Code generation.
1249   // The relocation writer's position is at least kGap bytes below the end of
1250   // the generated instructions. This is so that multi-instruction sequences do
1251   // not have to check for overflow. The same is true for writes of large
1252   // relocation info entries.
1253   static const int kGap = 32;
1254
1255
1256   // Repeated checking whether the trampoline pool should be emitted is rather
1257   // expensive. By default we only check again once a number of instructions
1258   // has been generated.
1259   static const int kCheckConstIntervalInst = 32;
1260   static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize;
1261
1262   int next_buffer_check_;  // pc offset of next buffer check.
1263
1264   // Emission of the trampoline pool may be blocked in some code sequences.
1265   int trampoline_pool_blocked_nesting_;  // Block emission if this is not zero.
1266   int no_trampoline_pool_before_;  // Block emission before this pc offset.
1267
1268   // Keep track of the last emitted pool to guarantee a maximal distance.
1269   int last_trampoline_pool_end_;  // pc offset of the end of the last pool.
1270
1271   // Automatic growth of the assembly buffer may be blocked for some sequences.
1272   bool block_buffer_growth_;  // Block growth when true.
1273
1274   // Relocation information generation.
1275   // Each relocation is encoded as a variable size value.
1276   static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
1277   RelocInfoWriter reloc_info_writer;
1278
1279   // The bound position, before this we cannot do instruction elimination.
1280   int last_bound_pos_;
1281
1282   // Code emission.
1283   inline void CheckBuffer();
1284   void GrowBuffer();
1285   inline void emit(Instr x);
1286   inline void CheckTrampolinePoolQuick(int extra_instructions = 0);
1287
1288   // Instruction generation.
1289   // We have 3 different kind of encoding layout on MIPS.
1290   // However due to many different types of objects encoded in the same fields
1291   // we have quite a few aliases for each mode.
1292   // Using the same structure to refer to Register and FPURegister would spare a
1293   // few aliases, but mixing both does not look clean to me.
1294   // Anyway we could surely implement this differently.
1295
1296   void GenInstrRegister(Opcode opcode,
1297                         Register rs,
1298                         Register rt,
1299                         Register rd,
1300                         uint16_t sa = 0,
1301                         SecondaryField func = NULLSF);
1302
1303   void GenInstrRegister(Opcode opcode,
1304                         Register rs,
1305                         Register rt,
1306                         uint16_t msb,
1307                         uint16_t lsb,
1308                         SecondaryField func);
1309
1310   void GenInstrRegister(Opcode opcode,
1311                         SecondaryField fmt,
1312                         FPURegister ft,
1313                         FPURegister fs,
1314                         FPURegister fd,
1315                         SecondaryField func = NULLSF);
1316
1317   void GenInstrRegister(Opcode opcode,
1318                         FPURegister fr,
1319                         FPURegister ft,
1320                         FPURegister fs,
1321                         FPURegister fd,
1322                         SecondaryField func = NULLSF);
1323
1324   void GenInstrRegister(Opcode opcode,
1325                         SecondaryField fmt,
1326                         Register rt,
1327                         FPURegister fs,
1328                         FPURegister fd,
1329                         SecondaryField func = NULLSF);
1330
1331   void GenInstrRegister(Opcode opcode,
1332                         SecondaryField fmt,
1333                         Register rt,
1334                         FPUControlRegister fs,
1335                         SecondaryField func = NULLSF);
1336
1337
1338   void GenInstrImmediate(Opcode opcode,
1339                          Register rs,
1340                          Register rt,
1341                          int32_t  j);
1342   void GenInstrImmediate(Opcode opcode,
1343                          Register rs,
1344                          SecondaryField SF,
1345                          int32_t  j);
1346   void GenInstrImmediate(Opcode opcode,
1347                          Register r1,
1348                          FPURegister r2,
1349                          int32_t  j);
1350   void GenInstrImmediate(Opcode opcode, Register rs, int32_t j);
1351   void GenInstrImmediate(Opcode opcode, int32_t offset26);
1352
1353
1354   void GenInstrJump(Opcode opcode,
1355                      uint32_t address);
1356
1357   // Helpers.
1358   void LoadRegPlusOffsetToAt(const MemOperand& src);
1359
1360   // Labels.
1361   void print(Label* L);
1362   void bind_to(Label* L, int pos);
1363   void next(Label* L, bool is_internal);
1364
1365   // One trampoline consists of:
1366   // - space for trampoline slots,
1367   // - space for labels.
1368   //
1369   // Space for trampoline slots is equal to slot_count * 2 * kInstrSize.
1370   // Space for trampoline slots preceeds space for labels. Each label is of one
1371   // instruction size, so total amount for labels is equal to
1372   // label_count *  kInstrSize.
1373   class Trampoline {
1374    public:
1375     Trampoline() {
1376       start_ = 0;
1377       next_slot_ = 0;
1378       free_slot_count_ = 0;
1379       end_ = 0;
1380     }
1381     Trampoline(int start, int slot_count) {
1382       start_ = start;
1383       next_slot_ = start;
1384       free_slot_count_ = slot_count;
1385       end_ = start + slot_count * kTrampolineSlotsSize;
1386     }
1387     int start() {
1388       return start_;
1389     }
1390     int end() {
1391       return end_;
1392     }
1393     int take_slot() {
1394       int trampoline_slot = kInvalidSlotPos;
1395       if (free_slot_count_ <= 0) {
1396         // We have run out of space on trampolines.
1397         // Make sure we fail in debug mode, so we become aware of each case
1398         // when this happens.
1399         DCHECK(0);
1400         // Internal exception will be caught.
1401       } else {
1402         trampoline_slot = next_slot_;
1403         free_slot_count_--;
1404         next_slot_ += kTrampolineSlotsSize;
1405       }
1406       return trampoline_slot;
1407     }
1408
1409    private:
1410     int start_;
1411     int end_;
1412     int next_slot_;
1413     int free_slot_count_;
1414   };
1415
1416   int32_t get_trampoline_entry(int32_t pos);
1417   int unbound_labels_count_;
1418   // If trampoline is emitted, generated code is becoming large. As this is
1419   // already a slow case which can possibly break our code generation for the
1420   // extreme case, we use this information to trigger different mode of
1421   // branch instruction generation, where we use jump instructions rather
1422   // than regular branch instructions.
1423   bool trampoline_emitted_;
1424   static const int kTrampolineSlotsSize = 4 * kInstrSize;
1425   static const int kMaxBranchOffset = (1 << (18 - 1)) - 1;
1426   static const int kInvalidSlotPos = -1;
1427
1428   // Internal reference positions, required for unbounded internal reference
1429   // labels.
1430   std::set<int> internal_reference_positions_;
1431
1432   Trampoline trampoline_;
1433   bool internal_trampoline_exception_;
1434
1435   friend class RegExpMacroAssemblerMIPS;
1436   friend class RelocInfo;
1437   friend class CodePatcher;
1438   friend class BlockTrampolinePoolScope;
1439
1440   PositionsRecorder positions_recorder_;
1441   friend class PositionsRecorder;
1442   friend class EnsureSpace;
1443 };
1444
1445
1446 class EnsureSpace BASE_EMBEDDED {
1447  public:
1448   explicit EnsureSpace(Assembler* assembler) {
1449     assembler->CheckBuffer();
1450   }
1451 };
1452
1453 } }  // namespace v8::internal
1454
1455 #endif  // V8_ARM_ASSEMBLER_MIPS_H_