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