blob: f93bc48837ff91c8a93fa3c2eb68ad678eda833b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
41#include <set>
42
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043#include "src/assembler.h"
44#include "src/mips64/constants-mips64.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045
46namespace v8 {
47namespace internal {
48
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049// 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
Ben Murdochc5610432016-08-08 18:44:38 +010066#define FLOAT_REGISTERS DOUBLE_REGISTERS
67
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068#define ALLOCATABLE_DOUBLE_REGISTERS(V) \
69 V(f0) V(f2) V(f4) V(f6) V(f8) V(f10) V(f12) V(f14) \
70 V(f16) V(f18) V(f20) V(f22) V(f24) V(f26)
71// clang-format on
72
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073// CPU Registers.
74//
75// 1) We would prefer to use an enum, but enum values are assignment-
76// compatible with int, which has caused code-generation bugs.
77//
78// 2) We would prefer to use a class instead of a struct but we don't like
79// the register initialization to depend on the particular initialization
80// order (which appears to be different on OS X, Linux, and Windows for the
81// installed versions of C++ we tried). Using a struct permits C-style
82// "initialization". Also, the Register objects cannot be const as this
83// forces initialization stubs in MSVC, making us dependent on initialization
84// order.
85//
86// 3) By not using an enum, we are possibly preventing the compiler from
87// doing certain constant folds, which may significantly reduce the
88// code generated for some assembly instructions (because they boil down
89// to a few constants). If this is a problem, we could change the code
90// such that we use an enum in optimized mode, and the struct in debug
91// mode. This way we get the compile-time error checking in debug mode
92// and best performance in optimized code.
93
94
95// -----------------------------------------------------------------------------
96// Implementation of Register and FPURegister.
97
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098struct Register {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 static const int kCpRegister = 23; // cp (s7) is the 23rd register.
100
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101#if defined(V8_TARGET_LITTLE_ENDIAN)
102 static const int kMantissaOffset = 0;
103 static const int kExponentOffset = 4;
104#elif defined(V8_TARGET_BIG_ENDIAN)
105 static const int kMantissaOffset = 4;
106 static const int kExponentOffset = 0;
107#else
108#error Unknown endianness
109#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 enum Code {
112#define REGISTER_CODE(R) kCode_##R,
113 GENERAL_REGISTERS(REGISTER_CODE)
114#undef REGISTER_CODE
115 kAfterLast,
116 kCode_no_reg = -1
117 };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 static const int kNumRegisters = Code::kAfterLast;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120
121 static Register from_code(int code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 DCHECK(code >= 0);
123 DCHECK(code < kNumRegisters);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 Register r = { code };
125 return r;
126 }
127
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 const char* ToString();
129 bool IsAllocatable() const;
130 bool is_valid() const { return 0 <= reg_code && reg_code < kNumRegisters; }
131 bool is(Register reg) const { return reg_code == reg.reg_code; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 int code() const {
133 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 return reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 }
136 int bit() const {
137 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 return 1 << reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 }
140
141 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 int reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143};
144
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145// s7: context register
146// s3: lithium scratch
147// s4: lithium scratch2
148#define DECLARE_REGISTER(R) const Register R = {Register::kCode_##R};
149GENERAL_REGISTERS(DECLARE_REGISTER)
150#undef DECLARE_REGISTER
151const Register no_reg = {Register::kCode_no_reg};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152
153
154int ToNumber(Register reg);
155
156Register ToRegister(int num);
157
158// Coprocessor register.
Ben Murdochc5610432016-08-08 18:44:38 +0100159struct FPURegister {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160 enum Code {
161#define REGISTER_CODE(R) kCode_##R,
162 DOUBLE_REGISTERS(REGISTER_CODE)
163#undef REGISTER_CODE
164 kAfterLast,
165 kCode_no_reg = -1
166 };
167
168 static const int kMaxNumRegisters = Code::kAfterLast;
169
170 inline static int NumRegisters();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171
172 // TODO(plind): Warning, inconsistent numbering here. kNumFPURegisters refers
173 // to number of 32-bit FPU regs, but kNumAllocatableRegisters refers to
174 // number of Double regs (64-bit regs, or FPU-reg-pairs).
175
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 const char* ToString();
177 bool IsAllocatable() const;
178 bool is_valid() const { return 0 <= reg_code && reg_code < kMaxNumRegisters; }
Ben Murdochc5610432016-08-08 18:44:38 +0100179 bool is(FPURegister reg) const { return reg_code == reg.reg_code; }
180 FPURegister low() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 // TODO(plind): Create DCHECK for FR=0 mode. This usage suspect for FR=1.
182 // Find low reg of a Double-reg pair, which is the reg itself.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 DCHECK(reg_code % 2 == 0); // Specified Double reg must be even.
Ben Murdochc5610432016-08-08 18:44:38 +0100184 FPURegister reg;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000185 reg.reg_code = reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 DCHECK(reg.is_valid());
187 return reg;
188 }
Ben Murdochc5610432016-08-08 18:44:38 +0100189 FPURegister high() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 // TODO(plind): Create DCHECK for FR=0 mode. This usage illegal in FR=1.
191 // Find high reg of a Doubel-reg pair, which is reg + 1.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 DCHECK(reg_code % 2 == 0); // Specified Double reg must be even.
Ben Murdochc5610432016-08-08 18:44:38 +0100193 FPURegister reg;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194 reg.reg_code = reg_code + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195 DCHECK(reg.is_valid());
196 return reg;
197 }
198
199 int code() const {
200 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201 return reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 }
203 int bit() const {
204 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205 return 1 << reg_code;
206 }
207
Ben Murdochc5610432016-08-08 18:44:38 +0100208 static FPURegister from_code(int code) {
209 FPURegister r = {code};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000210 return r;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 }
212 void setcode(int f) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 reg_code = f;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 DCHECK(is_valid());
215 }
216 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217 int reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218};
219
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220// A few double registers are reserved: one as a scratch register and one to
221// hold 0.0.
222// f28: 0.0
223// f30: scratch register.
224
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225// V8 now supports the O32 ABI, and the FPU Registers are organized as 32
226// 32-bit registers, f0 through f31. When used as 'double' they are used
227// in pairs, starting with the even numbered register. So a double operation
228// on f0 really uses f0 and f1.
229// (Modern mips hardware also supports 32 64-bit registers, via setting
230// (privileged) Status Register FR bit to 1. This is used by the N32 ABI,
231// but it is not in common use. Someday we will want to support this in v8.)
232
233// For O32 ABI, Floats and Doubles refer to same set of 32 32-bit registers.
Ben Murdochc5610432016-08-08 18:44:38 +0100234typedef FPURegister FloatRegister;
235
236typedef FPURegister DoubleRegister;
237
238// TODO(mips64) Define SIMD registers.
239typedef FPURegister Simd128Register;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241const DoubleRegister no_freg = {-1};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000243const DoubleRegister f0 = {0}; // Return value in hard float mode.
244const DoubleRegister f1 = {1};
245const DoubleRegister f2 = {2};
246const DoubleRegister f3 = {3};
247const DoubleRegister f4 = {4};
248const DoubleRegister f5 = {5};
249const DoubleRegister f6 = {6};
250const DoubleRegister f7 = {7};
251const DoubleRegister f8 = {8};
252const DoubleRegister f9 = {9};
253const DoubleRegister f10 = {10};
254const DoubleRegister f11 = {11};
255const DoubleRegister f12 = {12}; // Arg 0 in hard float mode.
256const DoubleRegister f13 = {13};
257const DoubleRegister f14 = {14}; // Arg 1 in hard float mode.
258const DoubleRegister f15 = {15};
259const DoubleRegister f16 = {16};
260const DoubleRegister f17 = {17};
261const DoubleRegister f18 = {18};
262const DoubleRegister f19 = {19};
263const DoubleRegister f20 = {20};
264const DoubleRegister f21 = {21};
265const DoubleRegister f22 = {22};
266const DoubleRegister f23 = {23};
267const DoubleRegister f24 = {24};
268const DoubleRegister f25 = {25};
269const DoubleRegister f26 = {26};
270const DoubleRegister f27 = {27};
271const DoubleRegister f28 = {28};
272const DoubleRegister f29 = {29};
273const DoubleRegister f30 = {30};
274const DoubleRegister f31 = {31};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275
276// Register aliases.
277// cp is assumed to be a callee saved register.
278// Defined using #define instead of "static const Register&" because Clang
279// complains otherwise when a compilation unit that includes this header
280// doesn't use the variables.
281#define kRootRegister s6
282#define cp s7
283#define kLithiumScratchReg s3
284#define kLithiumScratchReg2 s4
285#define kLithiumScratchDouble f30
286#define kDoubleRegZero f28
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287// Used on mips64r6 for compare operations.
288// We use the last non-callee saved odd register for N64 ABI
289#define kDoubleCompareReg f23
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290
291// FPU (coprocessor 1) control registers.
292// Currently only FCSR (#31) is implemented.
293struct FPUControlRegister {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294 bool is_valid() const { return reg_code == kFCSRRegister; }
295 bool is(FPUControlRegister creg) const { return reg_code == creg.reg_code; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 int code() const {
297 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 return reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 }
300 int bit() const {
301 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 return 1 << reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303 }
304 void setcode(int f) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000305 reg_code = f;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 DCHECK(is_valid());
307 }
308 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 int reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310};
311
312const FPUControlRegister no_fpucreg = { kInvalidFPUControlRegister };
313const FPUControlRegister FCSR = { kFCSRRegister };
314
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315// -----------------------------------------------------------------------------
316// Machine instruction Operands.
317const int kSmiShift = kSmiTagSize + kSmiShiftSize;
318const uint64_t kSmiShiftMask = (1UL << kSmiShift) - 1;
319// Class Operand represents a shifter operand in data processing instructions.
320class Operand BASE_EMBEDDED {
321 public:
322 // Immediate.
323 INLINE(explicit Operand(int64_t immediate,
324 RelocInfo::Mode rmode = RelocInfo::NONE64));
325 INLINE(explicit Operand(const ExternalReference& f));
326 INLINE(explicit Operand(const char* s));
327 INLINE(explicit Operand(Object** opp));
328 INLINE(explicit Operand(Context** cpp));
329 explicit Operand(Handle<Object> handle);
330 INLINE(explicit Operand(Smi* value));
331
332 // Register.
333 INLINE(explicit Operand(Register rm));
334
335 // Return true if this is a register operand.
336 INLINE(bool is_reg() const);
337
338 inline int64_t immediate() const {
339 DCHECK(!is_reg());
340 return imm64_;
341 }
342
343 Register rm() const { return rm_; }
344
345 private:
346 Register rm_;
347 int64_t imm64_; // Valid if rm_ == no_reg.
348 RelocInfo::Mode rmode_;
349
350 friend class Assembler;
351 friend class MacroAssembler;
352};
353
354
355// On MIPS we have only one adressing mode with base_reg + offset.
356// Class MemOperand represents a memory operand in load and store instructions.
357class MemOperand : public Operand {
358 public:
359 // Immediate value attached to offset.
360 enum OffsetAddend {
361 offset_minus_one = -1,
362 offset_zero = 0
363 };
364
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000365 explicit MemOperand(Register rn, int32_t offset = 0);
366 explicit MemOperand(Register rn, int32_t unit, int32_t multiplier,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 OffsetAddend offset_addend = offset_zero);
368 int32_t offset() const { return offset_; }
369
370 bool OffsetIsInt16Encodable() const {
371 return is_int16(offset_);
372 }
373
374 private:
375 int32_t offset_;
376
377 friend class Assembler;
378};
379
380
381class Assembler : public AssemblerBase {
382 public:
383 // Create an assembler. Instructions and relocation information are emitted
384 // into a buffer, with the instructions starting from the beginning and the
385 // relocation information starting from the end of the buffer. See CodeDesc
386 // for a detailed comment on the layout (globals.h).
387 //
388 // If the provided buffer is NULL, the assembler allocates and grows its own
389 // buffer, and buffer_size determines the initial buffer size. The buffer is
390 // owned by the assembler and deallocated upon destruction of the assembler.
391 //
392 // If the provided buffer is not NULL, the assembler uses the provided buffer
393 // for code generation and assumes its size to be buffer_size. If the buffer
394 // is too small, a fatal error occurs. No deallocation of the buffer is done
395 // upon destruction of the assembler.
396 Assembler(Isolate* isolate, void* buffer, int buffer_size);
397 virtual ~Assembler() { }
398
399 // GetCode emits any pending (non-emitted) code and fills the descriptor
400 // desc. GetCode() is idempotent; it returns the same result if no other
401 // Assembler functions are invoked in between GetCode() calls.
402 void GetCode(CodeDesc* desc);
403
404 // Label operations & relative jumps (PPUM Appendix D).
405 //
406 // Takes a branch opcode (cc) and a label (L) and generates
407 // either a backward branch or a forward branch and links it
408 // to the label fixup chain. Usage:
409 //
410 // Label L; // unbound label
411 // j(cc, &L); // forward branch to unbound label
412 // bind(&L); // bind label to the current pc
413 // j(cc, &L); // backward branch to bound label
414 // bind(&L); // illegal: a label may be bound only once
415 //
416 // Note: The same Label can be used for forward and backward branches
417 // but it may be bound only once.
418 void bind(Label* L); // Binds an unbound label L to current code position.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419
420 enum OffsetSize : int { kOffset26 = 26, kOffset21 = 21, kOffset16 = 16 };
421
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 // Determines if Label is bound and near enough so that branch instruction
423 // can be used to reach it, instead of jump instruction.
424 bool is_near(Label* L);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000425 bool is_near(Label* L, OffsetSize bits);
426 bool is_near_branch(Label* L);
427 inline bool is_near_pre_r6(Label* L) {
428 DCHECK(!(kArchVariant == kMips64r6));
429 return pc_offset() - L->pos() < kMaxBranchOffset - 4 * kInstrSize;
430 }
431 inline bool is_near_r6(Label* L) {
432 DCHECK(kArchVariant == kMips64r6);
433 return pc_offset() - L->pos() < kMaxCompactBranchOffset - 4 * kInstrSize;
434 }
435
436 int BranchOffset(Instr instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437
438 // Returns the branch offset to the given label from the current code
439 // position. Links the label to the current position if it is still unbound.
440 // Manages the jump elimination optimization if the second parameter is true.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 int32_t branch_offset_helper(Label* L, OffsetSize bits);
442 inline int32_t branch_offset(Label* L) {
443 return branch_offset_helper(L, OffsetSize::kOffset16);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000444 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 inline int32_t branch_offset21(Label* L) {
446 return branch_offset_helper(L, OffsetSize::kOffset21);
447 }
448 inline int32_t branch_offset26(Label* L) {
449 return branch_offset_helper(L, OffsetSize::kOffset26);
450 }
451 inline int32_t shifted_branch_offset(Label* L) {
452 return branch_offset(L) >> 2;
453 }
454 inline int32_t shifted_branch_offset21(Label* L) {
455 return branch_offset21(L) >> 2;
456 }
457 inline int32_t shifted_branch_offset26(Label* L) {
458 return branch_offset26(L) >> 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 }
460 uint64_t jump_address(Label* L);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 uint64_t jump_offset(Label* L);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462
463 // Puts a labels target address at the given position.
464 // The high 8 bits are set to zero.
465 void label_at_put(Label* L, int at_offset);
466
467 // Read/Modify the code target address in the branch/call instruction at pc.
468 static Address target_address_at(Address pc);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 static void set_target_address_at(
470 Isolate* isolate, Address pc, Address target,
471 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 // On MIPS there is no Constant Pool so we skip that parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000473 INLINE(static Address target_address_at(Address pc, Address constant_pool)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 return target_address_at(pc);
475 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000476 INLINE(static void set_target_address_at(
477 Isolate* isolate, Address pc, Address constant_pool, Address target,
478 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) {
479 set_target_address_at(isolate, pc, target, icache_flush_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 }
481 INLINE(static Address target_address_at(Address pc, Code* code)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000482 Address constant_pool = code ? code->constant_pool() : NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 return target_address_at(pc, constant_pool);
484 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000485 INLINE(static void set_target_address_at(
486 Isolate* isolate, Address pc, Code* code, Address target,
487 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) {
488 Address constant_pool = code ? code->constant_pool() : NULL;
489 set_target_address_at(isolate, pc, constant_pool, target,
490 icache_flush_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 }
492
493 // Return the code target address at a call site from the return address
494 // of that call in the instruction stream.
495 inline static Address target_address_from_return_address(Address pc);
496
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497 static void JumpLabelToJumpRegister(Address pc);
498
499 static void QuietNaN(HeapObject* nan);
500
501 // This sets the branch destination (which gets loaded at the call address).
502 // This is for calls and branches within generated code. The serializer
503 // has already deserialized the lui/ori instructions etc.
504 inline static void deserialization_set_special_target_at(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000505 Isolate* isolate, Address instruction_payload, Code* code,
506 Address target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507 set_target_address_at(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000508 isolate,
509 instruction_payload - kInstructionsFor64BitConstant * kInstrSize, code,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000510 target);
511 }
512
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000513 // This sets the internal reference at the pc.
514 inline static void deserialization_set_target_internal_reference_at(
515 Isolate* isolate, Address pc, Address target,
516 RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
517
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 // Size of an instruction.
519 static const int kInstrSize = sizeof(Instr);
520
521 // Difference between address of current opcode and target address offset.
522 static const int kBranchPCOffset = 4;
523
524 // Here we are patching the address in the LUI/ORI instruction pair.
525 // These values are used in the serialization process and must be zero for
526 // MIPS platform, as Code, Embedded Object or External-reference pointers
527 // are split across two consecutive instructions and don't exist separately
528 // in the code, so the serializer should not step forwards in memory after
529 // a target is resolved and written.
530 static const int kSpecialTargetSize = 0;
531
532 // Number of consecutive instructions used to store 32bit/64bit constant.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000533 // This constant was used in RelocInfo::target_address_address() function
534 // to tell serializer address of the instruction that follows
535 // LUI/ORI instruction pair.
536 static const int kInstructionsFor32BitConstant = 2;
537 static const int kInstructionsFor64BitConstant = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538
539 // Distance between the instruction referring to the address of the call
540 // target and the return address.
Ben Murdochda12d292016-06-02 14:46:10 +0100541#ifdef _MIPS_ARCH_MIPS64R6
542 static const int kCallTargetAddressOffset = 5 * kInstrSize;
543#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 static const int kCallTargetAddressOffset = 6 * kInstrSize;
Ben Murdochda12d292016-06-02 14:46:10 +0100545#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000546
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 // Distance between start of patched debug break slot and the emitted address
548 // to jump to.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 static const int kPatchDebugBreakSlotAddressOffset = 6 * kInstrSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000550
551 // Difference between address of current opcode and value read from pc
552 // register.
553 static const int kPcLoadDelta = 4;
554
Ben Murdochda12d292016-06-02 14:46:10 +0100555#ifdef _MIPS_ARCH_MIPS64R6
556 static const int kDebugBreakSlotInstructions = 5;
557#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 static const int kDebugBreakSlotInstructions = 6;
Ben Murdochda12d292016-06-02 14:46:10 +0100559#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560 static const int kDebugBreakSlotLength =
561 kDebugBreakSlotInstructions * kInstrSize;
562
563
564 // ---------------------------------------------------------------------------
565 // Code generation.
566
567 // Insert the smallest number of nop instructions
568 // possible to align the pc offset to a multiple
569 // of m. m must be a power of 2 (>= 4).
570 void Align(int m);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000571 // Insert the smallest number of zero bytes possible to align the pc offset
572 // to a mulitple of m. m must be a power of 2 (>= 2).
573 void DataAlign(int m);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574 // Aligns code to something that's optimal for a jump target for the platform.
575 void CodeTargetAlign();
576
577 // Different nop operations are used by the code generator to detect certain
578 // states of the generated code.
579 enum NopMarkerTypes {
580 NON_MARKING_NOP = 0,
581 DEBUG_BREAK_NOP,
582 // IC markers.
583 PROPERTY_ACCESS_INLINED,
584 PROPERTY_ACCESS_INLINED_CONTEXT,
585 PROPERTY_ACCESS_INLINED_CONTEXT_DONT_DELETE,
586 // Helper values.
587 LAST_CODE_MARKER,
588 FIRST_IC_MARKER = PROPERTY_ACCESS_INLINED,
589 // Code aging
590 CODE_AGE_MARKER_NOP = 6,
591 CODE_AGE_SEQUENCE_NOP
592 };
593
594 // Type == 0 is the default non-marking nop. For mips this is a
595 // sll(zero_reg, zero_reg, 0). We use rt_reg == at for non-zero
596 // marking, to avoid conflict with ssnop and ehb instructions.
597 void nop(unsigned int type = 0) {
598 DCHECK(type < 32);
599 Register nop_rt_reg = (type == 0) ? zero_reg : at;
600 sll(zero_reg, nop_rt_reg, type, true);
601 }
602
603
604 // --------Branch-and-jump-instructions----------
605 // We don't use likely variant of instructions.
606 void b(int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 inline void b(Label* L) { b(shifted_branch_offset(L)); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608 void bal(int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000609 inline void bal(Label* L) { bal(shifted_branch_offset(L)); }
610 void bc(int32_t offset);
611 inline void bc(Label* L) { bc(shifted_branch_offset26(L)); }
612 void balc(int32_t offset);
613 inline void balc(Label* L) { balc(shifted_branch_offset26(L)); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000614
615 void beq(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000616 inline void beq(Register rs, Register rt, Label* L) {
617 beq(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 }
619 void bgez(Register rs, int16_t offset);
620 void bgezc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621 inline void bgezc(Register rt, Label* L) {
622 bgezc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623 }
624 void bgeuc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000625 inline void bgeuc(Register rs, Register rt, Label* L) {
626 bgeuc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 }
628 void bgec(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000629 inline void bgec(Register rs, Register rt, Label* L) {
630 bgec(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 }
632 void bgezal(Register rs, int16_t offset);
633 void bgezalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000634 inline void bgezalc(Register rt, Label* L) {
635 bgezalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 }
637 void bgezall(Register rs, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000638 inline void bgezall(Register rs, Label* L) {
639 bgezall(rs, branch_offset(L) >> 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000640 }
641 void bgtz(Register rs, int16_t offset);
642 void bgtzc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000643 inline void bgtzc(Register rt, Label* L) {
644 bgtzc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 }
646 void blez(Register rs, int16_t offset);
647 void blezc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000648 inline void blezc(Register rt, Label* L) {
649 blezc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 }
651 void bltz(Register rs, int16_t offset);
652 void bltzc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000653 inline void bltzc(Register rt, Label* L) {
654 bltzc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000655 }
656 void bltuc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000657 inline void bltuc(Register rs, Register rt, Label* L) {
658 bltuc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000659 }
660 void bltc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000661 inline void bltc(Register rs, Register rt, Label* L) {
662 bltc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000663 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000664 void bltzal(Register rs, int16_t offset);
665 void blezalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000666 inline void blezalc(Register rt, Label* L) {
667 blezalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668 }
669 void bltzalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000670 inline void bltzalc(Register rt, Label* L) {
671 bltzalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 }
673 void bgtzalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000674 inline void bgtzalc(Register rt, Label* L) {
675 bgtzalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000676 }
677 void beqzalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000678 inline void beqzalc(Register rt, Label* L) {
679 beqzalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000680 }
681 void beqc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000682 inline void beqc(Register rs, Register rt, Label* L) {
683 beqc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000684 }
685 void beqzc(Register rs, int32_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000686 inline void beqzc(Register rs, Label* L) {
687 beqzc(rs, shifted_branch_offset21(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688 }
689 void bnezalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000690 inline void bnezalc(Register rt, Label* L) {
691 bnezalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692 }
693 void bnec(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000694 inline void bnec(Register rs, Register rt, Label* L) {
695 bnec(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000696 }
697 void bnezc(Register rt, int32_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000698 inline void bnezc(Register rt, Label* L) {
699 bnezc(rt, shifted_branch_offset21(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 }
701 void bne(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000702 inline void bne(Register rs, Register rt, Label* L) {
703 bne(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 }
705 void bovc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000706 inline void bovc(Register rs, Register rt, Label* L) {
707 bovc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 }
709 void bnvc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000710 inline void bnvc(Register rs, Register rt, Label* L) {
711 bnvc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000712 }
713
714 // Never use the int16_t b(l)cond version with a branch offset
715 // instead of using the Label* version.
716
717 // Jump targets must be in the current 256 MB-aligned region. i.e. 28 bits.
718 void j(int64_t target);
719 void jal(int64_t target);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000720 void j(Label* target);
721 void jal(Label* target);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000722 void jalr(Register rs, Register rd = ra);
723 void jr(Register target);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000724 void jic(Register rt, int16_t offset);
725 void jialc(Register rt, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726
727
728 // -------Data-processing-instructions---------
729
730 // Arithmetic.
731 void addu(Register rd, Register rs, Register rt);
732 void subu(Register rd, Register rs, Register rt);
733
734 void div(Register rs, Register rt);
735 void divu(Register rs, Register rt);
736 void ddiv(Register rs, Register rt);
737 void ddivu(Register rs, Register rt);
738 void div(Register rd, Register rs, Register rt);
739 void divu(Register rd, Register rs, Register rt);
740 void ddiv(Register rd, Register rs, Register rt);
741 void ddivu(Register rd, Register rs, Register rt);
742 void mod(Register rd, Register rs, Register rt);
743 void modu(Register rd, Register rs, Register rt);
744 void dmod(Register rd, Register rs, Register rt);
745 void dmodu(Register rd, Register rs, Register rt);
746
747 void mul(Register rd, Register rs, Register rt);
748 void muh(Register rd, Register rs, Register rt);
749 void mulu(Register rd, Register rs, Register rt);
750 void muhu(Register rd, Register rs, Register rt);
751 void mult(Register rs, Register rt);
752 void multu(Register rs, Register rt);
753 void dmul(Register rd, Register rs, Register rt);
754 void dmuh(Register rd, Register rs, Register rt);
755 void dmulu(Register rd, Register rs, Register rt);
756 void dmuhu(Register rd, Register rs, Register rt);
757 void daddu(Register rd, Register rs, Register rt);
758 void dsubu(Register rd, Register rs, Register rt);
759 void dmult(Register rs, Register rt);
760 void dmultu(Register rs, Register rt);
761
762 void addiu(Register rd, Register rs, int32_t j);
763 void daddiu(Register rd, Register rs, int32_t j);
764
765 // Logical.
766 void and_(Register rd, Register rs, Register rt);
767 void or_(Register rd, Register rs, Register rt);
768 void xor_(Register rd, Register rs, Register rt);
769 void nor(Register rd, Register rs, Register rt);
770
771 void andi(Register rd, Register rs, int32_t j);
772 void ori(Register rd, Register rs, int32_t j);
773 void xori(Register rd, Register rs, int32_t j);
774 void lui(Register rd, int32_t j);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000775 void aui(Register rt, Register rs, int32_t j);
776 void daui(Register rt, Register rs, int32_t j);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777 void dahi(Register rs, int32_t j);
778 void dati(Register rs, int32_t j);
779
780 // Shifts.
781 // Please note: sll(zero_reg, zero_reg, x) instructions are reserved as nop
782 // and may cause problems in normal code. coming_from_nop makes sure this
783 // doesn't happen.
784 void sll(Register rd, Register rt, uint16_t sa, bool coming_from_nop = false);
785 void sllv(Register rd, Register rt, Register rs);
786 void srl(Register rd, Register rt, uint16_t sa);
787 void srlv(Register rd, Register rt, Register rs);
788 void sra(Register rt, Register rd, uint16_t sa);
789 void srav(Register rt, Register rd, Register rs);
790 void rotr(Register rd, Register rt, uint16_t sa);
791 void rotrv(Register rd, Register rt, Register rs);
792 void dsll(Register rd, Register rt, uint16_t sa);
793 void dsllv(Register rd, Register rt, Register rs);
794 void dsrl(Register rd, Register rt, uint16_t sa);
795 void dsrlv(Register rd, Register rt, Register rs);
796 void drotr(Register rd, Register rt, uint16_t sa);
Ben Murdochda12d292016-06-02 14:46:10 +0100797 void drotr32(Register rd, Register rt, uint16_t sa);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 void drotrv(Register rd, Register rt, Register rs);
799 void dsra(Register rt, Register rd, uint16_t sa);
800 void dsrav(Register rd, Register rt, Register rs);
801 void dsll32(Register rt, Register rd, uint16_t sa);
802 void dsrl32(Register rt, Register rd, uint16_t sa);
803 void dsra32(Register rt, Register rd, uint16_t sa);
804
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 // ------------Memory-instructions-------------
806
807 void lb(Register rd, const MemOperand& rs);
808 void lbu(Register rd, const MemOperand& rs);
809 void lh(Register rd, const MemOperand& rs);
810 void lhu(Register rd, const MemOperand& rs);
811 void lw(Register rd, const MemOperand& rs);
812 void lwu(Register rd, const MemOperand& rs);
813 void lwl(Register rd, const MemOperand& rs);
814 void lwr(Register rd, const MemOperand& rs);
815 void sb(Register rd, const MemOperand& rs);
816 void sh(Register rd, const MemOperand& rs);
817 void sw(Register rd, const MemOperand& rs);
818 void swl(Register rd, const MemOperand& rs);
819 void swr(Register rd, const MemOperand& rs);
820 void ldl(Register rd, const MemOperand& rs);
821 void ldr(Register rd, const MemOperand& rs);
822 void sdl(Register rd, const MemOperand& rs);
823 void sdr(Register rd, const MemOperand& rs);
824 void ld(Register rd, const MemOperand& rs);
825 void sd(Register rd, const MemOperand& rs);
826
827
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000828 // ---------PC-Relative-instructions-----------
829
830 void addiupc(Register rs, int32_t imm19);
831 void lwpc(Register rs, int32_t offset19);
832 void lwupc(Register rs, int32_t offset19);
833 void ldpc(Register rs, int32_t offset18);
834 void auipc(Register rs, int16_t imm16);
835 void aluipc(Register rs, int16_t imm16);
836
837
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000838 // ----------------Prefetch--------------------
839
840 void pref(int32_t hint, const MemOperand& rs);
841
842
843 // -------------Misc-instructions--------------
844
845 // Break / Trap instructions.
846 void break_(uint32_t code, bool break_as_stop = false);
847 void stop(const char* msg, uint32_t code = kMaxStopCode);
848 void tge(Register rs, Register rt, uint16_t code);
849 void tgeu(Register rs, Register rt, uint16_t code);
850 void tlt(Register rs, Register rt, uint16_t code);
851 void tltu(Register rs, Register rt, uint16_t code);
852 void teq(Register rs, Register rt, uint16_t code);
853 void tne(Register rs, Register rt, uint16_t code);
854
Ben Murdochc5610432016-08-08 18:44:38 +0100855 // Memory barrier instruction.
856 void sync();
857
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000858 // Move from HI/LO register.
859 void mfhi(Register rd);
860 void mflo(Register rd);
861
862 // Set on less than.
863 void slt(Register rd, Register rs, Register rt);
864 void sltu(Register rd, Register rs, Register rt);
865 void slti(Register rd, Register rs, int32_t j);
866 void sltiu(Register rd, Register rs, int32_t j);
867
868 // Conditional move.
869 void movz(Register rd, Register rs, Register rt);
870 void movn(Register rd, Register rs, Register rt);
871 void movt(Register rd, Register rs, uint16_t cc = 0);
872 void movf(Register rd, Register rs, uint16_t cc = 0);
873
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000874 void sel(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
875 void sel_s(FPURegister fd, FPURegister fs, FPURegister ft);
876 void sel_d(FPURegister fd, FPURegister fs, FPURegister ft);
877 void seleqz(Register rd, Register rs, Register rt);
878 void seleqz(SecondaryField fmt, FPURegister fd, FPURegister fs,
879 FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880 void selnez(Register rs, Register rt, Register rd);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000881 void selnez(SecondaryField fmt, FPURegister fd, FPURegister fs,
882 FPURegister ft);
883 void seleqz_d(FPURegister fd, FPURegister fs, FPURegister ft);
884 void seleqz_s(FPURegister fd, FPURegister fs, FPURegister ft);
885 void selnez_d(FPURegister fd, FPURegister fs, FPURegister ft);
886 void selnez_s(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000887
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000888 void movz_s(FPURegister fd, FPURegister fs, Register rt);
889 void movz_d(FPURegister fd, FPURegister fs, Register rt);
890 void movt_s(FPURegister fd, FPURegister fs, uint16_t cc = 0);
891 void movt_d(FPURegister fd, FPURegister fs, uint16_t cc = 0);
892 void movf_s(FPURegister fd, FPURegister fs, uint16_t cc = 0);
893 void movf_d(FPURegister fd, FPURegister fs, uint16_t cc = 0);
894 void movn_s(FPURegister fd, FPURegister fs, Register rt);
895 void movn_d(FPURegister fd, FPURegister fs, Register rt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 // Bit twiddling.
897 void clz(Register rd, Register rs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000898 void dclz(Register rd, Register rs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 void ins_(Register rt, Register rs, uint16_t pos, uint16_t size);
900 void ext_(Register rt, Register rs, uint16_t pos, uint16_t size);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400901 void dext_(Register rt, Register rs, uint16_t pos, uint16_t size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000902 void dextm(Register rt, Register rs, uint16_t pos, uint16_t size);
903 void dextu(Register rt, Register rs, uint16_t pos, uint16_t size);
904 void dins_(Register rt, Register rs, uint16_t pos, uint16_t size);
905 void bitswap(Register rd, Register rt);
906 void dbitswap(Register rd, Register rt);
907 void align(Register rd, Register rs, Register rt, uint8_t bp);
908 void dalign(Register rd, Register rs, Register rt, uint8_t bp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000909
910 // --------Coprocessor-instructions----------------
911
912 // Load, store, and move.
913 void lwc1(FPURegister fd, const MemOperand& src);
914 void ldc1(FPURegister fd, const MemOperand& src);
915
916 void swc1(FPURegister fs, const MemOperand& dst);
917 void sdc1(FPURegister fs, const MemOperand& dst);
918
919 void mtc1(Register rt, FPURegister fs);
920 void mthc1(Register rt, FPURegister fs);
921 void dmtc1(Register rt, FPURegister fs);
922
923 void mfc1(Register rt, FPURegister fs);
924 void mfhc1(Register rt, FPURegister fs);
925 void dmfc1(Register rt, FPURegister fs);
926
927 void ctc1(Register rt, FPUControlRegister fs);
928 void cfc1(Register rt, FPUControlRegister fs);
929
930 // Arithmetic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000931 void add_s(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 void add_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000933 void sub_s(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000934 void sub_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000935 void mul_s(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 void mul_d(FPURegister fd, FPURegister fs, FPURegister ft);
937 void madd_d(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000938 void div_s(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000939 void div_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000940 void abs_s(FPURegister fd, FPURegister fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941 void abs_d(FPURegister fd, FPURegister fs);
942 void mov_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000943 void mov_s(FPURegister fd, FPURegister fs);
944 void neg_s(FPURegister fd, FPURegister fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945 void neg_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000946 void sqrt_s(FPURegister fd, FPURegister fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000947 void sqrt_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000948 void rsqrt_s(FPURegister fd, FPURegister fs);
949 void rsqrt_d(FPURegister fd, FPURegister fs);
950 void recip_d(FPURegister fd, FPURegister fs);
951 void recip_s(FPURegister fd, FPURegister fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952
953 // Conversion.
954 void cvt_w_s(FPURegister fd, FPURegister fs);
955 void cvt_w_d(FPURegister fd, FPURegister fs);
956 void trunc_w_s(FPURegister fd, FPURegister fs);
957 void trunc_w_d(FPURegister fd, FPURegister fs);
958 void round_w_s(FPURegister fd, FPURegister fs);
959 void round_w_d(FPURegister fd, FPURegister fs);
960 void floor_w_s(FPURegister fd, FPURegister fs);
961 void floor_w_d(FPURegister fd, FPURegister fs);
962 void ceil_w_s(FPURegister fd, FPURegister fs);
963 void ceil_w_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000964 void rint_s(FPURegister fd, FPURegister fs);
965 void rint_d(FPURegister fd, FPURegister fs);
966 void rint(SecondaryField fmt, FPURegister fd, FPURegister fs);
967
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968
969 void cvt_l_s(FPURegister fd, FPURegister fs);
970 void cvt_l_d(FPURegister fd, FPURegister fs);
971 void trunc_l_s(FPURegister fd, FPURegister fs);
972 void trunc_l_d(FPURegister fd, FPURegister fs);
973 void round_l_s(FPURegister fd, FPURegister fs);
974 void round_l_d(FPURegister fd, FPURegister fs);
975 void floor_l_s(FPURegister fd, FPURegister fs);
976 void floor_l_d(FPURegister fd, FPURegister fs);
977 void ceil_l_s(FPURegister fd, FPURegister fs);
978 void ceil_l_d(FPURegister fd, FPURegister fs);
979
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000980 void class_s(FPURegister fd, FPURegister fs);
981 void class_d(FPURegister fd, FPURegister fs);
982
983 void min(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
984 void mina(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
985 void max(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
986 void maxa(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
987 void min_s(FPURegister fd, FPURegister fs, FPURegister ft);
988 void min_d(FPURegister fd, FPURegister fs, FPURegister ft);
989 void max_s(FPURegister fd, FPURegister fs, FPURegister ft);
990 void max_d(FPURegister fd, FPURegister fs, FPURegister ft);
991 void mina_s(FPURegister fd, FPURegister fs, FPURegister ft);
992 void mina_d(FPURegister fd, FPURegister fs, FPURegister ft);
993 void maxa_s(FPURegister fd, FPURegister fs, FPURegister ft);
994 void maxa_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995
996 void cvt_s_w(FPURegister fd, FPURegister fs);
997 void cvt_s_l(FPURegister fd, FPURegister fs);
998 void cvt_s_d(FPURegister fd, FPURegister fs);
999
1000 void cvt_d_w(FPURegister fd, FPURegister fs);
1001 void cvt_d_l(FPURegister fd, FPURegister fs);
1002 void cvt_d_s(FPURegister fd, FPURegister fs);
1003
1004 // Conditions and branches for MIPSr6.
1005 void cmp(FPUCondition cond, SecondaryField fmt,
1006 FPURegister fd, FPURegister ft, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001007 void cmp_s(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft);
1008 void cmp_d(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001009
1010 void bc1eqz(int16_t offset, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001011 inline void bc1eqz(Label* L, FPURegister ft) {
1012 bc1eqz(shifted_branch_offset(L), ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013 }
1014 void bc1nez(int16_t offset, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 inline void bc1nez(Label* L, FPURegister ft) {
1016 bc1nez(shifted_branch_offset(L), ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 }
1018
1019 // Conditions and branches for non MIPSr6.
1020 void c(FPUCondition cond, SecondaryField fmt,
1021 FPURegister ft, FPURegister fs, uint16_t cc = 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022 void c_s(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0);
1023 void c_d(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024
1025 void bc1f(int16_t offset, uint16_t cc = 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001026 inline void bc1f(Label* L, uint16_t cc = 0) {
1027 bc1f(shifted_branch_offset(L), cc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001028 }
1029 void bc1t(int16_t offset, uint16_t cc = 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001030 inline void bc1t(Label* L, uint16_t cc = 0) {
1031 bc1t(shifted_branch_offset(L), cc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 }
1033 void fcmp(FPURegister src1, const double src2, FPUCondition cond);
1034
1035 // Check the code size generated from label to here.
1036 int SizeOfCodeGeneratedSince(Label* label) {
1037 return pc_offset() - label->pos();
1038 }
1039
1040 // Check the number of instructions generated from label to here.
1041 int InstructionsGeneratedSince(Label* label) {
1042 return SizeOfCodeGeneratedSince(label) / kInstrSize;
1043 }
1044
1045 // Class for scoping postponing the trampoline pool generation.
1046 class BlockTrampolinePoolScope {
1047 public:
1048 explicit BlockTrampolinePoolScope(Assembler* assem) : assem_(assem) {
1049 assem_->StartBlockTrampolinePool();
1050 }
1051 ~BlockTrampolinePoolScope() {
1052 assem_->EndBlockTrampolinePool();
1053 }
1054
1055 private:
1056 Assembler* assem_;
1057
1058 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockTrampolinePoolScope);
1059 };
1060
1061 // Class for postponing the assembly buffer growth. Typically used for
1062 // sequences of instructions that must be emitted as a unit, before
1063 // buffer growth (and relocation) can occur.
1064 // This blocking scope is not nestable.
1065 class BlockGrowBufferScope {
1066 public:
1067 explicit BlockGrowBufferScope(Assembler* assem) : assem_(assem) {
1068 assem_->StartBlockGrowBuffer();
1069 }
1070 ~BlockGrowBufferScope() {
1071 assem_->EndBlockGrowBuffer();
1072 }
1073
1074 private:
1075 Assembler* assem_;
1076
1077 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockGrowBufferScope);
1078 };
1079
1080 // Debugging.
1081
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001082 // Mark generator continuation.
1083 void RecordGeneratorContinuation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084
1085 // Mark address of a debug break slot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001086 void RecordDebugBreakSlot(RelocInfo::Mode mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001087
1088 // Record the AST id of the CallIC being compiled, so that it can be placed
1089 // in the relocation information.
1090 void SetRecordedAstId(TypeFeedbackId ast_id) {
1091 DCHECK(recorded_ast_id_.IsNone());
1092 recorded_ast_id_ = ast_id;
1093 }
1094
1095 TypeFeedbackId RecordedAstId() {
1096 DCHECK(!recorded_ast_id_.IsNone());
1097 return recorded_ast_id_;
1098 }
1099
1100 void ClearRecordedAstId() { recorded_ast_id_ = TypeFeedbackId::None(); }
1101
1102 // Record a comment relocation entry that can be used by a disassembler.
1103 // Use --code-comments to enable.
1104 void RecordComment(const char* msg);
1105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001106 // Record a deoptimization reason that can be used by a log or cpu profiler.
1107 // Use --trace-deopt to enable.
Ben Murdochc5610432016-08-08 18:44:38 +01001108 void RecordDeoptReason(const int reason, int raw_position, int id);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001109
1110 static int RelocateInternalReference(RelocInfo::Mode rmode, byte* pc,
1111 intptr_t pc_delta);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001112
1113 // Writes a single byte or word of data in the code stream. Used for
1114 // inline tables, e.g., jump-tables.
1115 void db(uint8_t data);
1116 void dd(uint32_t data);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001117 void dq(uint64_t data);
1118 void dp(uintptr_t data) { dq(data); }
1119 void dd(Label* label);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120
Ben Murdochda12d292016-06-02 14:46:10 +01001121 AssemblerPositionsRecorder* positions_recorder() {
1122 return &positions_recorder_;
1123 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124
1125 // Postpone the generation of the trampoline pool for the specified number of
1126 // instructions.
1127 void BlockTrampolinePoolFor(int instructions);
1128
1129 // Check if there is less than kGap bytes available in the buffer.
1130 // If this is the case, we need to grow the buffer before emitting
1131 // an instruction or relocation information.
1132 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
1133
1134 // Get the number of bytes available in the buffer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001135 inline intptr_t available_space() const {
1136 return reloc_info_writer.pos() - pc_;
1137 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138
1139 // Read/patch instructions.
1140 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
1141 static void instr_at_put(byte* pc, Instr instr) {
1142 *reinterpret_cast<Instr*>(pc) = instr;
1143 }
1144 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
1145 void instr_at_put(int pos, Instr instr) {
1146 *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
1147 }
1148
1149 // Check if an instruction is a branch of some kind.
1150 static bool IsBranch(Instr instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001151 static bool IsBc(Instr instr);
1152 static bool IsBzc(Instr instr);
1153
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001154 static bool IsBeq(Instr instr);
1155 static bool IsBne(Instr instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001156 static bool IsBeqzc(Instr instr);
1157 static bool IsBnezc(Instr instr);
1158 static bool IsBeqc(Instr instr);
1159 static bool IsBnec(Instr instr);
1160
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001161
1162 static bool IsJump(Instr instr);
1163 static bool IsJ(Instr instr);
1164 static bool IsLui(Instr instr);
1165 static bool IsOri(Instr instr);
1166
1167 static bool IsJal(Instr instr);
1168 static bool IsJr(Instr instr);
1169 static bool IsJalr(Instr instr);
1170
1171 static bool IsNop(Instr instr, unsigned int type);
1172 static bool IsPop(Instr instr);
1173 static bool IsPush(Instr instr);
1174 static bool IsLwRegFpOffset(Instr instr);
1175 static bool IsSwRegFpOffset(Instr instr);
1176 static bool IsLwRegFpNegOffset(Instr instr);
1177 static bool IsSwRegFpNegOffset(Instr instr);
1178
1179 static Register GetRtReg(Instr instr);
1180 static Register GetRsReg(Instr instr);
1181 static Register GetRdReg(Instr instr);
1182
1183 static uint32_t GetRt(Instr instr);
1184 static uint32_t GetRtField(Instr instr);
1185 static uint32_t GetRs(Instr instr);
1186 static uint32_t GetRsField(Instr instr);
1187 static uint32_t GetRd(Instr instr);
1188 static uint32_t GetRdField(Instr instr);
1189 static uint32_t GetSa(Instr instr);
1190 static uint32_t GetSaField(Instr instr);
1191 static uint32_t GetOpcodeField(Instr instr);
1192 static uint32_t GetFunction(Instr instr);
1193 static uint32_t GetFunctionField(Instr instr);
1194 static uint32_t GetImmediate16(Instr instr);
1195 static uint32_t GetLabelConst(Instr instr);
1196
1197 static int32_t GetBranchOffset(Instr instr);
1198 static bool IsLw(Instr instr);
1199 static int16_t GetLwOffset(Instr instr);
1200 static Instr SetLwOffset(Instr instr, int16_t offset);
1201
1202 static bool IsSw(Instr instr);
1203 static Instr SetSwOffset(Instr instr, int16_t offset);
1204 static bool IsAddImmediate(Instr instr);
1205 static Instr SetAddImmediateOffset(Instr instr, int16_t offset);
1206
1207 static bool IsAndImmediate(Instr instr);
1208 static bool IsEmittedConstant(Instr instr);
1209
1210 void CheckTrampolinePool();
1211
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001212 void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
1213 ConstantPoolEntry::Access access,
1214 ConstantPoolEntry::Type type) {
1215 // No embedded constant pool support.
1216 UNREACHABLE();
1217 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001218
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001219 bool IsPrevInstrCompactBranch() { return prev_instr_compact_branch_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001220
Ben Murdochc5610432016-08-08 18:44:38 +01001221 inline int UnboundLabelsCount() { return unbound_labels_count_; }
1222
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001223 protected:
Ben Murdochda12d292016-06-02 14:46:10 +01001224 // Load Scaled Address instructions.
1225 void lsa(Register rd, Register rt, Register rs, uint8_t sa);
1226 void dlsa(Register rd, Register rt, Register rs, uint8_t sa);
1227
Ben Murdochc5610432016-08-08 18:44:38 +01001228 // Helpers.
1229 void LoadRegPlusOffsetToAt(const MemOperand& src);
1230
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001231 // Relocation for a type-recording IC has the AST id added to it. This
1232 // member variable is a way to pass the information from the call site to
1233 // the relocation info.
1234 TypeFeedbackId recorded_ast_id_;
1235
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001236 inline static void set_target_internal_reference_encoded_at(Address pc,
1237 Address target);
1238
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239 int64_t buffer_space() const { return reloc_info_writer.pos() - pc_; }
1240
1241 // Decode branch instruction at pos and return branch target pos.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001242 int target_at(int pos, bool is_internal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243
1244 // Patch branch instruction at pos to branch to given branch target pos.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001245 void target_at_put(int pos, int target_pos, bool is_internal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246
1247 // Say if we need to relocate with this mode.
1248 bool MustUseReg(RelocInfo::Mode rmode);
1249
1250 // Record reloc info for current pc_.
1251 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1252
1253 // Block the emission of the trampoline pool before pc_offset.
1254 void BlockTrampolinePoolBefore(int pc_offset) {
1255 if (no_trampoline_pool_before_ < pc_offset)
1256 no_trampoline_pool_before_ = pc_offset;
1257 }
1258
1259 void StartBlockTrampolinePool() {
1260 trampoline_pool_blocked_nesting_++;
1261 }
1262
1263 void EndBlockTrampolinePool() {
1264 trampoline_pool_blocked_nesting_--;
1265 }
1266
1267 bool is_trampoline_pool_blocked() const {
1268 return trampoline_pool_blocked_nesting_ > 0;
1269 }
1270
1271 bool has_exception() const {
1272 return internal_trampoline_exception_;
1273 }
1274
1275 void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi);
1276
1277 bool is_trampoline_emitted() const {
1278 return trampoline_emitted_;
1279 }
1280
1281 // Temporarily block automatic assembly buffer growth.
1282 void StartBlockGrowBuffer() {
1283 DCHECK(!block_buffer_growth_);
1284 block_buffer_growth_ = true;
1285 }
1286
1287 void EndBlockGrowBuffer() {
1288 DCHECK(block_buffer_growth_);
1289 block_buffer_growth_ = false;
1290 }
1291
1292 bool is_buffer_growth_blocked() const {
1293 return block_buffer_growth_;
1294 }
1295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001296 void EmitForbiddenSlotInstruction() {
1297 if (IsPrevInstrCompactBranch()) {
1298 nop();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001299 }
1300 }
1301
1302 inline void CheckTrampolinePoolQuick(int extra_instructions = 0);
1303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304 private:
1305 // Buffer size and constant pool distance are checked together at regular
1306 // intervals of kBufferCheckInterval emitted bytes.
1307 static const int kBufferCheckInterval = 1*KB/2;
1308
1309 // Code generation.
1310 // The relocation writer's position is at least kGap bytes below the end of
1311 // the generated instructions. This is so that multi-instruction sequences do
1312 // not have to check for overflow. The same is true for writes of large
1313 // relocation info entries.
1314 static const int kGap = 32;
1315
1316
1317 // Repeated checking whether the trampoline pool should be emitted is rather
1318 // expensive. By default we only check again once a number of instructions
1319 // has been generated.
1320 static const int kCheckConstIntervalInst = 32;
1321 static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize;
1322
1323 int next_buffer_check_; // pc offset of next buffer check.
1324
1325 // Emission of the trampoline pool may be blocked in some code sequences.
1326 int trampoline_pool_blocked_nesting_; // Block emission if this is not zero.
1327 int no_trampoline_pool_before_; // Block emission before this pc offset.
1328
1329 // Keep track of the last emitted pool to guarantee a maximal distance.
1330 int last_trampoline_pool_end_; // pc offset of the end of the last pool.
1331
1332 // Automatic growth of the assembly buffer may be blocked for some sequences.
1333 bool block_buffer_growth_; // Block growth when true.
1334
1335 // Relocation information generation.
1336 // Each relocation is encoded as a variable size value.
1337 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
1338 RelocInfoWriter reloc_info_writer;
1339
1340 // The bound position, before this we cannot do instruction elimination.
1341 int last_bound_pos_;
1342
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001343 // Readable constants for compact branch handling in emit()
1344 enum class CompactBranchType : bool { NO = false, COMPACT_BRANCH = true };
1345
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001346 // Code emission.
1347 inline void CheckBuffer();
1348 void GrowBuffer();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001349 inline void emit(Instr x,
1350 CompactBranchType is_compact_branch = CompactBranchType::NO);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351 inline void emit(uint64_t x);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001352 inline void CheckForEmitInForbiddenSlot();
1353 template <typename T>
1354 inline void EmitHelper(T x);
1355 inline void EmitHelper(Instr x, CompactBranchType is_compact_branch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001356
1357 // Instruction generation.
1358 // We have 3 different kind of encoding layout on MIPS.
1359 // However due to many different types of objects encoded in the same fields
1360 // we have quite a few aliases for each mode.
1361 // Using the same structure to refer to Register and FPURegister would spare a
1362 // few aliases, but mixing both does not look clean to me.
1363 // Anyway we could surely implement this differently.
1364
1365 void GenInstrRegister(Opcode opcode,
1366 Register rs,
1367 Register rt,
1368 Register rd,
1369 uint16_t sa = 0,
1370 SecondaryField func = NULLSF);
1371
1372 void GenInstrRegister(Opcode opcode,
1373 Register rs,
1374 Register rt,
1375 uint16_t msb,
1376 uint16_t lsb,
1377 SecondaryField func);
1378
1379 void GenInstrRegister(Opcode opcode,
1380 SecondaryField fmt,
1381 FPURegister ft,
1382 FPURegister fs,
1383 FPURegister fd,
1384 SecondaryField func = NULLSF);
1385
1386 void GenInstrRegister(Opcode opcode,
1387 FPURegister fr,
1388 FPURegister ft,
1389 FPURegister fs,
1390 FPURegister fd,
1391 SecondaryField func = NULLSF);
1392
1393 void GenInstrRegister(Opcode opcode,
1394 SecondaryField fmt,
1395 Register rt,
1396 FPURegister fs,
1397 FPURegister fd,
1398 SecondaryField func = NULLSF);
1399
1400 void GenInstrRegister(Opcode opcode,
1401 SecondaryField fmt,
1402 Register rt,
1403 FPUControlRegister fs,
1404 SecondaryField func = NULLSF);
1405
1406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001407 void GenInstrImmediate(
1408 Opcode opcode, Register rs, Register rt, int32_t j,
1409 CompactBranchType is_compact_branch = CompactBranchType::NO);
1410 void GenInstrImmediate(
1411 Opcode opcode, Register rs, SecondaryField SF, int32_t j,
1412 CompactBranchType is_compact_branch = CompactBranchType::NO);
1413 void GenInstrImmediate(
1414 Opcode opcode, Register r1, FPURegister r2, int32_t j,
1415 CompactBranchType is_compact_branch = CompactBranchType::NO);
1416 void GenInstrImmediate(
1417 Opcode opcode, Register rs, int32_t offset21,
1418 CompactBranchType is_compact_branch = CompactBranchType::NO);
1419 void GenInstrImmediate(Opcode opcode, Register rs, uint32_t offset21);
1420 void GenInstrImmediate(
1421 Opcode opcode, int32_t offset26,
1422 CompactBranchType is_compact_branch = CompactBranchType::NO);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001423
1424 void GenInstrJump(Opcode opcode,
1425 uint32_t address);
1426
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001427 // Labels.
1428 void print(Label* L);
1429 void bind_to(Label* L, int pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001430 void next(Label* L, bool is_internal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001431
1432 // One trampoline consists of:
1433 // - space for trampoline slots,
1434 // - space for labels.
1435 //
1436 // Space for trampoline slots is equal to slot_count * 2 * kInstrSize.
1437 // Space for trampoline slots preceeds space for labels. Each label is of one
1438 // instruction size, so total amount for labels is equal to
1439 // label_count * kInstrSize.
1440 class Trampoline {
1441 public:
1442 Trampoline() {
1443 start_ = 0;
1444 next_slot_ = 0;
1445 free_slot_count_ = 0;
1446 end_ = 0;
1447 }
1448 Trampoline(int start, int slot_count) {
1449 start_ = start;
1450 next_slot_ = start;
1451 free_slot_count_ = slot_count;
1452 end_ = start + slot_count * kTrampolineSlotsSize;
1453 }
1454 int start() {
1455 return start_;
1456 }
1457 int end() {
1458 return end_;
1459 }
1460 int take_slot() {
1461 int trampoline_slot = kInvalidSlotPos;
1462 if (free_slot_count_ <= 0) {
1463 // We have run out of space on trampolines.
1464 // Make sure we fail in debug mode, so we become aware of each case
1465 // when this happens.
1466 DCHECK(0);
1467 // Internal exception will be caught.
1468 } else {
1469 trampoline_slot = next_slot_;
1470 free_slot_count_--;
1471 next_slot_ += kTrampolineSlotsSize;
1472 }
1473 return trampoline_slot;
1474 }
1475
1476 private:
1477 int start_;
1478 int end_;
1479 int next_slot_;
1480 int free_slot_count_;
1481 };
1482
1483 int32_t get_trampoline_entry(int32_t pos);
1484 int unbound_labels_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001485 // After trampoline is emitted, long branches are used in generated code for
1486 // the forward branches whose target offsets could be beyond reach of branch
1487 // instruction. We use this information to trigger different mode of
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488 // branch instruction generation, where we use jump instructions rather
1489 // than regular branch instructions.
1490 bool trampoline_emitted_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001491 static const int kTrampolineSlotsSize = 2 * kInstrSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001492 static const int kMaxBranchOffset = (1 << (18 - 1)) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001493 static const int kMaxCompactBranchOffset = (1 << (28 - 1)) - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001494 static const int kInvalidSlotPos = -1;
1495
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001496 // Internal reference positions, required for unbounded internal reference
1497 // labels.
1498 std::set<int64_t> internal_reference_positions_;
1499
1500 void EmittedCompactBranchInstruction() { prev_instr_compact_branch_ = true; }
1501 void ClearCompactBranchState() { prev_instr_compact_branch_ = false; }
1502 bool prev_instr_compact_branch_ = false;
1503
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001504 Trampoline trampoline_;
1505 bool internal_trampoline_exception_;
1506
1507 friend class RegExpMacroAssemblerMIPS;
1508 friend class RelocInfo;
1509 friend class CodePatcher;
1510 friend class BlockTrampolinePoolScope;
1511
Ben Murdochda12d292016-06-02 14:46:10 +01001512 AssemblerPositionsRecorder positions_recorder_;
1513 friend class AssemblerPositionsRecorder;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001514 friend class EnsureSpace;
1515};
1516
1517
1518class EnsureSpace BASE_EMBEDDED {
1519 public:
1520 explicit EnsureSpace(Assembler* assembler) {
1521 assembler->CheckBuffer();
1522 }
1523};
1524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001525} // namespace internal
1526} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001527
1528#endif // V8_ARM_ASSEMBLER_MIPS_H_