blob: b708ef7700f94754b09560983a1017966cdec959 [file] [log] [blame]
Andrei Popescu31002712010-02-23 13:46:05 +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.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010033// Copyright 2012 the V8 project authors. All rights reserved.
Andrei Popescu31002712010-02-23 13:46:05 +000034
35
36#ifndef V8_MIPS_ASSEMBLER_MIPS_H_
37#define V8_MIPS_ASSEMBLER_MIPS_H_
38
39#include <stdio.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041#include <set>
42
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043#include "src/assembler.h"
44#include "src/mips/constants-mips.h"
Andrei Popescu31002712010-02-23 13:46:05 +000045
Andrei Popescu31002712010-02-23 13:46:05 +000046namespace 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(t0) V(t1) V(t2) V(t3) V(t4) V(t5) V(t6) V(t7) \
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(t0) V(t1) V(t2) V(t3) V(t4) V(t5) V(t6) 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
Andrei Popescu31002712010-02-23 13:46:05 +000071// 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// -----------------------------------------------------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +000094// Implementation of Register and FPURegister.
Andrei Popescu31002712010-02-23 13:46:05 +000095
Andrei Popescu31002712010-02-23 13:46:05 +000096struct Register {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 static const int kCpRegister = 23; // cp (s7) is the 23rd register.
98
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 enum Code {
100#define REGISTER_CODE(R) kCode_##R,
101 GENERAL_REGISTERS(REGISTER_CODE)
102#undef REGISTER_CODE
103 kAfterLast,
104 kCode_no_reg = -1
105 };
106
107 static const int kNumRegisters = Code::kAfterLast;
108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109#if defined(V8_TARGET_LITTLE_ENDIAN)
110 static const int kMantissaOffset = 0;
111 static const int kExponentOffset = 4;
112#elif defined(V8_TARGET_BIG_ENDIAN)
113 static const int kMantissaOffset = 4;
114 static const int kExponentOffset = 0;
115#else
116#error Unknown endianness
117#endif
118
Steve Block44f0eee2011-05-26 01:26:41 +0100119
120 static Register from_code(int code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 DCHECK(code >= 0);
122 DCHECK(code < kNumRegisters);
123 Register r = {code};
Steve Block44f0eee2011-05-26 01:26:41 +0100124 return r;
125 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 const char* ToString();
127 bool IsAllocatable() const;
128 bool is_valid() const { return 0 <= reg_code && reg_code < kNumRegisters; }
129 bool is(Register reg) const { return reg_code == reg.reg_code; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100130 int code() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 return reg_code;
Andrei Popescu31002712010-02-23 13:46:05 +0000133 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100134 int bit() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 return 1 << reg_code;
Andrei Popescu31002712010-02-23 13:46:05 +0000137 }
138
139 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 int reg_code;
Andrei Popescu31002712010-02-23 13:46:05 +0000141};
142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143// s7: context register
144// s3: lithium scratch
145// s4: lithium scratch2
146#define DECLARE_REGISTER(R) const Register R = {Register::kCode_##R};
147GENERAL_REGISTERS(DECLARE_REGISTER)
148#undef DECLARE_REGISTER
149const Register no_reg = {Register::kCode_no_reg};
Steve Block44f0eee2011-05-26 01:26:41 +0100150
Andrei Popescu31002712010-02-23 13:46:05 +0000151
152int ToNumber(Register reg);
153
154Register ToRegister(int num);
155
156// Coprocessor register.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157struct 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();
Ben Murdoch589d6972011-11-30 16:04:58 +0000169
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 const char* ToString();
175 bool IsAllocatable() const;
176 bool is_valid() const { return 0 <= reg_code && reg_code < kMaxNumRegisters; }
177 bool is(DoubleRegister reg) const { return reg_code == reg.reg_code; }
178 DoubleRegister low() const {
Ben Murdoch589d6972011-11-30 16:04:58 +0000179 // Find low reg of a Double-reg pair, which is the reg itself.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000180 DCHECK(reg_code % 2 == 0); // Specified Double reg must be even.
181 DoubleRegister reg;
182 reg.reg_code = reg_code;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183 DCHECK(reg.is_valid());
Ben Murdoch589d6972011-11-30 16:04:58 +0000184 return reg;
185 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 DoubleRegister high() const {
Ben Murdoch589d6972011-11-30 16:04:58 +0000187 // Find high reg of a Doubel-reg pair, which is reg + 1.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 DCHECK(reg_code % 2 == 0); // Specified Double reg must be even.
189 DoubleRegister reg;
190 reg.reg_code = reg_code + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 DCHECK(reg.is_valid());
Ben Murdoch589d6972011-11-30 16:04:58 +0000192 return reg;
193 }
194
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100195 int code() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 return reg_code;
Andrei Popescu31002712010-02-23 13:46:05 +0000198 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100199 int bit() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201 return 1 << reg_code;
202 }
203
204 static DoubleRegister from_code(int code) {
205 DoubleRegister r = {code};
206 return r;
Andrei Popescu31002712010-02-23 13:46:05 +0000207 }
Steve Block44f0eee2011-05-26 01:26:41 +0100208 void setcode(int f) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209 reg_code = f;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 DCHECK(is_valid());
Steve Block44f0eee2011-05-26 01:26:41 +0100211 }
Andrei Popescu31002712010-02-23 13:46:05 +0000212 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 int reg_code;
Andrei Popescu31002712010-02-23 13:46:05 +0000214};
215
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216// A few double registers are reserved: one as a scratch register and one to
217// hold 0.0.
218// f28: 0.0
219// f30: scratch register.
220
Ben Murdoch589d6972011-11-30 16:04:58 +0000221// V8 now supports the O32 ABI, and the FPU Registers are organized as 32
222// 32-bit registers, f0 through f31. When used as 'double' they are used
223// in pairs, starting with the even numbered register. So a double operation
224// on f0 really uses f0 and f1.
225// (Modern mips hardware also supports 32 64-bit registers, via setting
226// (priviledged) Status Register FR bit to 1. This is used by the N32 ABI,
227// but it is not in common use. Someday we will want to support this in v8.)
Andrei Popescu31002712010-02-23 13:46:05 +0000228
Ben Murdoch589d6972011-11-30 16:04:58 +0000229// For O32 ABI, Floats and Doubles refer to same set of 32 32-bit registers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000230typedef DoubleRegister FPURegister;
231typedef DoubleRegister FloatRegister;
Ben Murdoch589d6972011-11-30 16:04:58 +0000232
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000233const DoubleRegister no_freg = {-1};
Andrei Popescu31002712010-02-23 13:46:05 +0000234
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235const DoubleRegister f0 = {0}; // Return value in hard float mode.
236const DoubleRegister f1 = {1};
237const DoubleRegister f2 = {2};
238const DoubleRegister f3 = {3};
239const DoubleRegister f4 = {4};
240const DoubleRegister f5 = {5};
241const DoubleRegister f6 = {6};
242const DoubleRegister f7 = {7};
243const DoubleRegister f8 = {8};
244const DoubleRegister f9 = {9};
245const DoubleRegister f10 = {10};
246const DoubleRegister f11 = {11};
247const DoubleRegister f12 = {12}; // Arg 0 in hard float mode.
248const DoubleRegister f13 = {13};
249const DoubleRegister f14 = {14}; // Arg 1 in hard float mode.
250const DoubleRegister f15 = {15};
251const DoubleRegister f16 = {16};
252const DoubleRegister f17 = {17};
253const DoubleRegister f18 = {18};
254const DoubleRegister f19 = {19};
255const DoubleRegister f20 = {20};
256const DoubleRegister f21 = {21};
257const DoubleRegister f22 = {22};
258const DoubleRegister f23 = {23};
259const DoubleRegister f24 = {24};
260const DoubleRegister f25 = {25};
261const DoubleRegister f26 = {26};
262const DoubleRegister f27 = {27};
263const DoubleRegister f28 = {28};
264const DoubleRegister f29 = {29};
265const DoubleRegister f30 = {30};
266const DoubleRegister f31 = {31};
Andrei Popescu31002712010-02-23 13:46:05 +0000267
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100268// Register aliases.
269// cp is assumed to be a callee saved register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000270// Defined using #define instead of "static const Register&" because Clang
271// complains otherwise when a compilation unit that includes this header
272// doesn't use the variables.
273#define kRootRegister s6
274#define cp s7
275#define kLithiumScratchReg s3
276#define kLithiumScratchReg2 s4
277#define kLithiumScratchDouble f30
278#define kDoubleRegZero f28
279// Used on mips32r6 for compare operations.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280// We use the last non-callee saved odd register for O32 ABI
281#define kDoubleCompareReg f19
Ben Murdoch589d6972011-11-30 16:04:58 +0000282
Steve Block44f0eee2011-05-26 01:26:41 +0100283// FPU (coprocessor 1) control registers.
284// Currently only FCSR (#31) is implemented.
285struct FPUControlRegister {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000286 bool is_valid() const { return reg_code == kFCSRRegister; }
287 bool is(FPUControlRegister creg) const { return reg_code == creg.reg_code; }
Steve Block44f0eee2011-05-26 01:26:41 +0100288 int code() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000290 return reg_code;
Steve Block44f0eee2011-05-26 01:26:41 +0100291 }
292 int bit() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 DCHECK(is_valid());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294 return 1 << reg_code;
Steve Block44f0eee2011-05-26 01:26:41 +0100295 }
296 void setcode(int f) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 reg_code = f;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 DCHECK(is_valid());
Steve Block44f0eee2011-05-26 01:26:41 +0100299 }
300 // Unfortunately we can't make this private in a struct.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000301 int reg_code;
Andrei Popescu31002712010-02-23 13:46:05 +0000302};
303
Ben Murdoch257744e2011-11-30 15:57:28 +0000304const FPUControlRegister no_fpucreg = { kInvalidFPUControlRegister };
Steve Block44f0eee2011-05-26 01:26:41 +0100305const FPUControlRegister FCSR = { kFCSRRegister };
Andrei Popescu31002712010-02-23 13:46:05 +0000306
Ben Murdoch097c5b22016-05-18 11:27:45 +0100307// TODO(mips) Define SIMD registers.
308typedef DoubleRegister Simd128Register;
Andrei Popescu31002712010-02-23 13:46:05 +0000309
310// -----------------------------------------------------------------------------
311// Machine instruction Operands.
312
313// Class Operand represents a shifter operand in data processing instructions.
314class Operand BASE_EMBEDDED {
315 public:
316 // Immediate.
317 INLINE(explicit Operand(int32_t immediate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 RelocInfo::Mode rmode = RelocInfo::NONE32));
Andrei Popescu31002712010-02-23 13:46:05 +0000319 INLINE(explicit Operand(const ExternalReference& f));
320 INLINE(explicit Operand(const char* s));
321 INLINE(explicit Operand(Object** opp));
322 INLINE(explicit Operand(Context** cpp));
323 explicit Operand(Handle<Object> handle);
324 INLINE(explicit Operand(Smi* value));
325
326 // Register.
327 INLINE(explicit Operand(Register rm));
328
329 // Return true if this is a register operand.
330 INLINE(bool is_reg() const);
331
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 inline int32_t immediate() const {
333 DCHECK(!is_reg());
334 return imm32_;
335 }
336
Andrei Popescu31002712010-02-23 13:46:05 +0000337 Register rm() const { return rm_; }
338
339 private:
340 Register rm_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000341 int32_t imm32_; // Valid if rm_ == no_reg.
Andrei Popescu31002712010-02-23 13:46:05 +0000342 RelocInfo::Mode rmode_;
343
344 friend class Assembler;
345 friend class MacroAssembler;
346};
347
348
349// On MIPS we have only one adressing mode with base_reg + offset.
350// Class MemOperand represents a memory operand in load and store instructions.
351class MemOperand : public Operand {
352 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 // Immediate value attached to offset.
354 enum OffsetAddend {
355 offset_minus_one = -1,
356 offset_zero = 0
357 };
358
Steve Block44f0eee2011-05-26 01:26:41 +0100359 explicit MemOperand(Register rn, int32_t offset = 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 explicit MemOperand(Register rn, int32_t unit, int32_t multiplier,
361 OffsetAddend offset_addend = offset_zero);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000362 int32_t offset() const { return offset_; }
Andrei Popescu31002712010-02-23 13:46:05 +0000363
Ben Murdoch589d6972011-11-30 16:04:58 +0000364 bool OffsetIsInt16Encodable() const {
365 return is_int16(offset_);
366 }
367
Andrei Popescu31002712010-02-23 13:46:05 +0000368 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100369 int32_t offset_;
Andrei Popescu31002712010-02-23 13:46:05 +0000370
371 friend class Assembler;
372};
373
374
Steve Block44f0eee2011-05-26 01:26:41 +0100375class Assembler : public AssemblerBase {
Andrei Popescu31002712010-02-23 13:46:05 +0000376 public:
377 // Create an assembler. Instructions and relocation information are emitted
378 // into a buffer, with the instructions starting from the beginning and the
379 // relocation information starting from the end of the buffer. See CodeDesc
380 // for a detailed comment on the layout (globals.h).
381 //
382 // If the provided buffer is NULL, the assembler allocates and grows its own
383 // buffer, and buffer_size determines the initial buffer size. The buffer is
384 // owned by the assembler and deallocated upon destruction of the assembler.
385 //
386 // If the provided buffer is not NULL, the assembler uses the provided buffer
387 // for code generation and assumes its size to be buffer_size. If the buffer
388 // is too small, a fatal error occurs. No deallocation of the buffer is done
389 // upon destruction of the assembler.
Ben Murdoch257744e2011-11-30 15:57:28 +0000390 Assembler(Isolate* isolate, void* buffer, int buffer_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000391 virtual ~Assembler() { }
Steve Block44f0eee2011-05-26 01:26:41 +0100392
Andrei Popescu31002712010-02-23 13:46:05 +0000393 // GetCode emits any pending (non-emitted) code and fills the descriptor
394 // desc. GetCode() is idempotent; it returns the same result if no other
395 // Assembler functions are invoked in between GetCode() calls.
396 void GetCode(CodeDesc* desc);
397
398 // Label operations & relative jumps (PPUM Appendix D).
399 //
400 // Takes a branch opcode (cc) and a label (L) and generates
401 // either a backward branch or a forward branch and links it
402 // to the label fixup chain. Usage:
403 //
404 // Label L; // unbound label
405 // j(cc, &L); // forward branch to unbound label
406 // bind(&L); // bind label to the current pc
407 // j(cc, &L); // backward branch to bound label
408 // bind(&L); // illegal: a label may be bound only once
409 //
410 // Note: The same Label can be used for forward and backward branches
411 // but it may be bound only once.
Ben Murdoch257744e2011-11-30 15:57:28 +0000412 void bind(Label* L); // Binds an unbound label L to current code position.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413
414 enum OffsetSize : int { kOffset26 = 26, kOffset21 = 21, kOffset16 = 16 };
415
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000416 // Determines if Label is bound and near enough so that branch instruction
417 // can be used to reach it, instead of jump instruction.
418 bool is_near(Label* L);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 bool is_near(Label* L, OffsetSize bits);
420 bool is_near_branch(Label* L);
421 inline bool is_near_pre_r6(Label* L) {
422 DCHECK(!IsMipsArchVariant(kMips32r6));
423 return pc_offset() - L->pos() < kMaxBranchOffset - 4 * kInstrSize;
424 }
425 inline bool is_near_r6(Label* L) {
426 DCHECK(IsMipsArchVariant(kMips32r6));
427 return pc_offset() - L->pos() < kMaxCompactBranchOffset - 4 * kInstrSize;
428 }
429
430 int BranchOffset(Instr instr);
Andrei Popescu31002712010-02-23 13:46:05 +0000431
Ben Murdoch257744e2011-11-30 15:57:28 +0000432 // Returns the branch offset to the given label from the current code
433 // position. Links the label to the current position if it is still unbound.
Andrei Popescu31002712010-02-23 13:46:05 +0000434 // Manages the jump elimination optimization if the second parameter is true.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 int32_t branch_offset_helper(Label* L, OffsetSize bits);
436 inline int32_t branch_offset(Label* L) {
437 return branch_offset_helper(L, OffsetSize::kOffset16);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000439 inline int32_t branch_offset21(Label* L) {
440 return branch_offset_helper(L, OffsetSize::kOffset21);
441 }
442 inline int32_t branch_offset26(Label* L) {
443 return branch_offset_helper(L, OffsetSize::kOffset26);
444 }
445 inline int32_t shifted_branch_offset(Label* L) {
446 return branch_offset(L) >> 2;
447 }
448 inline int32_t shifted_branch_offset21(Label* L) {
449 return branch_offset21(L) >> 2;
450 }
451 inline int32_t shifted_branch_offset26(Label* L) {
452 return branch_offset26(L) >> 2;
Andrei Popescu31002712010-02-23 13:46:05 +0000453 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000454 uint32_t jump_address(Label* L);
Andrei Popescu31002712010-02-23 13:46:05 +0000455
456 // Puts a labels target address at the given position.
457 // The high 8 bits are set to zero.
458 void label_at_put(Label* L, int at_offset);
459
Andrei Popescu31002712010-02-23 13:46:05 +0000460 // Read/Modify the code target address in the branch/call instruction at pc.
461 static Address target_address_at(Address pc);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000462 static void set_target_address_at(
463 Isolate* isolate, Address pc, Address target,
464 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 // On MIPS there is no Constant Pool so we skip that parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466 INLINE(static Address target_address_at(Address pc, Address constant_pool)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467 return target_address_at(pc);
468 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 INLINE(static void set_target_address_at(
470 Isolate* isolate, Address pc, Address constant_pool, Address target,
471 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) {
472 set_target_address_at(isolate, pc, target, icache_flush_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 }
474 INLINE(static Address target_address_at(Address pc, Code* code)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475 Address constant_pool = code ? code->constant_pool() : NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 return target_address_at(pc, constant_pool);
477 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000478 INLINE(static void set_target_address_at(
479 Isolate* isolate, Address pc, Code* code, Address target,
480 ICacheFlushMode icache_flush_mode = FLUSH_ICACHE_IF_NEEDED)) {
481 Address constant_pool = code ? code->constant_pool() : NULL;
482 set_target_address_at(isolate, pc, constant_pool, target,
483 icache_flush_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000484 }
485
486 // Return the code target address at a call site from the return address
487 // of that call in the instruction stream.
488 inline static Address target_address_from_return_address(Address pc);
489
Ben Murdochdb1b4382012-04-26 19:03:50 +0100490 static void QuietNaN(HeapObject* nan);
491
Andrei Popescu31002712010-02-23 13:46:05 +0000492 // This sets the branch destination (which gets loaded at the call address).
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100493 // This is for calls and branches within generated code. The serializer
494 // has already deserialized the lui/ori instructions etc.
495 inline static void deserialization_set_special_target_at(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496 Isolate* isolate, Address instruction_payload, Code* code,
497 Address target) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100498 set_target_address_at(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000499 isolate,
500 instruction_payload - kInstructionsFor32BitConstant * kInstrSize, code,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100501 target);
Andrei Popescu31002712010-02-23 13:46:05 +0000502 }
503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000504 // This sets the internal reference at the pc.
505 inline static void deserialization_set_target_internal_reference_at(
506 Isolate* isolate, Address pc, Address target,
507 RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
508
Steve Block44f0eee2011-05-26 01:26:41 +0100509 // Size of an instruction.
510 static const int kInstrSize = sizeof(Instr);
511
512 // Difference between address of current opcode and target address offset.
513 static const int kBranchPCOffset = 4;
514
515 // Here we are patching the address in the LUI/ORI instruction pair.
516 // These values are used in the serialization process and must be zero for
517 // MIPS platform, as Code, Embedded Object or External-reference pointers
518 // are split across two consecutive instructions and don't exist separately
519 // in the code, so the serializer should not step forwards in memory after
520 // a target is resolved and written.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100521 static const int kSpecialTargetSize = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100522
Ben Murdoch097c5b22016-05-18 11:27:45 +0100523 // Number of consecutive instructions used to store 32bit constant. This
524 // constant is used in RelocInfo::target_address_address() function to tell
525 // serializer address of the instruction that follows LUI/ORI instruction
526 // pair.
527 static const int kInstructionsFor32BitConstant = 2;
Andrei Popescu31002712010-02-23 13:46:05 +0000528
529 // Distance between the instruction referring to the address of the call
530 // target and the return address.
531 static const int kCallTargetAddressOffset = 4 * kInstrSize;
532
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100533 // Distance between start of patched debug break slot and the emitted address
534 // to jump to.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535 static const int kPatchDebugBreakSlotAddressOffset = 4 * kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +0100536
537 // Difference between address of current opcode and value read from pc
538 // register.
539 static const int kPcLoadDelta = 4;
540
Steve Block44f0eee2011-05-26 01:26:41 +0100541 static const int kDebugBreakSlotInstructions = 4;
542 static const int kDebugBreakSlotLength =
543 kDebugBreakSlotInstructions * kInstrSize;
544
Andrei Popescu31002712010-02-23 13:46:05 +0000545
546 // ---------------------------------------------------------------------------
547 // Code generation.
548
Steve Block44f0eee2011-05-26 01:26:41 +0100549 // Insert the smallest number of nop instructions
550 // possible to align the pc offset to a multiple
551 // of m. m must be a power of 2 (>= 4).
552 void Align(int m);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000553 // Insert the smallest number of zero bytes possible to align the pc offset
554 // to a mulitple of m. m must be a power of 2 (>= 2).
555 void DataAlign(int m);
Steve Block44f0eee2011-05-26 01:26:41 +0100556 // Aligns code to something that's optimal for a jump target for the platform.
557 void CodeTargetAlign();
558
559 // Different nop operations are used by the code generator to detect certain
560 // states of the generated code.
561 enum NopMarkerTypes {
562 NON_MARKING_NOP = 0,
563 DEBUG_BREAK_NOP,
564 // IC markers.
565 PROPERTY_ACCESS_INLINED,
566 PROPERTY_ACCESS_INLINED_CONTEXT,
567 PROPERTY_ACCESS_INLINED_CONTEXT_DONT_DELETE,
568 // Helper values.
569 LAST_CODE_MARKER,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 FIRST_IC_MARKER = PROPERTY_ACCESS_INLINED,
571 // Code aging
572 CODE_AGE_MARKER_NOP = 6,
573 CODE_AGE_SEQUENCE_NOP
Steve Block44f0eee2011-05-26 01:26:41 +0100574 };
575
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576 // Type == 0 is the default non-marking nop. For mips this is a
577 // sll(zero_reg, zero_reg, 0). We use rt_reg == at for non-zero
578 // marking, to avoid conflict with ssnop and ehb instructions.
Steve Block44f0eee2011-05-26 01:26:41 +0100579 void nop(unsigned int type = 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 DCHECK(type < 32);
581 Register nop_rt_reg = (type == 0) ? zero_reg : at;
582 sll(zero_reg, nop_rt_reg, type, true);
Steve Block44f0eee2011-05-26 01:26:41 +0100583 }
Andrei Popescu31002712010-02-23 13:46:05 +0000584
585
Ben Murdoch257744e2011-11-30 15:57:28 +0000586 // --------Branch-and-jump-instructions----------
Andrei Popescu31002712010-02-23 13:46:05 +0000587 // We don't use likely variant of instructions.
588 void b(int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000589 inline void b(Label* L) { b(shifted_branch_offset(L)); }
Andrei Popescu31002712010-02-23 13:46:05 +0000590 void bal(int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000591 inline void bal(Label* L) { bal(shifted_branch_offset(L)); }
592 void bc(int32_t offset);
593 inline void bc(Label* L) { bc(shifted_branch_offset26(L)); }
594 void balc(int32_t offset);
595 inline void balc(Label* L) { balc(shifted_branch_offset26(L)); }
Andrei Popescu31002712010-02-23 13:46:05 +0000596
597 void beq(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000598 inline void beq(Register rs, Register rt, Label* L) {
599 beq(rs, rt, shifted_branch_offset(L));
Andrei Popescu31002712010-02-23 13:46:05 +0000600 }
601 void bgez(Register rs, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602 void bgezc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000603 inline void bgezc(Register rt, Label* L) {
604 bgezc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000605 }
606 void bgeuc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 inline void bgeuc(Register rs, Register rt, Label* L) {
608 bgeuc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609 }
610 void bgec(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000611 inline void bgec(Register rs, Register rt, Label* L) {
612 bgec(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613 }
Andrei Popescu31002712010-02-23 13:46:05 +0000614 void bgezal(Register rs, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000615 void bgezalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000616 inline void bgezalc(Register rt, Label* L) {
617 bgezalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 }
619 void bgezall(Register rs, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000620 inline void bgezall(Register rs, Label* L) {
621 bgezall(rs, branch_offset(L) >> 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 }
Andrei Popescu31002712010-02-23 13:46:05 +0000623 void bgtz(Register rs, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 void bgtzc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000625 inline void bgtzc(Register rt, Label* L) {
626 bgtzc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 }
Andrei Popescu31002712010-02-23 13:46:05 +0000628 void blez(Register rs, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 void blezc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000630 inline void blezc(Register rt, Label* L) {
631 blezc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632 }
Andrei Popescu31002712010-02-23 13:46:05 +0000633 void bltz(Register rs, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634 void bltzc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000635 inline void bltzc(Register rt, Label* L) {
636 bltzc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 }
638 void bltuc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000639 inline void bltuc(Register rs, Register rt, Label* L) {
640 bltuc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 }
642 void bltc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000643 inline void bltc(Register rs, Register rt, Label* L) {
644 bltc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 }
Andrei Popescu31002712010-02-23 13:46:05 +0000646 void bltzal(Register rs, int16_t offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 void blezalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000648 inline void blezalc(Register rt, Label* L) {
649 blezalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 }
651 void bltzalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000652 inline void bltzalc(Register rt, Label* L) {
653 bltzalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 }
655 void bgtzalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000656 inline void bgtzalc(Register rt, Label* L) {
657 bgtzalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000658 }
659 void beqzalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000660 inline void beqzalc(Register rt, Label* L) {
661 beqzalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 }
663 void beqc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000664 inline void beqc(Register rs, Register rt, Label* L) {
665 beqc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666 }
667 void beqzc(Register rs, int32_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000668 inline void beqzc(Register rs, Label* L) {
669 beqzc(rs, shifted_branch_offset21(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000670 }
671 void bnezalc(Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000672 inline void bnezalc(Register rt, Label* L) {
673 bnezalc(rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 }
675 void bnec(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 inline void bnec(Register rs, Register rt, Label* L) {
677 bnec(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 }
679 void bnezc(Register rt, int32_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000680 inline void bnezc(Register rt, Label* L) {
681 bnezc(rt, shifted_branch_offset21(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682 }
Andrei Popescu31002712010-02-23 13:46:05 +0000683 void bne(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684 inline void bne(Register rs, Register rt, Label* L) {
685 bne(rs, rt, shifted_branch_offset(L));
Andrei Popescu31002712010-02-23 13:46:05 +0000686 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000687 void bovc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000688 inline void bovc(Register rs, Register rt, Label* L) {
689 bovc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000690 }
691 void bnvc(Register rs, Register rt, int16_t offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000692 inline void bnvc(Register rs, Register rt, Label* L) {
693 bnvc(rs, rt, shifted_branch_offset(L));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000694 }
Andrei Popescu31002712010-02-23 13:46:05 +0000695
696 // Never use the int16_t b(l)cond version with a branch offset
Ben Murdoch257744e2011-11-30 15:57:28 +0000697 // instead of using the Label* version.
Andrei Popescu31002712010-02-23 13:46:05 +0000698
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100699 // Jump targets must be in the current 256 MB-aligned region. i.e. 28 bits.
Andrei Popescu31002712010-02-23 13:46:05 +0000700 void j(int32_t target);
701 void jal(int32_t target);
702 void jalr(Register rs, Register rd = ra);
703 void jr(Register target);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000704 void jic(Register rt, int16_t offset);
705 void jialc(Register rt, int16_t offset);
Andrei Popescu31002712010-02-23 13:46:05 +0000706
707
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 // -------Data-processing-instructions---------
Andrei Popescu31002712010-02-23 13:46:05 +0000709
710 // Arithmetic.
Andrei Popescu31002712010-02-23 13:46:05 +0000711 void addu(Register rd, Register rs, Register rt);
Andrei Popescu31002712010-02-23 13:46:05 +0000712 void subu(Register rd, Register rs, Register rt);
713 void mult(Register rs, Register rt);
714 void multu(Register rs, Register rt);
715 void div(Register rs, Register rt);
716 void divu(Register rs, Register rt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 void div(Register rd, Register rs, Register rt);
718 void divu(Register rd, Register rs, Register rt);
719 void mod(Register rd, Register rs, Register rt);
720 void modu(Register rd, Register rs, Register rt);
Andrei Popescu31002712010-02-23 13:46:05 +0000721 void mul(Register rd, Register rs, Register rt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000722 void muh(Register rd, Register rs, Register rt);
723 void mulu(Register rd, Register rs, Register rt);
724 void muhu(Register rd, Register rs, Register rt);
Andrei Popescu31002712010-02-23 13:46:05 +0000725
Andrei Popescu31002712010-02-23 13:46:05 +0000726 void addiu(Register rd, Register rs, int32_t j);
727
728 // Logical.
729 void and_(Register rd, Register rs, Register rt);
730 void or_(Register rd, Register rs, Register rt);
731 void xor_(Register rd, Register rs, Register rt);
732 void nor(Register rd, Register rs, Register rt);
733
734 void andi(Register rd, Register rs, int32_t j);
735 void ori(Register rd, Register rs, int32_t j);
736 void xori(Register rd, Register rs, int32_t j);
737 void lui(Register rd, int32_t j);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 void aui(Register rs, Register rt, int32_t j);
Andrei Popescu31002712010-02-23 13:46:05 +0000739
740 // Shifts.
Steve Block44f0eee2011-05-26 01:26:41 +0100741 // Please note: sll(zero_reg, zero_reg, x) instructions are reserved as nop
742 // and may cause problems in normal code. coming_from_nop makes sure this
743 // doesn't happen.
744 void sll(Register rd, Register rt, uint16_t sa, bool coming_from_nop = false);
Andrei Popescu31002712010-02-23 13:46:05 +0000745 void sllv(Register rd, Register rt, Register rs);
746 void srl(Register rd, Register rt, uint16_t sa);
747 void srlv(Register rd, Register rt, Register rs);
748 void sra(Register rt, Register rd, uint16_t sa);
749 void srav(Register rt, Register rd, Register rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100750 void rotr(Register rd, Register rt, uint16_t sa);
751 void rotrv(Register rd, Register rt, Register rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000752
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000753 // Address computing instructions with shift.
754 void lsa(Register rd, Register rt, Register rs, uint8_t sa);
Andrei Popescu31002712010-02-23 13:46:05 +0000755
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 // ------------Memory-instructions-------------
Andrei Popescu31002712010-02-23 13:46:05 +0000757
758 void lb(Register rd, const MemOperand& rs);
759 void lbu(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100760 void lh(Register rd, const MemOperand& rs);
761 void lhu(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000762 void lw(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100763 void lwl(Register rd, const MemOperand& rs);
764 void lwr(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000765 void sb(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100766 void sh(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000767 void sw(Register rd, const MemOperand& rs);
Steve Block44f0eee2011-05-26 01:26:41 +0100768 void swl(Register rd, const MemOperand& rs);
769 void swr(Register rd, const MemOperand& rs);
Andrei Popescu31002712010-02-23 13:46:05 +0000770
771
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000772 // ---------PC-Relative-instructions-----------
773
774 void addiupc(Register rs, int32_t imm19);
775 void lwpc(Register rs, int32_t offset19);
776 void auipc(Register rs, int16_t imm16);
777 void aluipc(Register rs, int16_t imm16);
778
779
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 // ----------------Prefetch--------------------
781
782 void pref(int32_t hint, const MemOperand& rs);
783
784
785 // -------------Misc-instructions--------------
Andrei Popescu31002712010-02-23 13:46:05 +0000786
787 // Break / Trap instructions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000788 void break_(uint32_t code, bool break_as_stop = false);
789 void stop(const char* msg, uint32_t code = kMaxStopCode);
Andrei Popescu31002712010-02-23 13:46:05 +0000790 void tge(Register rs, Register rt, uint16_t code);
791 void tgeu(Register rs, Register rt, uint16_t code);
792 void tlt(Register rs, Register rt, uint16_t code);
793 void tltu(Register rs, Register rt, uint16_t code);
794 void teq(Register rs, Register rt, uint16_t code);
795 void tne(Register rs, Register rt, uint16_t code);
796
797 // Move from HI/LO register.
798 void mfhi(Register rd);
799 void mflo(Register rd);
800
801 // Set on less than.
802 void slt(Register rd, Register rs, Register rt);
803 void sltu(Register rd, Register rs, Register rt);
804 void slti(Register rd, Register rs, int32_t j);
805 void sltiu(Register rd, Register rs, int32_t j);
806
Steve Block44f0eee2011-05-26 01:26:41 +0100807 // Conditional move.
808 void movz(Register rd, Register rs, Register rt);
809 void movn(Register rd, Register rs, Register rt);
810 void movt(Register rd, Register rs, uint16_t cc = 0);
811 void movf(Register rd, Register rs, uint16_t cc = 0);
812
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000813 void sel(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
814 void sel_s(FPURegister fd, FPURegister fs, FPURegister ft);
815 void sel_d(FPURegister fd, FPURegister fs, FPURegister ft);
816 void seleqz(Register rd, Register rs, Register rt);
817 void seleqz(SecondaryField fmt, FPURegister fd, FPURegister fs,
818 FPURegister ft);
819 void selnez(Register rd, Register rs, Register rt);
820 void selnez(SecondaryField fmt, FPURegister fd, FPURegister fs,
821 FPURegister ft);
822 void seleqz_d(FPURegister fd, FPURegister fs, FPURegister ft);
823 void seleqz_s(FPURegister fd, FPURegister fs, FPURegister ft);
824 void selnez_d(FPURegister fd, FPURegister fs, FPURegister ft);
825 void selnez_s(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000827 void movz_s(FPURegister fd, FPURegister fs, Register rt);
828 void movz_d(FPURegister fd, FPURegister fs, Register rt);
829 void movt_s(FPURegister fd, FPURegister fs, uint16_t cc = 0);
830 void movt_d(FPURegister fd, FPURegister fs, uint16_t cc = 0);
831 void movf_s(FPURegister fd, FPURegister fs, uint16_t cc = 0);
832 void movf_d(FPURegister fd, FPURegister fs, uint16_t cc = 0);
833 void movn_s(FPURegister fd, FPURegister fs, Register rt);
834 void movn_d(FPURegister fd, FPURegister fs, Register rt);
Steve Block44f0eee2011-05-26 01:26:41 +0100835 // Bit twiddling.
836 void clz(Register rd, Register rs);
837 void ins_(Register rt, Register rs, uint16_t pos, uint16_t size);
838 void ext_(Register rt, Register rs, uint16_t pos, uint16_t size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000839 void bitswap(Register rd, Register rt);
840 void align(Register rd, Register rs, Register rt, uint8_t bp);
Andrei Popescu31002712010-02-23 13:46:05 +0000841
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842 // --------Coprocessor-instructions----------------
Andrei Popescu31002712010-02-23 13:46:05 +0000843
844 // Load, store, and move.
845 void lwc1(FPURegister fd, const MemOperand& src);
846 void ldc1(FPURegister fd, const MemOperand& src);
847
848 void swc1(FPURegister fs, const MemOperand& dst);
849 void sdc1(FPURegister fs, const MemOperand& dst);
850
Steve Block44f0eee2011-05-26 01:26:41 +0100851 void mtc1(Register rt, FPURegister fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 void mthc1(Register rt, FPURegister fs);
853
Steve Block44f0eee2011-05-26 01:26:41 +0100854 void mfc1(Register rt, FPURegister fs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 void mfhc1(Register rt, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100856
857 void ctc1(Register rt, FPUControlRegister fs);
858 void cfc1(Register rt, FPUControlRegister fs);
859
860 // Arithmetic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000861 void add_s(FPURegister fd, FPURegister fs, FPURegister ft);
Steve Block44f0eee2011-05-26 01:26:41 +0100862 void add_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000863 void sub_s(FPURegister fd, FPURegister fs, FPURegister ft);
Steve Block44f0eee2011-05-26 01:26:41 +0100864 void sub_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000865 void mul_s(FPURegister fd, FPURegister fs, FPURegister ft);
Steve Block44f0eee2011-05-26 01:26:41 +0100866 void mul_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000867 void madd_d(FPURegister fd, FPURegister fr, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000868 void div_s(FPURegister fd, FPURegister fs, FPURegister ft);
Steve Block44f0eee2011-05-26 01:26:41 +0100869 void div_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000870 void abs_s(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100871 void abs_d(FPURegister fd, FPURegister fs);
872 void mov_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000873 void mov_s(FPURegister fd, FPURegister fs);
874 void neg_s(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100875 void neg_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000876 void sqrt_s(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100877 void sqrt_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000878 void rsqrt_s(FPURegister fd, FPURegister fs);
879 void rsqrt_d(FPURegister fd, FPURegister fs);
880 void recip_d(FPURegister fd, FPURegister fs);
881 void recip_s(FPURegister fd, FPURegister fs);
Andrei Popescu31002712010-02-23 13:46:05 +0000882
883 // Conversion.
884 void cvt_w_s(FPURegister fd, FPURegister fs);
885 void cvt_w_d(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100886 void trunc_w_s(FPURegister fd, FPURegister fs);
887 void trunc_w_d(FPURegister fd, FPURegister fs);
888 void round_w_s(FPURegister fd, FPURegister fs);
889 void round_w_d(FPURegister fd, FPURegister fs);
890 void floor_w_s(FPURegister fd, FPURegister fs);
891 void floor_w_d(FPURegister fd, FPURegister fs);
892 void ceil_w_s(FPURegister fd, FPURegister fs);
893 void ceil_w_d(FPURegister fd, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000894 void rint_s(FPURegister fd, FPURegister fs);
895 void rint_d(FPURegister fd, FPURegister fs);
896 void rint(SecondaryField fmt, FPURegister fd, FPURegister fs);
Andrei Popescu31002712010-02-23 13:46:05 +0000897
898 void cvt_l_s(FPURegister fd, FPURegister fs);
899 void cvt_l_d(FPURegister fd, FPURegister fs);
Steve Block44f0eee2011-05-26 01:26:41 +0100900 void trunc_l_s(FPURegister fd, FPURegister fs);
901 void trunc_l_d(FPURegister fd, FPURegister fs);
902 void round_l_s(FPURegister fd, FPURegister fs);
903 void round_l_d(FPURegister fd, FPURegister fs);
904 void floor_l_s(FPURegister fd, FPURegister fs);
905 void floor_l_d(FPURegister fd, FPURegister fs);
906 void ceil_l_s(FPURegister fd, FPURegister fs);
907 void ceil_l_d(FPURegister fd, FPURegister fs);
Andrei Popescu31002712010-02-23 13:46:05 +0000908
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000909 void class_s(FPURegister fd, FPURegister fs);
910 void class_d(FPURegister fd, FPURegister fs);
911
912 void min(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
913 void mina(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
914 void max(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
915 void maxa(SecondaryField fmt, FPURegister fd, FPURegister fs, FPURegister ft);
916 void min_s(FPURegister fd, FPURegister fs, FPURegister ft);
917 void min_d(FPURegister fd, FPURegister fs, FPURegister ft);
918 void max_s(FPURegister fd, FPURegister fs, FPURegister ft);
919 void max_d(FPURegister fd, FPURegister fs, FPURegister ft);
920 void mina_s(FPURegister fd, FPURegister fs, FPURegister ft);
921 void mina_d(FPURegister fd, FPURegister fs, FPURegister ft);
922 void maxa_s(FPURegister fd, FPURegister fs, FPURegister ft);
923 void maxa_d(FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924
Andrei Popescu31002712010-02-23 13:46:05 +0000925 void cvt_s_w(FPURegister fd, FPURegister fs);
926 void cvt_s_l(FPURegister fd, FPURegister fs);
927 void cvt_s_d(FPURegister fd, FPURegister fs);
928
929 void cvt_d_w(FPURegister fd, FPURegister fs);
930 void cvt_d_l(FPURegister fd, FPURegister fs);
931 void cvt_d_s(FPURegister fd, FPURegister fs);
932
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000933 // Conditions and branches for MIPSr6.
934 void cmp(FPUCondition cond, SecondaryField fmt,
935 FPURegister fd, FPURegister ft, FPURegister fs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000936 void cmp_s(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft);
937 void cmp_d(FPUCondition cond, FPURegister fd, FPURegister fs, FPURegister ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938
939 void bc1eqz(int16_t offset, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000940 inline void bc1eqz(Label* L, FPURegister ft) {
941 bc1eqz(shifted_branch_offset(L), ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 }
943 void bc1nez(int16_t offset, FPURegister ft);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000944 inline void bc1nez(Label* L, FPURegister ft) {
945 bc1nez(shifted_branch_offset(L), ft);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000946 }
947
948 // Conditions and branches for non MIPSr6.
Andrei Popescu31002712010-02-23 13:46:05 +0000949 void c(FPUCondition cond, SecondaryField fmt,
950 FPURegister ft, FPURegister fs, uint16_t cc = 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 void c_s(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0);
952 void c_d(FPUCondition cond, FPURegister ft, FPURegister fs, uint16_t cc = 0);
Andrei Popescu31002712010-02-23 13:46:05 +0000953
954 void bc1f(int16_t offset, uint16_t cc = 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000955 inline void bc1f(Label* L, uint16_t cc = 0) {
956 bc1f(shifted_branch_offset(L), cc);
957 }
Andrei Popescu31002712010-02-23 13:46:05 +0000958 void bc1t(int16_t offset, uint16_t cc = 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000959 inline void bc1t(Label* L, uint16_t cc = 0) {
960 bc1t(shifted_branch_offset(L), cc);
961 }
Steve Block44f0eee2011-05-26 01:26:41 +0100962 void fcmp(FPURegister src1, const double src2, FPUCondition cond);
Andrei Popescu31002712010-02-23 13:46:05 +0000963
964 // Check the code size generated from label to here.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000965 int SizeOfCodeGeneratedSince(Label* label) {
966 return pc_offset() - label->pos();
967 }
968
969 // Check the number of instructions generated from label to here.
970 int InstructionsGeneratedSince(Label* label) {
971 return SizeOfCodeGeneratedSince(label) / kInstrSize;
Andrei Popescu31002712010-02-23 13:46:05 +0000972 }
973
Steve Block44f0eee2011-05-26 01:26:41 +0100974 // Class for scoping postponing the trampoline pool generation.
975 class BlockTrampolinePoolScope {
976 public:
977 explicit BlockTrampolinePoolScope(Assembler* assem) : assem_(assem) {
978 assem_->StartBlockTrampolinePool();
979 }
980 ~BlockTrampolinePoolScope() {
981 assem_->EndBlockTrampolinePool();
982 }
983
984 private:
985 Assembler* assem_;
986
987 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockTrampolinePoolScope);
988 };
989
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000990 // Class for postponing the assembly buffer growth. Typically used for
991 // sequences of instructions that must be emitted as a unit, before
992 // buffer growth (and relocation) can occur.
993 // This blocking scope is not nestable.
994 class BlockGrowBufferScope {
995 public:
996 explicit BlockGrowBufferScope(Assembler* assem) : assem_(assem) {
997 assem_->StartBlockGrowBuffer();
998 }
999 ~BlockGrowBufferScope() {
1000 assem_->EndBlockGrowBuffer();
1001 }
1002
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001003 private:
1004 Assembler* assem_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockGrowBufferScope);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001007 };
1008
Andrei Popescu31002712010-02-23 13:46:05 +00001009 // Debugging.
1010
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001011 // Mark generator continuation.
1012 void RecordGeneratorContinuation();
Andrei Popescu31002712010-02-23 13:46:05 +00001013
Steve Block44f0eee2011-05-26 01:26:41 +01001014 // Mark address of a debug break slot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 void RecordDebugBreakSlot(RelocInfo::Mode mode);
Steve Block44f0eee2011-05-26 01:26:41 +01001016
Ben Murdoch257744e2011-11-30 15:57:28 +00001017 // Record the AST id of the CallIC being compiled, so that it can be placed
1018 // in the relocation information.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001019 void SetRecordedAstId(TypeFeedbackId ast_id) {
1020 DCHECK(recorded_ast_id_.IsNone());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001021 recorded_ast_id_ = ast_id;
1022 }
1023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024 TypeFeedbackId RecordedAstId() {
1025 DCHECK(!recorded_ast_id_.IsNone());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001026 return recorded_ast_id_;
1027 }
1028
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001029 void ClearRecordedAstId() { recorded_ast_id_ = TypeFeedbackId::None(); }
Ben Murdoch257744e2011-11-30 15:57:28 +00001030
Andrei Popescu31002712010-02-23 13:46:05 +00001031 // Record a comment relocation entry that can be used by a disassembler.
Steve Block44f0eee2011-05-26 01:26:41 +01001032 // Use --code-comments to enable.
Andrei Popescu31002712010-02-23 13:46:05 +00001033 void RecordComment(const char* msg);
1034
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001035 // Record a deoptimization reason that can be used by a log or cpu profiler.
1036 // Use --trace-deopt to enable.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001037 void RecordDeoptReason(const int reason, int raw_position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001038
1039
1040 static int RelocateInternalReference(RelocInfo::Mode rmode, byte* pc,
1041 intptr_t pc_delta);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001042
Steve Block44f0eee2011-05-26 01:26:41 +01001043 // Writes a single byte or word of data in the code stream. Used for
1044 // inline tables, e.g., jump-tables.
1045 void db(uint8_t data);
1046 void dd(uint32_t data);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001047 void dq(uint64_t data);
1048 void dp(uintptr_t data) { dd(data); }
1049 void dd(Label* label);
Steve Block44f0eee2011-05-26 01:26:41 +01001050
1051 PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1052
Steve Block44f0eee2011-05-26 01:26:41 +01001053 // Postpone the generation of the trampoline pool for the specified number of
1054 // instructions.
1055 void BlockTrampolinePoolFor(int instructions);
1056
Andrei Popescu31002712010-02-23 13:46:05 +00001057 // Check if there is less than kGap bytes available in the buffer.
1058 // If this is the case, we need to grow the buffer before emitting
1059 // an instruction or relocation information.
1060 inline bool overflow() const { return pc_ >= reloc_info_writer.pos() - kGap; }
1061
1062 // Get the number of bytes available in the buffer.
1063 inline int available_space() const { return reloc_info_writer.pos() - pc_; }
1064
Andrei Popescu31002712010-02-23 13:46:05 +00001065 // Read/patch instructions.
1066 static Instr instr_at(byte* pc) { return *reinterpret_cast<Instr*>(pc); }
Steve Block44f0eee2011-05-26 01:26:41 +01001067 static void instr_at_put(byte* pc, Instr instr) {
Andrei Popescu31002712010-02-23 13:46:05 +00001068 *reinterpret_cast<Instr*>(pc) = instr;
1069 }
1070 Instr instr_at(int pos) { return *reinterpret_cast<Instr*>(buffer_ + pos); }
1071 void instr_at_put(int pos, Instr instr) {
1072 *reinterpret_cast<Instr*>(buffer_ + pos) = instr;
1073 }
1074
1075 // Check if an instruction is a branch of some kind.
Steve Block44f0eee2011-05-26 01:26:41 +01001076 static bool IsBranch(Instr instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001077 static bool IsBc(Instr instr);
1078 static bool IsBzc(Instr instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00001079 static bool IsBeq(Instr instr);
1080 static bool IsBne(Instr instr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001081 static bool IsBeqzc(Instr instr);
1082 static bool IsBnezc(Instr instr);
1083 static bool IsBeqc(Instr instr);
1084 static bool IsBnec(Instr instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001085
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001086 static bool IsJump(Instr instr);
1087 static bool IsJ(Instr instr);
1088 static bool IsLui(Instr instr);
1089 static bool IsOri(Instr instr);
1090
Ben Murdoch589d6972011-11-30 16:04:58 +00001091 static bool IsJal(Instr instr);
1092 static bool IsJr(Instr instr);
1093 static bool IsJalr(Instr instr);
1094
Steve Block44f0eee2011-05-26 01:26:41 +01001095 static bool IsNop(Instr instr, unsigned int type);
1096 static bool IsPop(Instr instr);
1097 static bool IsPush(Instr instr);
1098 static bool IsLwRegFpOffset(Instr instr);
1099 static bool IsSwRegFpOffset(Instr instr);
1100 static bool IsLwRegFpNegOffset(Instr instr);
1101 static bool IsSwRegFpNegOffset(Instr instr);
1102
Ben Murdoch257744e2011-11-30 15:57:28 +00001103 static Register GetRtReg(Instr instr);
1104 static Register GetRsReg(Instr instr);
1105 static Register GetRdReg(Instr instr);
1106
1107 static uint32_t GetRt(Instr instr);
1108 static uint32_t GetRtField(Instr instr);
1109 static uint32_t GetRs(Instr instr);
1110 static uint32_t GetRsField(Instr instr);
1111 static uint32_t GetRd(Instr instr);
1112 static uint32_t GetRdField(Instr instr);
1113 static uint32_t GetSa(Instr instr);
1114 static uint32_t GetSaField(Instr instr);
1115 static uint32_t GetOpcodeField(Instr instr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001116 static uint32_t GetFunction(Instr instr);
1117 static uint32_t GetFunctionField(Instr instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00001118 static uint32_t GetImmediate16(Instr instr);
1119 static uint32_t GetLabelConst(Instr instr);
Steve Block44f0eee2011-05-26 01:26:41 +01001120
1121 static int32_t GetBranchOffset(Instr instr);
1122 static bool IsLw(Instr instr);
1123 static int16_t GetLwOffset(Instr instr);
1124 static Instr SetLwOffset(Instr instr, int16_t offset);
1125
1126 static bool IsSw(Instr instr);
1127 static Instr SetSwOffset(Instr instr, int16_t offset);
1128 static bool IsAddImmediate(Instr instr);
1129 static Instr SetAddImmediateOffset(Instr instr, int16_t offset);
1130
Ben Murdoch257744e2011-11-30 15:57:28 +00001131 static bool IsAndImmediate(Instr instr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001132 static bool IsEmittedConstant(Instr instr);
Ben Murdoch257744e2011-11-30 15:57:28 +00001133
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001134 void CheckTrampolinePool();
Steve Block44f0eee2011-05-26 01:26:41 +01001135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001136 void PatchConstantPoolAccessInstruction(int pc_offset, int offset,
1137 ConstantPoolEntry::Access access,
1138 ConstantPoolEntry::Type type) {
1139 // No embedded constant pool support.
1140 UNREACHABLE();
1141 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001143 bool IsPrevInstrCompactBranch() { return prev_instr_compact_branch_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001144
Steve Block44f0eee2011-05-26 01:26:41 +01001145 protected:
Ben Murdoch257744e2011-11-30 15:57:28 +00001146 // Relocation for a type-recording IC has the AST id added to it. This
1147 // member variable is a way to pass the information from the call site to
1148 // the relocation info.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149 TypeFeedbackId recorded_ast_id_;
Steve Block44f0eee2011-05-26 01:26:41 +01001150
1151 int32_t buffer_space() const { return reloc_info_writer.pos() - pc_; }
Andrei Popescu31002712010-02-23 13:46:05 +00001152
1153 // Decode branch instruction at pos and return branch target pos.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001154 int target_at(int pos, bool is_internal);
Andrei Popescu31002712010-02-23 13:46:05 +00001155
1156 // Patch branch instruction at pos to branch to given branch target pos.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001157 void target_at_put(int pos, int target_pos, bool is_internal);
Andrei Popescu31002712010-02-23 13:46:05 +00001158
1159 // Say if we need to relocate with this mode.
Steve Block44f0eee2011-05-26 01:26:41 +01001160 bool MustUseReg(RelocInfo::Mode rmode);
Andrei Popescu31002712010-02-23 13:46:05 +00001161
1162 // Record reloc info for current pc_.
1163 void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
1164
Steve Block44f0eee2011-05-26 01:26:41 +01001165 // Block the emission of the trampoline pool before pc_offset.
1166 void BlockTrampolinePoolBefore(int pc_offset) {
1167 if (no_trampoline_pool_before_ < pc_offset)
1168 no_trampoline_pool_before_ = pc_offset;
1169 }
1170
1171 void StartBlockTrampolinePool() {
1172 trampoline_pool_blocked_nesting_++;
1173 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001174
Steve Block44f0eee2011-05-26 01:26:41 +01001175 void EndBlockTrampolinePool() {
1176 trampoline_pool_blocked_nesting_--;
1177 }
1178
1179 bool is_trampoline_pool_blocked() const {
1180 return trampoline_pool_blocked_nesting_ > 0;
1181 }
1182
Ben Murdoch257744e2011-11-30 15:57:28 +00001183 bool has_exception() const {
1184 return internal_trampoline_exception_;
1185 }
1186
Ben Murdoch589d6972011-11-30 16:04:58 +00001187 void DoubleAsTwoUInt32(double d, uint32_t* lo, uint32_t* hi);
1188
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001189 bool is_trampoline_emitted() const {
1190 return trampoline_emitted_;
1191 }
1192
1193 // Temporarily block automatic assembly buffer growth.
1194 void StartBlockGrowBuffer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001195 DCHECK(!block_buffer_growth_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001196 block_buffer_growth_ = true;
1197 }
1198
1199 void EndBlockGrowBuffer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 DCHECK(block_buffer_growth_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001201 block_buffer_growth_ = false;
1202 }
1203
1204 bool is_buffer_growth_blocked() const {
1205 return block_buffer_growth_;
1206 }
1207
Ben Murdoch097c5b22016-05-18 11:27:45 +01001208 void EmitForbiddenSlotInstruction() {
1209 if (IsPrevInstrCompactBranch()) {
1210 nop();
1211 }
1212 }
1213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001214 inline void CheckTrampolinePoolQuick(int extra_instructions = 0);
1215
Andrei Popescu31002712010-02-23 13:46:05 +00001216 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001217 inline static void set_target_internal_reference_encoded_at(Address pc,
1218 Address target);
1219
Andrei Popescu31002712010-02-23 13:46:05 +00001220 // Buffer size and constant pool distance are checked together at regular
1221 // intervals of kBufferCheckInterval emitted bytes.
1222 static const int kBufferCheckInterval = 1*KB/2;
1223
1224 // Code generation.
1225 // The relocation writer's position is at least kGap bytes below the end of
1226 // the generated instructions. This is so that multi-instruction sequences do
1227 // not have to check for overflow. The same is true for writes of large
1228 // relocation info entries.
1229 static const int kGap = 32;
Andrei Popescu31002712010-02-23 13:46:05 +00001230
Steve Block44f0eee2011-05-26 01:26:41 +01001231
1232 // Repeated checking whether the trampoline pool should be emitted is rather
1233 // expensive. By default we only check again once a number of instructions
1234 // has been generated.
1235 static const int kCheckConstIntervalInst = 32;
1236 static const int kCheckConstInterval = kCheckConstIntervalInst * kInstrSize;
1237
1238 int next_buffer_check_; // pc offset of next buffer check.
1239
1240 // Emission of the trampoline pool may be blocked in some code sequences.
1241 int trampoline_pool_blocked_nesting_; // Block emission if this is not zero.
1242 int no_trampoline_pool_before_; // Block emission before this pc offset.
1243
1244 // Keep track of the last emitted pool to guarantee a maximal distance.
1245 int last_trampoline_pool_end_; // pc offset of the end of the last pool.
1246
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001247 // Automatic growth of the assembly buffer may be blocked for some sequences.
1248 bool block_buffer_growth_; // Block growth when true.
1249
Andrei Popescu31002712010-02-23 13:46:05 +00001250 // Relocation information generation.
1251 // Each relocation is encoded as a variable size value.
1252 static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
1253 RelocInfoWriter reloc_info_writer;
1254
1255 // The bound position, before this we cannot do instruction elimination.
1256 int last_bound_pos_;
1257
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001258 // Readable constants for compact branch handling in emit()
1259 enum class CompactBranchType : bool { NO = false, COMPACT_BRANCH = true };
1260
Andrei Popescu31002712010-02-23 13:46:05 +00001261 // Code emission.
1262 inline void CheckBuffer();
1263 void GrowBuffer();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001264 inline void emit(Instr x,
1265 CompactBranchType is_compact_branch = CompactBranchType::NO);
1266 inline void emit(uint64_t x);
1267 inline void CheckForEmitInForbiddenSlot();
1268 template <typename T>
1269 inline void EmitHelper(T x);
1270 inline void EmitHelper(Instr x, CompactBranchType is_compact_branch);
Andrei Popescu31002712010-02-23 13:46:05 +00001271
1272 // Instruction generation.
1273 // We have 3 different kind of encoding layout on MIPS.
1274 // However due to many different types of objects encoded in the same fields
1275 // we have quite a few aliases for each mode.
1276 // Using the same structure to refer to Register and FPURegister would spare a
1277 // few aliases, but mixing both does not look clean to me.
1278 // Anyway we could surely implement this differently.
1279
1280 void GenInstrRegister(Opcode opcode,
1281 Register rs,
1282 Register rt,
1283 Register rd,
1284 uint16_t sa = 0,
1285 SecondaryField func = NULLSF);
1286
1287 void GenInstrRegister(Opcode opcode,
Steve Block44f0eee2011-05-26 01:26:41 +01001288 Register rs,
1289 Register rt,
1290 uint16_t msb,
1291 uint16_t lsb,
1292 SecondaryField func);
1293
1294 void GenInstrRegister(Opcode opcode,
Andrei Popescu31002712010-02-23 13:46:05 +00001295 SecondaryField fmt,
1296 FPURegister ft,
1297 FPURegister fs,
1298 FPURegister fd,
1299 SecondaryField func = NULLSF);
1300
1301 void GenInstrRegister(Opcode opcode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001302 FPURegister fr,
1303 FPURegister ft,
1304 FPURegister fs,
1305 FPURegister fd,
1306 SecondaryField func = NULLSF);
1307
1308 void GenInstrRegister(Opcode opcode,
Andrei Popescu31002712010-02-23 13:46:05 +00001309 SecondaryField fmt,
1310 Register rt,
1311 FPURegister fs,
1312 FPURegister fd,
1313 SecondaryField func = NULLSF);
1314
Steve Block44f0eee2011-05-26 01:26:41 +01001315 void GenInstrRegister(Opcode opcode,
1316 SecondaryField fmt,
1317 Register rt,
1318 FPUControlRegister fs,
1319 SecondaryField func = NULLSF);
1320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001321 void GenInstrImmediate(
1322 Opcode opcode, Register rs, Register rt, int32_t j,
1323 CompactBranchType is_compact_branch = CompactBranchType::NO);
1324 void GenInstrImmediate(
1325 Opcode opcode, Register rs, SecondaryField SF, int32_t j,
1326 CompactBranchType is_compact_branch = CompactBranchType::NO);
1327 void GenInstrImmediate(
1328 Opcode opcode, Register r1, FPURegister r2, int32_t j,
1329 CompactBranchType is_compact_branch = CompactBranchType::NO);
1330 void GenInstrImmediate(
1331 Opcode opcode, Register rs, int32_t offset21,
1332 CompactBranchType is_compact_branch = CompactBranchType::NO);
1333 void GenInstrImmediate(Opcode opcode, Register rs, uint32_t offset21);
1334 void GenInstrImmediate(
1335 Opcode opcode, int32_t offset26,
1336 CompactBranchType is_compact_branch = CompactBranchType::NO);
Andrei Popescu31002712010-02-23 13:46:05 +00001337
1338
1339 void GenInstrJump(Opcode opcode,
1340 uint32_t address);
1341
Steve Block44f0eee2011-05-26 01:26:41 +01001342 // Helpers.
1343 void LoadRegPlusOffsetToAt(const MemOperand& src);
Andrei Popescu31002712010-02-23 13:46:05 +00001344
1345 // Labels.
1346 void print(Label* L);
1347 void bind_to(Label* L, int pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001348 void next(Label* L, bool is_internal);
Andrei Popescu31002712010-02-23 13:46:05 +00001349
Steve Block44f0eee2011-05-26 01:26:41 +01001350 // One trampoline consists of:
1351 // - space for trampoline slots,
1352 // - space for labels.
1353 //
1354 // Space for trampoline slots is equal to slot_count * 2 * kInstrSize.
1355 // Space for trampoline slots preceeds space for labels. Each label is of one
1356 // instruction size, so total amount for labels is equal to
1357 // label_count * kInstrSize.
1358 class Trampoline {
1359 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001360 Trampoline() {
1361 start_ = 0;
1362 next_slot_ = 0;
1363 free_slot_count_ = 0;
1364 end_ = 0;
1365 }
1366 Trampoline(int start, int slot_count) {
Steve Block44f0eee2011-05-26 01:26:41 +01001367 start_ = start;
1368 next_slot_ = start;
1369 free_slot_count_ = slot_count;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001370 end_ = start + slot_count * kTrampolineSlotsSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001371 }
1372 int start() {
1373 return start_;
1374 }
1375 int end() {
1376 return end_;
1377 }
1378 int take_slot() {
Ben Murdoch257744e2011-11-30 15:57:28 +00001379 int trampoline_slot = kInvalidSlotPos;
1380 if (free_slot_count_ <= 0) {
1381 // We have run out of space on trampolines.
1382 // Make sure we fail in debug mode, so we become aware of each case
1383 // when this happens.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384 DCHECK(0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001385 // Internal exception will be caught.
1386 } else {
1387 trampoline_slot = next_slot_;
1388 free_slot_count_--;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001389 next_slot_ += kTrampolineSlotsSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00001390 }
Steve Block44f0eee2011-05-26 01:26:41 +01001391 return trampoline_slot;
1392 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001393
Steve Block44f0eee2011-05-26 01:26:41 +01001394 private:
1395 int start_;
1396 int end_;
1397 int next_slot_;
1398 int free_slot_count_;
Steve Block44f0eee2011-05-26 01:26:41 +01001399 };
1400
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001401 int32_t get_trampoline_entry(int32_t pos);
1402 int unbound_labels_count_;
1403 // If trampoline is emitted, generated code is becoming large. As this is
1404 // already a slow case which can possibly break our code generation for the
1405 // extreme case, we use this information to trigger different mode of
1406 // branch instruction generation, where we use jump instructions rather
1407 // than regular branch instructions.
1408 bool trampoline_emitted_;
1409 static const int kTrampolineSlotsSize = 4 * kInstrSize;
Steve Block44f0eee2011-05-26 01:26:41 +01001410 static const int kMaxBranchOffset = (1 << (18 - 1)) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001411 static const int kMaxCompactBranchOffset = (1 << (28 - 1)) - 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00001412 static const int kInvalidSlotPos = -1;
Steve Block44f0eee2011-05-26 01:26:41 +01001413
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001414 // Internal reference positions, required for unbounded internal reference
1415 // labels.
1416 std::set<int> internal_reference_positions_;
1417
1418 void EmittedCompactBranchInstruction() { prev_instr_compact_branch_ = true; }
1419 void ClearCompactBranchState() { prev_instr_compact_branch_ = false; }
1420 bool prev_instr_compact_branch_ = false;
1421
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001422 Trampoline trampoline_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001423 bool internal_trampoline_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +01001424
Andrei Popescu31002712010-02-23 13:46:05 +00001425 friend class RegExpMacroAssemblerMIPS;
1426 friend class RelocInfo;
Steve Block44f0eee2011-05-26 01:26:41 +01001427 friend class CodePatcher;
1428 friend class BlockTrampolinePoolScope;
1429
1430 PositionsRecorder positions_recorder_;
Steve Block44f0eee2011-05-26 01:26:41 +01001431 friend class PositionsRecorder;
1432 friend class EnsureSpace;
1433};
1434
1435
1436class EnsureSpace BASE_EMBEDDED {
1437 public:
1438 explicit EnsureSpace(Assembler* assembler) {
1439 assembler->CheckBuffer();
1440 }
Andrei Popescu31002712010-02-23 13:46:05 +00001441};
1442
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001443} // namespace internal
1444} // namespace v8
Andrei Popescu31002712010-02-23 13:46:05 +00001445
1446#endif // V8_ARM_ASSEMBLER_MIPS_H_