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