• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_CODEGEN_MIPS64_CONSTANTS_MIPS64_H_
6 #define V8_CODEGEN_MIPS64_CONSTANTS_MIPS64_H_
7 
8 #include "src/base/logging.h"
9 #include "src/base/macros.h"
10 #include "src/common/globals.h"
11 
12 // UNIMPLEMENTED_ macro for MIPS.
13 #ifdef DEBUG
14 #define UNIMPLEMENTED_MIPS()                                               \
15   v8::internal::PrintF("%s, \tline %d: \tfunction %s not implemented. \n", \
16                        __FILE__, __LINE__, __func__)
17 #else
18 #define UNIMPLEMENTED_MIPS()
19 #endif
20 
21 #define UNSUPPORTED_MIPS() v8::internal::PrintF("Unsupported instruction.\n")
22 
23 enum ArchVariants { kMips64r2, kMips64r6 };
24 
25 #ifdef _MIPS_ARCH_MIPS64R2
26 static const ArchVariants kArchVariant = kMips64r2;
27 #elif _MIPS_ARCH_MIPS64R6
28 static const ArchVariants kArchVariant = kMips64r6;
29 #else
30 static const ArchVariants kArchVariant = kMips64r2;
31 #endif
32 
33 enum Endianness { kLittle, kBig };
34 
35 #if defined(V8_TARGET_LITTLE_ENDIAN)
36 static const Endianness kArchEndian = kLittle;
37 #elif defined(V8_TARGET_BIG_ENDIAN)
38 static const Endianness kArchEndian = kBig;
39 #else
40 #error Unknown endianness
41 #endif
42 
43 // TODO(plind): consider renaming these ...
44 #if defined(__mips_hard_float) && __mips_hard_float != 0
45 // Use floating-point coprocessor instructions. This flag is raised when
46 // -mhard-float is passed to the compiler.
47 const bool IsMipsSoftFloatABI = false;
48 #elif defined(__mips_soft_float) && __mips_soft_float != 0
49 // This flag is raised when -msoft-float is passed to the compiler.
50 // Although FPU is a base requirement for v8, soft-float ABI is used
51 // on soft-float systems with FPU kernel emulation.
52 const bool IsMipsSoftFloatABI = true;
53 #else
54 const bool IsMipsSoftFloatABI = true;
55 #endif
56 
57 #if defined(V8_TARGET_LITTLE_ENDIAN)
58 const uint32_t kMipsLwrOffset = 0;
59 const uint32_t kMipsLwlOffset = 3;
60 const uint32_t kMipsSwrOffset = 0;
61 const uint32_t kMipsSwlOffset = 3;
62 const uint32_t kMipsLdrOffset = 0;
63 const uint32_t kMipsLdlOffset = 7;
64 const uint32_t kMipsSdrOffset = 0;
65 const uint32_t kMipsSdlOffset = 7;
66 #elif defined(V8_TARGET_BIG_ENDIAN)
67 const uint32_t kMipsLwrOffset = 3;
68 const uint32_t kMipsLwlOffset = 0;
69 const uint32_t kMipsSwrOffset = 3;
70 const uint32_t kMipsSwlOffset = 0;
71 const uint32_t kMipsLdrOffset = 7;
72 const uint32_t kMipsLdlOffset = 0;
73 const uint32_t kMipsSdrOffset = 7;
74 const uint32_t kMipsSdlOffset = 0;
75 #else
76 #error Unknown endianness
77 #endif
78 
79 #if defined(V8_TARGET_LITTLE_ENDIAN)
80 const uint32_t kLeastSignificantByteInInt32Offset = 0;
81 const uint32_t kLessSignificantWordInDoublewordOffset = 0;
82 #elif defined(V8_TARGET_BIG_ENDIAN)
83 const uint32_t kLeastSignificantByteInInt32Offset = 3;
84 const uint32_t kLessSignificantWordInDoublewordOffset = 4;
85 #else
86 #error Unknown endianness
87 #endif
88 
89 #ifndef __STDC_FORMAT_MACROS
90 #define __STDC_FORMAT_MACROS
91 #endif
92 #include <inttypes.h>
93 
94 // Defines constants and accessor classes to assemble, disassemble and
95 // simulate MIPS32 instructions.
96 //
97 // See: MIPS32 Architecture For Programmers
98 //      Volume II: The MIPS32 Instruction Set
99 // Try www.cs.cornell.edu/courses/cs3410/2008fa/MIPS_Vol2.pdf.
100 
101 namespace v8 {
102 namespace internal {
103 
104 // TODO(sigurds): Change this value once we use relative jumps.
105 constexpr size_t kMaxPCRelativeCodeRangeInMB = 0;
106 
107 // -----------------------------------------------------------------------------
108 // Registers and FPURegisters.
109 
110 // Number of general purpose registers.
111 const int kNumRegisters = 32;
112 const int kInvalidRegister = -1;
113 
114 // Number of registers with HI, LO, and pc.
115 const int kNumSimuRegisters = 35;
116 
117 // In the simulator, the PC register is simulated as the 34th register.
118 const int kPCRegister = 34;
119 
120 // Number coprocessor registers.
121 const int kNumFPURegisters = 32;
122 const int kInvalidFPURegister = -1;
123 
124 // Number of MSA registers
125 const int kNumMSARegisters = 32;
126 const int kInvalidMSARegister = -1;
127 
128 const int kInvalidMSAControlRegister = -1;
129 const int kMSAIRRegister = 0;
130 const int kMSACSRRegister = 1;
131 const int kMSARegSize = 128;
132 const int kMSALanesByte = kMSARegSize / 8;
133 const int kMSALanesHalf = kMSARegSize / 16;
134 const int kMSALanesWord = kMSARegSize / 32;
135 const int kMSALanesDword = kMSARegSize / 64;
136 
137 // FPU (coprocessor 1) control registers. Currently only FCSR is implemented.
138 const int kFCSRRegister = 31;
139 const int kInvalidFPUControlRegister = -1;
140 const uint32_t kFPUInvalidResult = static_cast<uint32_t>(1u << 31) - 1;
141 const int32_t kFPUInvalidResultNegative = static_cast<int32_t>(1u << 31);
142 const uint64_t kFPU64InvalidResult =
143     static_cast<uint64_t>(static_cast<uint64_t>(1) << 63) - 1;
144 const int64_t kFPU64InvalidResultNegative =
145     static_cast<int64_t>(static_cast<uint64_t>(1) << 63);
146 
147 // FCSR constants.
148 const uint32_t kFCSRInexactFlagBit = 2;
149 const uint32_t kFCSRUnderflowFlagBit = 3;
150 const uint32_t kFCSROverflowFlagBit = 4;
151 const uint32_t kFCSRDivideByZeroFlagBit = 5;
152 const uint32_t kFCSRInvalidOpFlagBit = 6;
153 const uint32_t kFCSRNaN2008FlagBit = 18;
154 
155 const uint32_t kFCSRInexactFlagMask = 1 << kFCSRInexactFlagBit;
156 const uint32_t kFCSRUnderflowFlagMask = 1 << kFCSRUnderflowFlagBit;
157 const uint32_t kFCSROverflowFlagMask = 1 << kFCSROverflowFlagBit;
158 const uint32_t kFCSRDivideByZeroFlagMask = 1 << kFCSRDivideByZeroFlagBit;
159 const uint32_t kFCSRInvalidOpFlagMask = 1 << kFCSRInvalidOpFlagBit;
160 const uint32_t kFCSRNaN2008FlagMask = 1 << kFCSRNaN2008FlagBit;
161 
162 const uint32_t kFCSRFlagMask =
163     kFCSRInexactFlagMask | kFCSRUnderflowFlagMask | kFCSROverflowFlagMask |
164     kFCSRDivideByZeroFlagMask | kFCSRInvalidOpFlagMask;
165 
166 const uint32_t kFCSRExceptionFlagMask = kFCSRFlagMask ^ kFCSRInexactFlagMask;
167 
168 const uint32_t kFCSRInexactCauseBit = 12;
169 const uint32_t kFCSRUnderflowCauseBit = 13;
170 const uint32_t kFCSROverflowCauseBit = 14;
171 const uint32_t kFCSRDivideByZeroCauseBit = 15;
172 const uint32_t kFCSRInvalidOpCauseBit = 16;
173 const uint32_t kFCSRUnimplementedOpCauseBit = 17;
174 
175 const uint32_t kFCSRInexactCauseMask = 1 << kFCSRInexactCauseBit;
176 const uint32_t kFCSRUnderflowCauseMask = 1 << kFCSRUnderflowCauseBit;
177 const uint32_t kFCSROverflowCauseMask = 1 << kFCSROverflowCauseBit;
178 const uint32_t kFCSRDivideByZeroCauseMask = 1 << kFCSRDivideByZeroCauseBit;
179 const uint32_t kFCSRInvalidOpCauseMask = 1 << kFCSRInvalidOpCauseBit;
180 const uint32_t kFCSRUnimplementedOpCauseMask = 1
181                                                << kFCSRUnimplementedOpCauseBit;
182 
183 const uint32_t kFCSRCauseMask =
184     kFCSRInexactCauseMask | kFCSRUnderflowCauseMask | kFCSROverflowCauseMask |
185     kFCSRDivideByZeroCauseMask | kFCSRInvalidOpCauseMask |
186     kFCSRUnimplementedOpCauseBit;
187 
188 // 'pref' instruction hints
189 const int32_t kPrefHintLoad = 0;
190 const int32_t kPrefHintStore = 1;
191 const int32_t kPrefHintLoadStreamed = 4;
192 const int32_t kPrefHintStoreStreamed = 5;
193 const int32_t kPrefHintLoadRetained = 6;
194 const int32_t kPrefHintStoreRetained = 7;
195 const int32_t kPrefHintWritebackInvalidate = 25;
196 const int32_t kPrefHintPrepareForStore = 30;
197 
198 // Actual value of root register is offset from the root array's start
199 // to take advantage of negative displacement values.
200 // TODO(sigurds): Choose best value.
201 constexpr int kRootRegisterBias = 256;
202 
203 // Helper functions for converting between register numbers and names.
204 class Registers {
205  public:
206   // Return the name of the register.
207   static const char* Name(int reg);
208 
209   // Lookup the register number for the name provided.
210   static int Number(const char* name);
211 
212   struct RegisterAlias {
213     int reg;
214     const char* name;
215   };
216 
217   static const int64_t kMaxValue = 0x7fffffffffffffffl;
218   static const int64_t kMinValue = 0x8000000000000000l;
219 
220  private:
221   static const char* names_[kNumSimuRegisters];
222   static const RegisterAlias aliases_[];
223 };
224 
225 // Helper functions for converting between register numbers and names.
226 class FPURegisters {
227  public:
228   // Return the name of the register.
229   static const char* Name(int reg);
230 
231   // Lookup the register number for the name provided.
232   static int Number(const char* name);
233 
234   struct RegisterAlias {
235     int creg;
236     const char* name;
237   };
238 
239  private:
240   static const char* names_[kNumFPURegisters];
241   static const RegisterAlias aliases_[];
242 };
243 
244 // Helper functions for converting between register numbers and names.
245 class MSARegisters {
246  public:
247   // Return the name of the register.
248   static const char* Name(int reg);
249 
250   // Lookup the register number for the name provided.
251   static int Number(const char* name);
252 
253   struct RegisterAlias {
254     int creg;
255     const char* name;
256   };
257 
258  private:
259   static const char* names_[kNumMSARegisters];
260   static const RegisterAlias aliases_[];
261 };
262 
263 // MSA sizes.
264 enum MSASize { MSA_B = 0x0, MSA_H = 0x1, MSA_W = 0x2, MSA_D = 0x3 };
265 
266 // MSA data type, top bit set for unsigned data types.
267 enum MSADataType {
268   MSAS8 = 0,
269   MSAS16 = 1,
270   MSAS32 = 2,
271   MSAS64 = 3,
272   MSAU8 = 4,
273   MSAU16 = 5,
274   MSAU32 = 6,
275   MSAU64 = 7
276 };
277 
278 // -----------------------------------------------------------------------------
279 // Instructions encoding constants.
280 
281 // On MIPS all instructions are 32 bits.
282 using Instr = int32_t;
283 
284 // Special Software Interrupt codes when used in the presence of the MIPS
285 // simulator.
286 enum SoftwareInterruptCodes {
287   // Transition to C code.
288   call_rt_redirected = 0xfffff
289 };
290 
291 // On MIPS Simulator breakpoints can have different codes:
292 // - Breaks between 0 and kMaxWatchpointCode are treated as simple watchpoints,
293 //   the simulator will run through them and print the registers.
294 // - Breaks between kMaxWatchpointCode and kMaxStopCode are treated as stop()
295 //   instructions (see Assembler::stop()).
296 // - Breaks larger than kMaxStopCode are simple breaks, dropping you into the
297 //   debugger.
298 const uint32_t kMaxWatchpointCode = 31;
299 const uint32_t kMaxStopCode = 127;
300 STATIC_ASSERT(kMaxWatchpointCode < kMaxStopCode);
301 
302 // ----- Fields offset and length.
303 const int kOpcodeShift = 26;
304 const int kOpcodeBits = 6;
305 const int kRsShift = 21;
306 const int kRsBits = 5;
307 const int kRtShift = 16;
308 const int kRtBits = 5;
309 const int kRdShift = 11;
310 const int kRdBits = 5;
311 const int kSaShift = 6;
312 const int kSaBits = 5;
313 const int kLsaSaBits = 2;
314 const int kFunctionShift = 0;
315 const int kFunctionBits = 6;
316 const int kLuiShift = 16;
317 const int kBp2Shift = 6;
318 const int kBp2Bits = 2;
319 const int kBp3Shift = 6;
320 const int kBp3Bits = 3;
321 const int kBaseShift = 21;
322 const int kBaseBits = 5;
323 const int kBit6Shift = 6;
324 const int kBit6Bits = 1;
325 
326 const int kImm9Shift = 7;
327 const int kImm9Bits = 9;
328 const int kImm16Shift = 0;
329 const int kImm16Bits = 16;
330 const int kImm18Shift = 0;
331 const int kImm18Bits = 18;
332 const int kImm19Shift = 0;
333 const int kImm19Bits = 19;
334 const int kImm21Shift = 0;
335 const int kImm21Bits = 21;
336 const int kImm26Shift = 0;
337 const int kImm26Bits = 26;
338 const int kImm28Shift = 0;
339 const int kImm28Bits = 28;
340 const int kImm32Shift = 0;
341 const int kImm32Bits = 32;
342 const int kMsaImm8Shift = 16;
343 const int kMsaImm8Bits = 8;
344 const int kMsaImm5Shift = 16;
345 const int kMsaImm5Bits = 5;
346 const int kMsaImm10Shift = 11;
347 const int kMsaImm10Bits = 10;
348 const int kMsaImmMI10Shift = 16;
349 const int kMsaImmMI10Bits = 10;
350 
351 // In branches and jumps immediate fields point to words, not bytes,
352 // and are therefore shifted by 2.
353 const int kImmFieldShift = 2;
354 
355 const int kFrBits = 5;
356 const int kFrShift = 21;
357 const int kFsShift = 11;
358 const int kFsBits = 5;
359 const int kFtShift = 16;
360 const int kFtBits = 5;
361 const int kFdShift = 6;
362 const int kFdBits = 5;
363 const int kFCccShift = 8;
364 const int kFCccBits = 3;
365 const int kFBccShift = 18;
366 const int kFBccBits = 3;
367 const int kFBtrueShift = 16;
368 const int kFBtrueBits = 1;
369 const int kWtBits = 5;
370 const int kWtShift = 16;
371 const int kWsBits = 5;
372 const int kWsShift = 11;
373 const int kWdBits = 5;
374 const int kWdShift = 6;
375 
376 // ----- Miscellaneous useful masks.
377 // Instruction bit masks.
378 const int kOpcodeMask = ((1 << kOpcodeBits) - 1) << kOpcodeShift;
379 const int kImm9Mask = ((1 << kImm9Bits) - 1) << kImm9Shift;
380 const int kImm16Mask = ((1 << kImm16Bits) - 1) << kImm16Shift;
381 const int kImm18Mask = ((1 << kImm18Bits) - 1) << kImm18Shift;
382 const int kImm19Mask = ((1 << kImm19Bits) - 1) << kImm19Shift;
383 const int kImm21Mask = ((1 << kImm21Bits) - 1) << kImm21Shift;
384 const int kImm26Mask = ((1 << kImm26Bits) - 1) << kImm26Shift;
385 const int kImm28Mask = ((1 << kImm28Bits) - 1) << kImm28Shift;
386 const int kImm5Mask = ((1 << 5) - 1);
387 const int kImm8Mask = ((1 << 8) - 1);
388 const int kImm10Mask = ((1 << 10) - 1);
389 const int kMsaI5I10Mask = ((7U << 23) | ((1 << 6) - 1));
390 const int kMsaI8Mask = ((3U << 24) | ((1 << 6) - 1));
391 const int kMsaI5Mask = ((7U << 23) | ((1 << 6) - 1));
392 const int kMsaMI10Mask = (15U << 2);
393 const int kMsaBITMask = ((7U << 23) | ((1 << 6) - 1));
394 const int kMsaELMMask = (15U << 22);
395 const int kMsaLongerELMMask = kMsaELMMask | (63U << 16);
396 const int kMsa3RMask = ((7U << 23) | ((1 << 6) - 1));
397 const int kMsa3RFMask = ((15U << 22) | ((1 << 6) - 1));
398 const int kMsaVECMask = (23U << 21);
399 const int kMsa2RMask = (7U << 18);
400 const int kMsa2RFMask = (15U << 17);
401 const int kRsFieldMask = ((1 << kRsBits) - 1) << kRsShift;
402 const int kRtFieldMask = ((1 << kRtBits) - 1) << kRtShift;
403 const int kRdFieldMask = ((1 << kRdBits) - 1) << kRdShift;
404 const int kSaFieldMask = ((1 << kSaBits) - 1) << kSaShift;
405 const int kFunctionFieldMask = ((1 << kFunctionBits) - 1) << kFunctionShift;
406 // Misc masks.
407 const int kHiMaskOf32 = 0xffff << 16;  // Only to be used with 32-bit values
408 const int kLoMaskOf32 = 0xffff;
409 const int kSignMaskOf32 = 0x80000000;  // Only to be used with 32-bit values
410 const int kJumpAddrMask = (1 << (kImm26Bits + kImmFieldShift)) - 1;
411 const int64_t kTop16MaskOf64 = (int64_t)0xffff << 48;
412 const int64_t kHigher16MaskOf64 = (int64_t)0xffff << 32;
413 const int64_t kUpper16MaskOf64 = (int64_t)0xffff << 16;
414 const int32_t kJalRawMark = 0x00000000;
415 const int32_t kJRawMark = 0xf0000000;
416 const int32_t kJumpRawMask = 0xf0000000;
417 
418 // ----- MIPS Opcodes and Function Fields.
419 // We use this presentation to stay close to the table representation in
420 // MIPS32 Architecture For Programmers, Volume II: The MIPS32 Instruction Set.
421 enum Opcode : uint32_t {
422   SPECIAL = 0U << kOpcodeShift,
423   REGIMM = 1U << kOpcodeShift,
424 
425   J = ((0U << 3) + 2) << kOpcodeShift,
426   JAL = ((0U << 3) + 3) << kOpcodeShift,
427   BEQ = ((0U << 3) + 4) << kOpcodeShift,
428   BNE = ((0U << 3) + 5) << kOpcodeShift,
429   BLEZ = ((0U << 3) + 6) << kOpcodeShift,
430   BGTZ = ((0U << 3) + 7) << kOpcodeShift,
431 
432   ADDI = ((1U << 3) + 0) << kOpcodeShift,
433   ADDIU = ((1U << 3) + 1) << kOpcodeShift,
434   SLTI = ((1U << 3) + 2) << kOpcodeShift,
435   SLTIU = ((1U << 3) + 3) << kOpcodeShift,
436   ANDI = ((1U << 3) + 4) << kOpcodeShift,
437   ORI = ((1U << 3) + 5) << kOpcodeShift,
438   XORI = ((1U << 3) + 6) << kOpcodeShift,
439   LUI = ((1U << 3) + 7) << kOpcodeShift,  // LUI/AUI family.
440   DAUI = ((3U << 3) + 5) << kOpcodeShift,
441 
442   BEQC = ((2U << 3) + 0) << kOpcodeShift,
443   COP1 = ((2U << 3) + 1) << kOpcodeShift,  // Coprocessor 1 class.
444   BEQL = ((2U << 3) + 4) << kOpcodeShift,
445   BNEL = ((2U << 3) + 5) << kOpcodeShift,
446   BLEZL = ((2U << 3) + 6) << kOpcodeShift,
447   BGTZL = ((2U << 3) + 7) << kOpcodeShift,
448 
449   DADDI = ((3U << 3) + 0) << kOpcodeShift,  // This is also BNEC.
450   DADDIU = ((3U << 3) + 1) << kOpcodeShift,
451   LDL = ((3U << 3) + 2) << kOpcodeShift,
452   LDR = ((3U << 3) + 3) << kOpcodeShift,
453   SPECIAL2 = ((3U << 3) + 4) << kOpcodeShift,
454   MSA = ((3U << 3) + 6) << kOpcodeShift,
455   SPECIAL3 = ((3U << 3) + 7) << kOpcodeShift,
456 
457   LB = ((4U << 3) + 0) << kOpcodeShift,
458   LH = ((4U << 3) + 1) << kOpcodeShift,
459   LWL = ((4U << 3) + 2) << kOpcodeShift,
460   LW = ((4U << 3) + 3) << kOpcodeShift,
461   LBU = ((4U << 3) + 4) << kOpcodeShift,
462   LHU = ((4U << 3) + 5) << kOpcodeShift,
463   LWR = ((4U << 3) + 6) << kOpcodeShift,
464   LWU = ((4U << 3) + 7) << kOpcodeShift,
465 
466   SB = ((5U << 3) + 0) << kOpcodeShift,
467   SH = ((5U << 3) + 1) << kOpcodeShift,
468   SWL = ((5U << 3) + 2) << kOpcodeShift,
469   SW = ((5U << 3) + 3) << kOpcodeShift,
470   SDL = ((5U << 3) + 4) << kOpcodeShift,
471   SDR = ((5U << 3) + 5) << kOpcodeShift,
472   SWR = ((5U << 3) + 6) << kOpcodeShift,
473 
474   LL = ((6U << 3) + 0) << kOpcodeShift,
475   LWC1 = ((6U << 3) + 1) << kOpcodeShift,
476   BC = ((6U << 3) + 2) << kOpcodeShift,
477   LLD = ((6U << 3) + 4) << kOpcodeShift,
478   LDC1 = ((6U << 3) + 5) << kOpcodeShift,
479   POP66 = ((6U << 3) + 6) << kOpcodeShift,
480   LD = ((6U << 3) + 7) << kOpcodeShift,
481 
482   PREF = ((6U << 3) + 3) << kOpcodeShift,
483 
484   SC = ((7U << 3) + 0) << kOpcodeShift,
485   SWC1 = ((7U << 3) + 1) << kOpcodeShift,
486   BALC = ((7U << 3) + 2) << kOpcodeShift,
487   PCREL = ((7U << 3) + 3) << kOpcodeShift,
488   SCD = ((7U << 3) + 4) << kOpcodeShift,
489   SDC1 = ((7U << 3) + 5) << kOpcodeShift,
490   POP76 = ((7U << 3) + 6) << kOpcodeShift,
491   SD = ((7U << 3) + 7) << kOpcodeShift,
492 
493   COP1X = ((1U << 4) + 3) << kOpcodeShift,
494 
495   // New r6 instruction.
496   POP06 = BLEZ,   // bgeuc/bleuc, blezalc, bgezalc
497   POP07 = BGTZ,   // bltuc/bgtuc, bgtzalc, bltzalc
498   POP10 = ADDI,   // beqzalc, bovc, beqc
499   POP26 = BLEZL,  // bgezc, blezc, bgec/blec
500   POP27 = BGTZL,  // bgtzc, bltzc, bltc/bgtc
501   POP30 = DADDI,  // bnezalc, bnvc, bnec
502 };
503 
504 enum SecondaryField : uint32_t {
505   // SPECIAL Encoding of Function Field.
506   SLL = ((0U << 3) + 0),
507   MOVCI = ((0U << 3) + 1),
508   SRL = ((0U << 3) + 2),
509   SRA = ((0U << 3) + 3),
510   SLLV = ((0U << 3) + 4),
511   LSA = ((0U << 3) + 5),
512   SRLV = ((0U << 3) + 6),
513   SRAV = ((0U << 3) + 7),
514 
515   JR = ((1U << 3) + 0),
516   JALR = ((1U << 3) + 1),
517   MOVZ = ((1U << 3) + 2),
518   MOVN = ((1U << 3) + 3),
519   BREAK = ((1U << 3) + 5),
520   SYNC = ((1U << 3) + 7),
521 
522   MFHI = ((2U << 3) + 0),
523   CLZ_R6 = ((2U << 3) + 0),
524   CLO_R6 = ((2U << 3) + 1),
525   MFLO = ((2U << 3) + 2),
526   DCLZ_R6 = ((2U << 3) + 2),
527   DCLO_R6 = ((2U << 3) + 3),
528   DSLLV = ((2U << 3) + 4),
529   DLSA = ((2U << 3) + 5),
530   DSRLV = ((2U << 3) + 6),
531   DSRAV = ((2U << 3) + 7),
532 
533   MULT = ((3U << 3) + 0),
534   MULTU = ((3U << 3) + 1),
535   DIV = ((3U << 3) + 2),
536   DIVU = ((3U << 3) + 3),
537   DMULT = ((3U << 3) + 4),
538   DMULTU = ((3U << 3) + 5),
539   DDIV = ((3U << 3) + 6),
540   DDIVU = ((3U << 3) + 7),
541 
542   ADD = ((4U << 3) + 0),
543   ADDU = ((4U << 3) + 1),
544   SUB = ((4U << 3) + 2),
545   SUBU = ((4U << 3) + 3),
546   AND = ((4U << 3) + 4),
547   OR = ((4U << 3) + 5),
548   XOR = ((4U << 3) + 6),
549   NOR = ((4U << 3) + 7),
550 
551   SLT = ((5U << 3) + 2),
552   SLTU = ((5U << 3) + 3),
553   DADD = ((5U << 3) + 4),
554   DADDU = ((5U << 3) + 5),
555   DSUB = ((5U << 3) + 6),
556   DSUBU = ((5U << 3) + 7),
557 
558   TGE = ((6U << 3) + 0),
559   TGEU = ((6U << 3) + 1),
560   TLT = ((6U << 3) + 2),
561   TLTU = ((6U << 3) + 3),
562   TEQ = ((6U << 3) + 4),
563   SELEQZ_S = ((6U << 3) + 5),
564   TNE = ((6U << 3) + 6),
565   SELNEZ_S = ((6U << 3) + 7),
566 
567   DSLL = ((7U << 3) + 0),
568   DSRL = ((7U << 3) + 2),
569   DSRA = ((7U << 3) + 3),
570   DSLL32 = ((7U << 3) + 4),
571   DSRL32 = ((7U << 3) + 6),
572   DSRA32 = ((7U << 3) + 7),
573 
574   // Multiply integers in r6.
575   MUL_MUH = ((3U << 3) + 0),      // MUL, MUH.
576   MUL_MUH_U = ((3U << 3) + 1),    // MUL_U, MUH_U.
577   D_MUL_MUH = ((7U << 2) + 0),    // DMUL, DMUH.
578   D_MUL_MUH_U = ((7U << 2) + 1),  // DMUL_U, DMUH_U.
579   RINT = ((3U << 3) + 2),
580 
581   MUL_OP = ((0U << 3) + 2),
582   MUH_OP = ((0U << 3) + 3),
583   DIV_OP = ((0U << 3) + 2),
584   MOD_OP = ((0U << 3) + 3),
585 
586   DIV_MOD = ((3U << 3) + 2),
587   DIV_MOD_U = ((3U << 3) + 3),
588   D_DIV_MOD = ((3U << 3) + 6),
589   D_DIV_MOD_U = ((3U << 3) + 7),
590 
591   // drotr in special4?
592 
593   // SPECIAL2 Encoding of Function Field.
594   MUL = ((0U << 3) + 2),
595   CLZ = ((4U << 3) + 0),
596   CLO = ((4U << 3) + 1),
597   DCLZ = ((4U << 3) + 4),
598   DCLO = ((4U << 3) + 5),
599 
600   // SPECIAL3 Encoding of Function Field.
601   EXT = ((0U << 3) + 0),
602   DEXTM = ((0U << 3) + 1),
603   DEXTU = ((0U << 3) + 2),
604   DEXT = ((0U << 3) + 3),
605   INS = ((0U << 3) + 4),
606   DINSM = ((0U << 3) + 5),
607   DINSU = ((0U << 3) + 6),
608   DINS = ((0U << 3) + 7),
609 
610   BSHFL = ((4U << 3) + 0),
611   DBSHFL = ((4U << 3) + 4),
612   SC_R6 = ((4U << 3) + 6),
613   SCD_R6 = ((4U << 3) + 7),
614   LL_R6 = ((6U << 3) + 6),
615   LLD_R6 = ((6U << 3) + 7),
616 
617   // SPECIAL3 Encoding of sa Field.
618   BITSWAP = ((0U << 3) + 0),
619   ALIGN = ((0U << 3) + 2),
620   WSBH = ((0U << 3) + 2),
621   SEB = ((2U << 3) + 0),
622   SEH = ((3U << 3) + 0),
623 
624   DBITSWAP = ((0U << 3) + 0),
625   DALIGN = ((0U << 3) + 1),
626   DBITSWAP_SA = ((0U << 3) + 0) << kSaShift,
627   DSBH = ((0U << 3) + 2),
628   DSHD = ((0U << 3) + 5),
629 
630   // REGIMM  encoding of rt Field.
631   BLTZ = ((0U << 3) + 0) << 16,
632   BGEZ = ((0U << 3) + 1) << 16,
633   BLTZAL = ((2U << 3) + 0) << 16,
634   BGEZAL = ((2U << 3) + 1) << 16,
635   BGEZALL = ((2U << 3) + 3) << 16,
636   DAHI = ((0U << 3) + 6) << 16,
637   DATI = ((3U << 3) + 6) << 16,
638 
639   // COP1 Encoding of rs Field.
640   MFC1 = ((0U << 3) + 0) << 21,
641   DMFC1 = ((0U << 3) + 1) << 21,
642   CFC1 = ((0U << 3) + 2) << 21,
643   MFHC1 = ((0U << 3) + 3) << 21,
644   MTC1 = ((0U << 3) + 4) << 21,
645   DMTC1 = ((0U << 3) + 5) << 21,
646   CTC1 = ((0U << 3) + 6) << 21,
647   MTHC1 = ((0U << 3) + 7) << 21,
648   BC1 = ((1U << 3) + 0) << 21,
649   S = ((2U << 3) + 0) << 21,
650   D = ((2U << 3) + 1) << 21,
651   W = ((2U << 3) + 4) << 21,
652   L = ((2U << 3) + 5) << 21,
653   PS = ((2U << 3) + 6) << 21,
654   // COP1 Encoding of Function Field When rs=S.
655 
656   ADD_S = ((0U << 3) + 0),
657   SUB_S = ((0U << 3) + 1),
658   MUL_S = ((0U << 3) + 2),
659   DIV_S = ((0U << 3) + 3),
660   ABS_S = ((0U << 3) + 5),
661   SQRT_S = ((0U << 3) + 4),
662   MOV_S = ((0U << 3) + 6),
663   NEG_S = ((0U << 3) + 7),
664   ROUND_L_S = ((1U << 3) + 0),
665   TRUNC_L_S = ((1U << 3) + 1),
666   CEIL_L_S = ((1U << 3) + 2),
667   FLOOR_L_S = ((1U << 3) + 3),
668   ROUND_W_S = ((1U << 3) + 4),
669   TRUNC_W_S = ((1U << 3) + 5),
670   CEIL_W_S = ((1U << 3) + 6),
671   FLOOR_W_S = ((1U << 3) + 7),
672   RECIP_S = ((2U << 3) + 5),
673   RSQRT_S = ((2U << 3) + 6),
674   MADDF_S = ((3U << 3) + 0),
675   MSUBF_S = ((3U << 3) + 1),
676   CLASS_S = ((3U << 3) + 3),
677   CVT_D_S = ((4U << 3) + 1),
678   CVT_W_S = ((4U << 3) + 4),
679   CVT_L_S = ((4U << 3) + 5),
680   CVT_PS_S = ((4U << 3) + 6),
681   // COP1 Encoding of Function Field When rs=D.
682   ADD_D = ((0U << 3) + 0),
683   SUB_D = ((0U << 3) + 1),
684   MUL_D = ((0U << 3) + 2),
685   DIV_D = ((0U << 3) + 3),
686   SQRT_D = ((0U << 3) + 4),
687   ABS_D = ((0U << 3) + 5),
688   MOV_D = ((0U << 3) + 6),
689   NEG_D = ((0U << 3) + 7),
690   ROUND_L_D = ((1U << 3) + 0),
691   TRUNC_L_D = ((1U << 3) + 1),
692   CEIL_L_D = ((1U << 3) + 2),
693   FLOOR_L_D = ((1U << 3) + 3),
694   ROUND_W_D = ((1U << 3) + 4),
695   TRUNC_W_D = ((1U << 3) + 5),
696   CEIL_W_D = ((1U << 3) + 6),
697   FLOOR_W_D = ((1U << 3) + 7),
698   RECIP_D = ((2U << 3) + 5),
699   RSQRT_D = ((2U << 3) + 6),
700   MADDF_D = ((3U << 3) + 0),
701   MSUBF_D = ((3U << 3) + 1),
702   CLASS_D = ((3U << 3) + 3),
703   MIN = ((3U << 3) + 4),
704   MINA = ((3U << 3) + 5),
705   MAX = ((3U << 3) + 6),
706   MAXA = ((3U << 3) + 7),
707   CVT_S_D = ((4U << 3) + 0),
708   CVT_W_D = ((4U << 3) + 4),
709   CVT_L_D = ((4U << 3) + 5),
710   C_F_D = ((6U << 3) + 0),
711   C_UN_D = ((6U << 3) + 1),
712   C_EQ_D = ((6U << 3) + 2),
713   C_UEQ_D = ((6U << 3) + 3),
714   C_OLT_D = ((6U << 3) + 4),
715   C_ULT_D = ((6U << 3) + 5),
716   C_OLE_D = ((6U << 3) + 6),
717   C_ULE_D = ((6U << 3) + 7),
718 
719   // COP1 Encoding of Function Field When rs=W or L.
720   CVT_S_W = ((4U << 3) + 0),
721   CVT_D_W = ((4U << 3) + 1),
722   CVT_S_L = ((4U << 3) + 0),
723   CVT_D_L = ((4U << 3) + 1),
724   BC1EQZ = ((2U << 2) + 1) << 21,
725   BC1NEZ = ((3U << 2) + 1) << 21,
726   // COP1 CMP positive predicates Bit 5..4 = 00.
727   CMP_AF = ((0U << 3) + 0),
728   CMP_UN = ((0U << 3) + 1),
729   CMP_EQ = ((0U << 3) + 2),
730   CMP_UEQ = ((0U << 3) + 3),
731   CMP_LT = ((0U << 3) + 4),
732   CMP_ULT = ((0U << 3) + 5),
733   CMP_LE = ((0U << 3) + 6),
734   CMP_ULE = ((0U << 3) + 7),
735   CMP_SAF = ((1U << 3) + 0),
736   CMP_SUN = ((1U << 3) + 1),
737   CMP_SEQ = ((1U << 3) + 2),
738   CMP_SUEQ = ((1U << 3) + 3),
739   CMP_SSLT = ((1U << 3) + 4),
740   CMP_SSULT = ((1U << 3) + 5),
741   CMP_SLE = ((1U << 3) + 6),
742   CMP_SULE = ((1U << 3) + 7),
743   // COP1 CMP negative predicates Bit 5..4 = 01.
744   CMP_AT = ((2U << 3) + 0),  // Reserved, not implemented.
745   CMP_OR = ((2U << 3) + 1),
746   CMP_UNE = ((2U << 3) + 2),
747   CMP_NE = ((2U << 3) + 3),
748   CMP_UGE = ((2U << 3) + 4),  // Reserved, not implemented.
749   CMP_OGE = ((2U << 3) + 5),  // Reserved, not implemented.
750   CMP_UGT = ((2U << 3) + 6),  // Reserved, not implemented.
751   CMP_OGT = ((2U << 3) + 7),  // Reserved, not implemented.
752   CMP_SAT = ((3U << 3) + 0),  // Reserved, not implemented.
753   CMP_SOR = ((3U << 3) + 1),
754   CMP_SUNE = ((3U << 3) + 2),
755   CMP_SNE = ((3U << 3) + 3),
756   CMP_SUGE = ((3U << 3) + 4),  // Reserved, not implemented.
757   CMP_SOGE = ((3U << 3) + 5),  // Reserved, not implemented.
758   CMP_SUGT = ((3U << 3) + 6),  // Reserved, not implemented.
759   CMP_SOGT = ((3U << 3) + 7),  // Reserved, not implemented.
760 
761   SEL = ((2U << 3) + 0),
762   MOVF = ((2U << 3) + 1),      // Function field for MOVT.fmt and MOVF.fmt
763   MOVZ_C = ((2U << 3) + 2),    // COP1 on FPR registers.
764   MOVN_C = ((2U << 3) + 3),    // COP1 on FPR registers.
765   SELEQZ_C = ((2U << 3) + 4),  // COP1 on FPR registers.
766   SELNEZ_C = ((2U << 3) + 7),  // COP1 on FPR registers.
767 
768   // COP1 Encoding of Function Field When rs=PS.
769 
770   // COP1X Encoding of Function Field.
771   MADD_S = ((4U << 3) + 0),
772   MADD_D = ((4U << 3) + 1),
773   MSUB_S = ((5U << 3) + 0),
774   MSUB_D = ((5U << 3) + 1),
775 
776   // PCREL Encoding of rt Field.
777   ADDIUPC = ((0U << 2) + 0),
778   LWPC = ((0U << 2) + 1),
779   LWUPC = ((0U << 2) + 2),
780   LDPC = ((0U << 3) + 6),
781   // reserved ((1U << 3) + 6),
782   AUIPC = ((3U << 3) + 6),
783   ALUIPC = ((3U << 3) + 7),
784 
785   // POP66 Encoding of rs Field.
786   JIC = ((0U << 5) + 0),
787 
788   // POP76 Encoding of rs Field.
789   JIALC = ((0U << 5) + 0),
790 
791   // COP1 Encoding of rs Field for MSA Branch Instructions
792   BZ_V = (((1U << 3) + 3) << kRsShift),
793   BNZ_V = (((1U << 3) + 7) << kRsShift),
794   BZ_B = (((3U << 3) + 0) << kRsShift),
795   BZ_H = (((3U << 3) + 1) << kRsShift),
796   BZ_W = (((3U << 3) + 2) << kRsShift),
797   BZ_D = (((3U << 3) + 3) << kRsShift),
798   BNZ_B = (((3U << 3) + 4) << kRsShift),
799   BNZ_H = (((3U << 3) + 5) << kRsShift),
800   BNZ_W = (((3U << 3) + 6) << kRsShift),
801   BNZ_D = (((3U << 3) + 7) << kRsShift),
802 
803   // MSA: Operation Field for MI10 Instruction Formats
804   MSA_LD = (8U << 2),
805   MSA_ST = (9U << 2),
806   LD_B = ((8U << 2) + 0),
807   LD_H = ((8U << 2) + 1),
808   LD_W = ((8U << 2) + 2),
809   LD_D = ((8U << 2) + 3),
810   ST_B = ((9U << 2) + 0),
811   ST_H = ((9U << 2) + 1),
812   ST_W = ((9U << 2) + 2),
813   ST_D = ((9U << 2) + 3),
814 
815   // MSA: Operation Field for I5 Instruction Format
816   ADDVI = ((0U << 23) + 6),
817   SUBVI = ((1U << 23) + 6),
818   MAXI_S = ((2U << 23) + 6),
819   MAXI_U = ((3U << 23) + 6),
820   MINI_S = ((4U << 23) + 6),
821   MINI_U = ((5U << 23) + 6),
822   CEQI = ((0U << 23) + 7),
823   CLTI_S = ((2U << 23) + 7),
824   CLTI_U = ((3U << 23) + 7),
825   CLEI_S = ((4U << 23) + 7),
826   CLEI_U = ((5U << 23) + 7),
827   LDI = ((6U << 23) + 7),  // I10 instruction format
828   I5_DF_b = (0U << 21),
829   I5_DF_h = (1U << 21),
830   I5_DF_w = (2U << 21),
831   I5_DF_d = (3U << 21),
832 
833   // MSA: Operation Field for I8 Instruction Format
834   ANDI_B = ((0U << 24) + 0),
835   ORI_B = ((1U << 24) + 0),
836   NORI_B = ((2U << 24) + 0),
837   XORI_B = ((3U << 24) + 0),
838   BMNZI_B = ((0U << 24) + 1),
839   BMZI_B = ((1U << 24) + 1),
840   BSELI_B = ((2U << 24) + 1),
841   SHF_B = ((0U << 24) + 2),
842   SHF_H = ((1U << 24) + 2),
843   SHF_W = ((2U << 24) + 2),
844 
845   MSA_VEC_2R_2RF_MINOR = ((3U << 3) + 6),
846 
847   // MSA: Operation Field for VEC Instruction Formats
848   AND_V = (((0U << 2) + 0) << 21),
849   OR_V = (((0U << 2) + 1) << 21),
850   NOR_V = (((0U << 2) + 2) << 21),
851   XOR_V = (((0U << 2) + 3) << 21),
852   BMNZ_V = (((1U << 2) + 0) << 21),
853   BMZ_V = (((1U << 2) + 1) << 21),
854   BSEL_V = (((1U << 2) + 2) << 21),
855 
856   // MSA: Operation Field for 2R Instruction Formats
857   MSA_2R_FORMAT = (((6U << 2) + 0) << 21),
858   FILL = (0U << 18),
859   PCNT = (1U << 18),
860   NLOC = (2U << 18),
861   NLZC = (3U << 18),
862   MSA_2R_DF_b = (0U << 16),
863   MSA_2R_DF_h = (1U << 16),
864   MSA_2R_DF_w = (2U << 16),
865   MSA_2R_DF_d = (3U << 16),
866 
867   // MSA: Operation Field for 2RF Instruction Formats
868   MSA_2RF_FORMAT = (((6U << 2) + 1) << 21),
869   FCLASS = (0U << 17),
870   FTRUNC_S = (1U << 17),
871   FTRUNC_U = (2U << 17),
872   FSQRT = (3U << 17),
873   FRSQRT = (4U << 17),
874   FRCP = (5U << 17),
875   FRINT = (6U << 17),
876   FLOG2 = (7U << 17),
877   FEXUPL = (8U << 17),
878   FEXUPR = (9U << 17),
879   FFQL = (10U << 17),
880   FFQR = (11U << 17),
881   FTINT_S = (12U << 17),
882   FTINT_U = (13U << 17),
883   FFINT_S = (14U << 17),
884   FFINT_U = (15U << 17),
885   MSA_2RF_DF_w = (0U << 16),
886   MSA_2RF_DF_d = (1U << 16),
887 
888   // MSA: Operation Field for 3R Instruction Format
889   SLL_MSA = ((0U << 23) + 13),
890   SRA_MSA = ((1U << 23) + 13),
891   SRL_MSA = ((2U << 23) + 13),
892   BCLR = ((3U << 23) + 13),
893   BSET = ((4U << 23) + 13),
894   BNEG = ((5U << 23) + 13),
895   BINSL = ((6U << 23) + 13),
896   BINSR = ((7U << 23) + 13),
897   ADDV = ((0U << 23) + 14),
898   SUBV = ((1U << 23) + 14),
899   MAX_S = ((2U << 23) + 14),
900   MAX_U = ((3U << 23) + 14),
901   MIN_S = ((4U << 23) + 14),
902   MIN_U = ((5U << 23) + 14),
903   MAX_A = ((6U << 23) + 14),
904   MIN_A = ((7U << 23) + 14),
905   CEQ = ((0U << 23) + 15),
906   CLT_S = ((2U << 23) + 15),
907   CLT_U = ((3U << 23) + 15),
908   CLE_S = ((4U << 23) + 15),
909   CLE_U = ((5U << 23) + 15),
910   ADD_A = ((0U << 23) + 16),
911   ADDS_A = ((1U << 23) + 16),
912   ADDS_S = ((2U << 23) + 16),
913   ADDS_U = ((3U << 23) + 16),
914   AVE_S = ((4U << 23) + 16),
915   AVE_U = ((5U << 23) + 16),
916   AVER_S = ((6U << 23) + 16),
917   AVER_U = ((7U << 23) + 16),
918   SUBS_S = ((0U << 23) + 17),
919   SUBS_U = ((1U << 23) + 17),
920   SUBSUS_U = ((2U << 23) + 17),
921   SUBSUU_S = ((3U << 23) + 17),
922   ASUB_S = ((4U << 23) + 17),
923   ASUB_U = ((5U << 23) + 17),
924   MULV = ((0U << 23) + 18),
925   MADDV = ((1U << 23) + 18),
926   MSUBV = ((2U << 23) + 18),
927   DIV_S_MSA = ((4U << 23) + 18),
928   DIV_U = ((5U << 23) + 18),
929   MOD_S = ((6U << 23) + 18),
930   MOD_U = ((7U << 23) + 18),
931   DOTP_S = ((0U << 23) + 19),
932   DOTP_U = ((1U << 23) + 19),
933   DPADD_S = ((2U << 23) + 19),
934   DPADD_U = ((3U << 23) + 19),
935   DPSUB_S = ((4U << 23) + 19),
936   DPSUB_U = ((5U << 23) + 19),
937   SLD = ((0U << 23) + 20),
938   SPLAT = ((1U << 23) + 20),
939   PCKEV = ((2U << 23) + 20),
940   PCKOD = ((3U << 23) + 20),
941   ILVL = ((4U << 23) + 20),
942   ILVR = ((5U << 23) + 20),
943   ILVEV = ((6U << 23) + 20),
944   ILVOD = ((7U << 23) + 20),
945   VSHF = ((0U << 23) + 21),
946   SRAR = ((1U << 23) + 21),
947   SRLR = ((2U << 23) + 21),
948   HADD_S = ((4U << 23) + 21),
949   HADD_U = ((5U << 23) + 21),
950   HSUB_S = ((6U << 23) + 21),
951   HSUB_U = ((7U << 23) + 21),
952   MSA_3R_DF_b = (0U << 21),
953   MSA_3R_DF_h = (1U << 21),
954   MSA_3R_DF_w = (2U << 21),
955   MSA_3R_DF_d = (3U << 21),
956 
957   // MSA: Operation Field for 3RF Instruction Format
958   FCAF = ((0U << 22) + 26),
959   FCUN = ((1U << 22) + 26),
960   FCEQ = ((2U << 22) + 26),
961   FCUEQ = ((3U << 22) + 26),
962   FCLT = ((4U << 22) + 26),
963   FCULT = ((5U << 22) + 26),
964   FCLE = ((6U << 22) + 26),
965   FCULE = ((7U << 22) + 26),
966   FSAF = ((8U << 22) + 26),
967   FSUN = ((9U << 22) + 26),
968   FSEQ = ((10U << 22) + 26),
969   FSUEQ = ((11U << 22) + 26),
970   FSLT = ((12U << 22) + 26),
971   FSULT = ((13U << 22) + 26),
972   FSLE = ((14U << 22) + 26),
973   FSULE = ((15U << 22) + 26),
974   FADD = ((0U << 22) + 27),
975   FSUB = ((1U << 22) + 27),
976   FMUL = ((2U << 22) + 27),
977   FDIV = ((3U << 22) + 27),
978   FMADD = ((4U << 22) + 27),
979   FMSUB = ((5U << 22) + 27),
980   FEXP2 = ((7U << 22) + 27),
981   FEXDO = ((8U << 22) + 27),
982   FTQ = ((10U << 22) + 27),
983   FMIN = ((12U << 22) + 27),
984   FMIN_A = ((13U << 22) + 27),
985   FMAX = ((14U << 22) + 27),
986   FMAX_A = ((15U << 22) + 27),
987   FCOR = ((1U << 22) + 28),
988   FCUNE = ((2U << 22) + 28),
989   FCNE = ((3U << 22) + 28),
990   MUL_Q = ((4U << 22) + 28),
991   MADD_Q = ((5U << 22) + 28),
992   MSUB_Q = ((6U << 22) + 28),
993   FSOR = ((9U << 22) + 28),
994   FSUNE = ((10U << 22) + 28),
995   FSNE = ((11U << 22) + 28),
996   MULR_Q = ((12U << 22) + 28),
997   MADDR_Q = ((13U << 22) + 28),
998   MSUBR_Q = ((14U << 22) + 28),
999 
1000   // MSA: Operation Field for ELM Instruction Format
1001   MSA_ELM_MINOR = ((3U << 3) + 1),
1002   SLDI = (0U << 22),
1003   CTCMSA = ((0U << 22) | (62U << 16)),
1004   SPLATI = (1U << 22),
1005   CFCMSA = ((1U << 22) | (62U << 16)),
1006   COPY_S = (2U << 22),
1007   MOVE_V = ((2U << 22) | (62U << 16)),
1008   COPY_U = (3U << 22),
1009   INSERT = (4U << 22),
1010   INSVE = (5U << 22),
1011   ELM_DF_B = ((0U << 4) << 16),
1012   ELM_DF_H = ((4U << 3) << 16),
1013   ELM_DF_W = ((12U << 2) << 16),
1014   ELM_DF_D = ((28U << 1) << 16),
1015 
1016   // MSA: Operation Field for BIT Instruction Format
1017   SLLI = ((0U << 23) + 9),
1018   SRAI = ((1U << 23) + 9),
1019   SRLI = ((2U << 23) + 9),
1020   BCLRI = ((3U << 23) + 9),
1021   BSETI = ((4U << 23) + 9),
1022   BNEGI = ((5U << 23) + 9),
1023   BINSLI = ((6U << 23) + 9),
1024   BINSRI = ((7U << 23) + 9),
1025   SAT_S = ((0U << 23) + 10),
1026   SAT_U = ((1U << 23) + 10),
1027   SRARI = ((2U << 23) + 10),
1028   SRLRI = ((3U << 23) + 10),
1029   BIT_DF_b = ((14U << 3) << 16),
1030   BIT_DF_h = ((6U << 4) << 16),
1031   BIT_DF_w = ((2U << 5) << 16),
1032   BIT_DF_d = ((0U << 6) << 16),
1033 
1034   nullptrSF = 0U
1035 };
1036 
1037 enum MSAMinorOpcode : uint32_t {
1038   kMsaMinorUndefined = 0,
1039   kMsaMinorI8,
1040   kMsaMinorI5,
1041   kMsaMinorI10,
1042   kMsaMinorBIT,
1043   kMsaMinor3R,
1044   kMsaMinor3RF,
1045   kMsaMinorELM,
1046   kMsaMinorVEC,
1047   kMsaMinor2R,
1048   kMsaMinor2RF,
1049   kMsaMinorMI10
1050 };
1051 
1052 // ----- Emulated conditions.
1053 // On MIPS we use this enum to abstract from conditional branch instructions.
1054 // The 'U' prefix is used to specify unsigned comparisons.
1055 // Opposite conditions must be paired as odd/even numbers
1056 // because 'NegateCondition' function flips LSB to negate condition.
1057 enum Condition {
1058   // Any value < 0 is considered no_condition.
1059   kNoCondition = -1,
1060   overflow = 0,
1061   no_overflow = 1,
1062   Uless = 2,
1063   Ugreater_equal = 3,
1064   Uless_equal = 4,
1065   Ugreater = 5,
1066   equal = 6,
1067   not_equal = 7,  // Unordered or Not Equal.
1068   negative = 8,
1069   positive = 9,
1070   parity_even = 10,
1071   parity_odd = 11,
1072   less = 12,
1073   greater_equal = 13,
1074   less_equal = 14,
1075   greater = 15,
1076   ueq = 16,  // Unordered or Equal.
1077   ogl = 17,  // Ordered and Not Equal.
1078   cc_always = 18,
1079 
1080   // Aliases.
1081   carry = Uless,
1082   not_carry = Ugreater_equal,
1083   zero = equal,
1084   eq = equal,
1085   not_zero = not_equal,
1086   ne = not_equal,
1087   nz = not_equal,
1088   sign = negative,
1089   not_sign = positive,
1090   mi = negative,
1091   pl = positive,
1092   hi = Ugreater,
1093   ls = Uless_equal,
1094   ge = greater_equal,
1095   lt = less,
1096   gt = greater,
1097   le = less_equal,
1098   hs = Ugreater_equal,
1099   lo = Uless,
1100   al = cc_always,
1101   ult = Uless,
1102   uge = Ugreater_equal,
1103   ule = Uless_equal,
1104   ugt = Ugreater,
1105   cc_default = kNoCondition
1106 };
1107 
1108 // Returns the equivalent of !cc.
1109 // Negation of the default kNoCondition (-1) results in a non-default
1110 // no_condition value (-2). As long as tests for no_condition check
1111 // for condition < 0, this will work as expected.
NegateCondition(Condition cc)1112 inline Condition NegateCondition(Condition cc) {
1113   DCHECK(cc != cc_always);
1114   return static_cast<Condition>(cc ^ 1);
1115 }
1116 
NegateFpuCondition(Condition cc)1117 inline Condition NegateFpuCondition(Condition cc) {
1118   DCHECK(cc != cc_always);
1119   switch (cc) {
1120     case ult:
1121       return ge;
1122     case ugt:
1123       return le;
1124     case uge:
1125       return lt;
1126     case ule:
1127       return gt;
1128     case lt:
1129       return uge;
1130     case gt:
1131       return ule;
1132     case ge:
1133       return ult;
1134     case le:
1135       return ugt;
1136     case eq:
1137       return ne;
1138     case ne:
1139       return eq;
1140     case ueq:
1141       return ogl;
1142     case ogl:
1143       return ueq;
1144     default:
1145       return cc;
1146   }
1147 }
1148 
1149 enum MSABranchCondition {
1150   all_not_zero = 0,   // Branch If All Elements Are Not Zero
1151   one_elem_not_zero,  // Branch If At Least One Element of Any Format Is Not
1152                       // Zero
1153   one_elem_zero,      // Branch If At Least One Element Is Zero
1154   all_zero            // Branch If All Elements of Any Format Are Zero
1155 };
1156 
NegateMSABranchCondition(MSABranchCondition cond)1157 inline MSABranchCondition NegateMSABranchCondition(MSABranchCondition cond) {
1158   switch (cond) {
1159     case all_not_zero:
1160       return one_elem_zero;
1161     case one_elem_not_zero:
1162       return all_zero;
1163     case one_elem_zero:
1164       return all_not_zero;
1165     case all_zero:
1166       return one_elem_not_zero;
1167     default:
1168       return cond;
1169   }
1170 }
1171 
1172 enum MSABranchDF {
1173   MSA_BRANCH_B = 0,
1174   MSA_BRANCH_H,
1175   MSA_BRANCH_W,
1176   MSA_BRANCH_D,
1177   MSA_BRANCH_V
1178 };
1179 
1180 // ----- Coprocessor conditions.
1181 enum FPUCondition {
1182   kNoFPUCondition = -1,
1183 
1184   F = 0x00,    // False.
1185   UN = 0x01,   // Unordered.
1186   EQ = 0x02,   // Equal.
1187   UEQ = 0x03,  // Unordered or Equal.
1188   OLT = 0x04,  // Ordered or Less Than, on Mips release < 6.
1189   LT = 0x04,   // Ordered or Less Than, on Mips release >= 6.
1190   ULT = 0x05,  // Unordered or Less Than.
1191   OLE = 0x06,  // Ordered or Less Than or Equal, on Mips release < 6.
1192   LE = 0x06,   // Ordered or Less Than or Equal, on Mips release >= 6.
1193   ULE = 0x07,  // Unordered or Less Than or Equal.
1194 
1195   // Following constants are available on Mips release >= 6 only.
1196   ORD = 0x11,  // Ordered, on Mips release >= 6.
1197   UNE = 0x12,  // Not equal, on Mips release >= 6.
1198   NE = 0x13,   // Ordered Greater Than or Less Than. on Mips >= 6 only.
1199 };
1200 
1201 // FPU rounding modes.
1202 enum FPURoundingMode {
1203   RN = 0 << 0,  // Round to Nearest.
1204   RZ = 1 << 0,  // Round towards zero.
1205   RP = 2 << 0,  // Round towards Plus Infinity.
1206   RM = 3 << 0,  // Round towards Minus Infinity.
1207 
1208   // Aliases.
1209   kRoundToNearest = RN,
1210   kRoundToZero = RZ,
1211   kRoundToPlusInf = RP,
1212   kRoundToMinusInf = RM,
1213 
1214   mode_round = RN,
1215   mode_ceil = RP,
1216   mode_floor = RM,
1217   mode_trunc = RZ
1218 };
1219 
1220 const uint32_t kFPURoundingModeMask = 3 << 0;
1221 
1222 enum CheckForInexactConversion {
1223   kCheckForInexactConversion,
1224   kDontCheckForInexactConversion
1225 };
1226 
1227 enum class MaxMinKind : int { kMin = 0, kMax = 1 };
1228 
1229 // -----------------------------------------------------------------------------
1230 // Hints.
1231 
1232 // Branch hints are not used on the MIPS.  They are defined so that they can
1233 // appear in shared function signatures, but will be ignored in MIPS
1234 // implementations.
1235 enum Hint { no_hint = 0 };
1236 
NegateHint(Hint hint)1237 inline Hint NegateHint(Hint hint) { return no_hint; }
1238 
1239 // -----------------------------------------------------------------------------
1240 // Specific instructions, constants, and masks.
1241 // These constants are declared in assembler-mips.cc, as they use named
1242 // registers and other constants.
1243 
1244 // addiu(sp, sp, 4) aka Pop() operation or part of Pop(r)
1245 // operations as post-increment of sp.
1246 extern const Instr kPopInstruction;
1247 // addiu(sp, sp, -4) part of Push(r) operation as pre-decrement of sp.
1248 extern const Instr kPushInstruction;
1249 // Sw(r, MemOperand(sp, 0))
1250 extern const Instr kPushRegPattern;
1251 // Lw(r, MemOperand(sp, 0))
1252 extern const Instr kPopRegPattern;
1253 extern const Instr kLwRegFpOffsetPattern;
1254 extern const Instr kSwRegFpOffsetPattern;
1255 extern const Instr kLwRegFpNegOffsetPattern;
1256 extern const Instr kSwRegFpNegOffsetPattern;
1257 // A mask for the Rt register for push, pop, lw, sw instructions.
1258 extern const Instr kRtMask;
1259 extern const Instr kLwSwInstrTypeMask;
1260 extern const Instr kLwSwInstrArgumentMask;
1261 extern const Instr kLwSwOffsetMask;
1262 
1263 // Break 0xfffff, reserved for redirected real time call.
1264 const Instr rtCallRedirInstr = SPECIAL | BREAK | call_rt_redirected << 6;
1265 // A nop instruction. (Encoding of sll 0 0 0).
1266 const Instr nopInstr = 0;
1267 
OpcodeToBitNumber(Opcode opcode)1268 static constexpr uint64_t OpcodeToBitNumber(Opcode opcode) {
1269   return 1ULL << (static_cast<uint32_t>(opcode) >> kOpcodeShift);
1270 }
1271 
1272 constexpr uint8_t kInstrSize = 4;
1273 constexpr uint8_t kInstrSizeLog2 = 2;
1274 
1275 class InstructionBase {
1276  public:
1277   enum {
1278     // On MIPS PC cannot actually be directly accessed. We behave as if PC was
1279     // always the value of the current instruction being executed.
1280     kPCReadOffset = 0
1281   };
1282 
1283   // Instruction type.
1284   enum Type { kRegisterType, kImmediateType, kJumpType, kUnsupported = -1 };
1285 
1286   // Get the raw instruction bits.
InstructionBits()1287   inline Instr InstructionBits() const {
1288     return *reinterpret_cast<const Instr*>(this);
1289   }
1290 
1291   // Set the raw instruction bits to value.
SetInstructionBits(Instr value)1292   inline void SetInstructionBits(Instr value) {
1293     *reinterpret_cast<Instr*>(this) = value;
1294   }
1295 
1296   // Read one particular bit out of the instruction bits.
Bit(int nr)1297   inline int Bit(int nr) const { return (InstructionBits() >> nr) & 1; }
1298 
1299   // Read a bit field out of the instruction bits.
Bits(int hi,int lo)1300   inline int Bits(int hi, int lo) const {
1301     return (InstructionBits() >> lo) & ((2U << (hi - lo)) - 1);
1302   }
1303 
1304   static constexpr uint64_t kOpcodeImmediateTypeMask =
1305       OpcodeToBitNumber(REGIMM) | OpcodeToBitNumber(BEQ) |
1306       OpcodeToBitNumber(BNE) | OpcodeToBitNumber(BLEZ) |
1307       OpcodeToBitNumber(BGTZ) | OpcodeToBitNumber(ADDI) |
1308       OpcodeToBitNumber(DADDI) | OpcodeToBitNumber(ADDIU) |
1309       OpcodeToBitNumber(DADDIU) | OpcodeToBitNumber(SLTI) |
1310       OpcodeToBitNumber(SLTIU) | OpcodeToBitNumber(ANDI) |
1311       OpcodeToBitNumber(ORI) | OpcodeToBitNumber(XORI) |
1312       OpcodeToBitNumber(LUI) | OpcodeToBitNumber(BEQL) |
1313       OpcodeToBitNumber(BNEL) | OpcodeToBitNumber(BLEZL) |
1314       OpcodeToBitNumber(BGTZL) | OpcodeToBitNumber(POP66) |
1315       OpcodeToBitNumber(POP76) | OpcodeToBitNumber(LB) | OpcodeToBitNumber(LH) |
1316       OpcodeToBitNumber(LWL) | OpcodeToBitNumber(LW) | OpcodeToBitNumber(LWU) |
1317       OpcodeToBitNumber(LD) | OpcodeToBitNumber(LBU) | OpcodeToBitNumber(LHU) |
1318       OpcodeToBitNumber(LDL) | OpcodeToBitNumber(LDR) | OpcodeToBitNumber(LWR) |
1319       OpcodeToBitNumber(SDL) | OpcodeToBitNumber(SB) | OpcodeToBitNumber(SH) |
1320       OpcodeToBitNumber(SWL) | OpcodeToBitNumber(SW) | OpcodeToBitNumber(SD) |
1321       OpcodeToBitNumber(SWR) | OpcodeToBitNumber(SDR) |
1322       OpcodeToBitNumber(LWC1) | OpcodeToBitNumber(LDC1) |
1323       OpcodeToBitNumber(SWC1) | OpcodeToBitNumber(SDC1) |
1324       OpcodeToBitNumber(PCREL) | OpcodeToBitNumber(DAUI) |
1325       OpcodeToBitNumber(BC) | OpcodeToBitNumber(BALC);
1326 
1327 #define FunctionFieldToBitNumber(function) (1ULL << function)
1328 
1329   // On r6, DCLZ_R6 aliases to existing MFLO.
1330   static const uint64_t kFunctionFieldRegisterTypeMask =
1331       FunctionFieldToBitNumber(JR) | FunctionFieldToBitNumber(JALR) |
1332       FunctionFieldToBitNumber(BREAK) | FunctionFieldToBitNumber(SLL) |
1333       FunctionFieldToBitNumber(DSLL) | FunctionFieldToBitNumber(DSLL32) |
1334       FunctionFieldToBitNumber(SRL) | FunctionFieldToBitNumber(DSRL) |
1335       FunctionFieldToBitNumber(DSRL32) | FunctionFieldToBitNumber(SRA) |
1336       FunctionFieldToBitNumber(DSRA) | FunctionFieldToBitNumber(DSRA32) |
1337       FunctionFieldToBitNumber(SLLV) | FunctionFieldToBitNumber(DSLLV) |
1338       FunctionFieldToBitNumber(SRLV) | FunctionFieldToBitNumber(DSRLV) |
1339       FunctionFieldToBitNumber(SRAV) | FunctionFieldToBitNumber(DSRAV) |
1340       FunctionFieldToBitNumber(LSA) | FunctionFieldToBitNumber(DLSA) |
1341       FunctionFieldToBitNumber(MFHI) | FunctionFieldToBitNumber(MFLO) |
1342       FunctionFieldToBitNumber(MULT) | FunctionFieldToBitNumber(DMULT) |
1343       FunctionFieldToBitNumber(MULTU) | FunctionFieldToBitNumber(DMULTU) |
1344       FunctionFieldToBitNumber(DIV) | FunctionFieldToBitNumber(DDIV) |
1345       FunctionFieldToBitNumber(DIVU) | FunctionFieldToBitNumber(DDIVU) |
1346       FunctionFieldToBitNumber(ADD) | FunctionFieldToBitNumber(DADD) |
1347       FunctionFieldToBitNumber(ADDU) | FunctionFieldToBitNumber(DADDU) |
1348       FunctionFieldToBitNumber(SUB) | FunctionFieldToBitNumber(DSUB) |
1349       FunctionFieldToBitNumber(SUBU) | FunctionFieldToBitNumber(DSUBU) |
1350       FunctionFieldToBitNumber(AND) | FunctionFieldToBitNumber(OR) |
1351       FunctionFieldToBitNumber(XOR) | FunctionFieldToBitNumber(NOR) |
1352       FunctionFieldToBitNumber(SLT) | FunctionFieldToBitNumber(SLTU) |
1353       FunctionFieldToBitNumber(TGE) | FunctionFieldToBitNumber(TGEU) |
1354       FunctionFieldToBitNumber(TLT) | FunctionFieldToBitNumber(TLTU) |
1355       FunctionFieldToBitNumber(TEQ) | FunctionFieldToBitNumber(TNE) |
1356       FunctionFieldToBitNumber(MOVZ) | FunctionFieldToBitNumber(MOVN) |
1357       FunctionFieldToBitNumber(MOVCI) | FunctionFieldToBitNumber(SELEQZ_S) |
1358       FunctionFieldToBitNumber(SELNEZ_S) | FunctionFieldToBitNumber(SYNC);
1359 
1360   // Accessors for the different named fields used in the MIPS encoding.
OpcodeValue()1361   inline Opcode OpcodeValue() const {
1362     return static_cast<Opcode>(
1363         Bits(kOpcodeShift + kOpcodeBits - 1, kOpcodeShift));
1364   }
1365 
FunctionFieldRaw()1366   inline int FunctionFieldRaw() const {
1367     return InstructionBits() & kFunctionFieldMask;
1368   }
1369 
1370   // Return the fields at their original place in the instruction encoding.
OpcodeFieldRaw()1371   inline Opcode OpcodeFieldRaw() const {
1372     return static_cast<Opcode>(InstructionBits() & kOpcodeMask);
1373   }
1374 
1375   // Safe to call within InstructionType().
RsFieldRawNoAssert()1376   inline int RsFieldRawNoAssert() const {
1377     return InstructionBits() & kRsFieldMask;
1378   }
1379 
SaFieldRaw()1380   inline int SaFieldRaw() const { return InstructionBits() & kSaFieldMask; }
1381 
1382   // Get the encoding type of the instruction.
1383   inline Type InstructionType() const;
1384 
MSAMinorOpcodeField()1385   inline MSAMinorOpcode MSAMinorOpcodeField() const {
1386     int op = this->FunctionFieldRaw();
1387     switch (op) {
1388       case 0:
1389       case 1:
1390       case 2:
1391         return kMsaMinorI8;
1392       case 6:
1393         return kMsaMinorI5;
1394       case 7:
1395         return (((this->InstructionBits() & kMsaI5I10Mask) == LDI)
1396                     ? kMsaMinorI10
1397                     : kMsaMinorI5);
1398       case 9:
1399       case 10:
1400         return kMsaMinorBIT;
1401       case 13:
1402       case 14:
1403       case 15:
1404       case 16:
1405       case 17:
1406       case 18:
1407       case 19:
1408       case 20:
1409       case 21:
1410         return kMsaMinor3R;
1411       case 25:
1412         return kMsaMinorELM;
1413       case 26:
1414       case 27:
1415       case 28:
1416         return kMsaMinor3RF;
1417       case 30:
1418         switch (this->RsFieldRawNoAssert()) {
1419           case MSA_2R_FORMAT:
1420             return kMsaMinor2R;
1421           case MSA_2RF_FORMAT:
1422             return kMsaMinor2RF;
1423           default:
1424             return kMsaMinorVEC;
1425         }
1426         break;
1427       case 32:
1428       case 33:
1429       case 34:
1430       case 35:
1431       case 36:
1432       case 37:
1433       case 38:
1434       case 39:
1435         return kMsaMinorMI10;
1436       default:
1437         return kMsaMinorUndefined;
1438     }
1439   }
1440 
1441  protected:
InstructionBase()1442   InstructionBase() {}
1443 };
1444 
1445 template <class T>
1446 class InstructionGetters : public T {
1447  public:
RsValue()1448   inline int RsValue() const {
1449     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1450            this->InstructionType() == InstructionBase::kImmediateType);
1451     return this->Bits(kRsShift + kRsBits - 1, kRsShift);
1452   }
1453 
RtValue()1454   inline int RtValue() const {
1455     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1456            this->InstructionType() == InstructionBase::kImmediateType);
1457     return this->Bits(kRtShift + kRtBits - 1, kRtShift);
1458   }
1459 
RdValue()1460   inline int RdValue() const {
1461     DCHECK_EQ(this->InstructionType(), InstructionBase::kRegisterType);
1462     return this->Bits(kRdShift + kRdBits - 1, kRdShift);
1463   }
1464 
BaseValue()1465   inline int BaseValue() const {
1466     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1467     return this->Bits(kBaseShift + kBaseBits - 1, kBaseShift);
1468   }
1469 
SaValue()1470   inline int SaValue() const {
1471     DCHECK_EQ(this->InstructionType(), InstructionBase::kRegisterType);
1472     return this->Bits(kSaShift + kSaBits - 1, kSaShift);
1473   }
1474 
LsaSaValue()1475   inline int LsaSaValue() const {
1476     DCHECK_EQ(this->InstructionType(), InstructionBase::kRegisterType);
1477     return this->Bits(kSaShift + kLsaSaBits - 1, kSaShift);
1478   }
1479 
FunctionValue()1480   inline int FunctionValue() const {
1481     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1482            this->InstructionType() == InstructionBase::kImmediateType);
1483     return this->Bits(kFunctionShift + kFunctionBits - 1, kFunctionShift);
1484   }
1485 
FdValue()1486   inline int FdValue() const {
1487     return this->Bits(kFdShift + kFdBits - 1, kFdShift);
1488   }
1489 
FsValue()1490   inline int FsValue() const {
1491     return this->Bits(kFsShift + kFsBits - 1, kFsShift);
1492   }
1493 
FtValue()1494   inline int FtValue() const {
1495     return this->Bits(kFtShift + kFtBits - 1, kFtShift);
1496   }
1497 
FrValue()1498   inline int FrValue() const {
1499     return this->Bits(kFrShift + kFrBits - 1, kFrShift);
1500   }
1501 
WdValue()1502   inline int WdValue() const {
1503     return this->Bits(kWdShift + kWdBits - 1, kWdShift);
1504   }
1505 
WsValue()1506   inline int WsValue() const {
1507     return this->Bits(kWsShift + kWsBits - 1, kWsShift);
1508   }
1509 
WtValue()1510   inline int WtValue() const {
1511     return this->Bits(kWtShift + kWtBits - 1, kWtShift);
1512   }
1513 
Bp2Value()1514   inline int Bp2Value() const {
1515     DCHECK_EQ(this->InstructionType(), InstructionBase::kRegisterType);
1516     return this->Bits(kBp2Shift + kBp2Bits - 1, kBp2Shift);
1517   }
1518 
Bp3Value()1519   inline int Bp3Value() const {
1520     DCHECK_EQ(this->InstructionType(), InstructionBase::kRegisterType);
1521     return this->Bits(kBp3Shift + kBp3Bits - 1, kBp3Shift);
1522   }
1523 
1524   // Float Compare condition code instruction bits.
FCccValue()1525   inline int FCccValue() const {
1526     return this->Bits(kFCccShift + kFCccBits - 1, kFCccShift);
1527   }
1528 
1529   // Float Branch condition code instruction bits.
FBccValue()1530   inline int FBccValue() const {
1531     return this->Bits(kFBccShift + kFBccBits - 1, kFBccShift);
1532   }
1533 
1534   // Float Branch true/false instruction bit.
FBtrueValue()1535   inline int FBtrueValue() const {
1536     return this->Bits(kFBtrueShift + kFBtrueBits - 1, kFBtrueShift);
1537   }
1538 
1539   // Return the fields at their original place in the instruction encoding.
OpcodeFieldRaw()1540   inline Opcode OpcodeFieldRaw() const {
1541     return static_cast<Opcode>(this->InstructionBits() & kOpcodeMask);
1542   }
1543 
RsFieldRaw()1544   inline int RsFieldRaw() const {
1545     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1546            this->InstructionType() == InstructionBase::kImmediateType);
1547     return this->InstructionBits() & kRsFieldMask;
1548   }
1549 
1550   // Same as above function, but safe to call within InstructionType().
RsFieldRawNoAssert()1551   inline int RsFieldRawNoAssert() const {
1552     return this->InstructionBits() & kRsFieldMask;
1553   }
1554 
RtFieldRaw()1555   inline int RtFieldRaw() const {
1556     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1557            this->InstructionType() == InstructionBase::kImmediateType);
1558     return this->InstructionBits() & kRtFieldMask;
1559   }
1560 
RdFieldRaw()1561   inline int RdFieldRaw() const {
1562     DCHECK_EQ(this->InstructionType(), InstructionBase::kRegisterType);
1563     return this->InstructionBits() & kRdFieldMask;
1564   }
1565 
SaFieldRaw()1566   inline int SaFieldRaw() const {
1567     return this->InstructionBits() & kSaFieldMask;
1568   }
1569 
FunctionFieldRaw()1570   inline int FunctionFieldRaw() const {
1571     return this->InstructionBits() & kFunctionFieldMask;
1572   }
1573 
1574   // Get the secondary field according to the opcode.
SecondaryValue()1575   inline int SecondaryValue() const {
1576     Opcode op = this->OpcodeFieldRaw();
1577     switch (op) {
1578       case SPECIAL:
1579       case SPECIAL2:
1580         return FunctionValue();
1581       case COP1:
1582         return RsValue();
1583       case REGIMM:
1584         return RtValue();
1585       default:
1586         return nullptrSF;
1587     }
1588   }
1589 
ImmValue(int bits)1590   inline int32_t ImmValue(int bits) const {
1591     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1592     return this->Bits(bits - 1, 0);
1593   }
1594 
Imm9Value()1595   inline int32_t Imm9Value() const {
1596     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1597     return this->Bits(kImm9Shift + kImm9Bits - 1, kImm9Shift);
1598   }
1599 
Imm16Value()1600   inline int32_t Imm16Value() const {
1601     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1602     return this->Bits(kImm16Shift + kImm16Bits - 1, kImm16Shift);
1603   }
1604 
Imm18Value()1605   inline int32_t Imm18Value() const {
1606     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1607     return this->Bits(kImm18Shift + kImm18Bits - 1, kImm18Shift);
1608   }
1609 
Imm19Value()1610   inline int32_t Imm19Value() const {
1611     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1612     return this->Bits(kImm19Shift + kImm19Bits - 1, kImm19Shift);
1613   }
1614 
Imm21Value()1615   inline int32_t Imm21Value() const {
1616     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1617     return this->Bits(kImm21Shift + kImm21Bits - 1, kImm21Shift);
1618   }
1619 
Imm26Value()1620   inline int32_t Imm26Value() const {
1621     DCHECK((this->InstructionType() == InstructionBase::kJumpType) ||
1622            (this->InstructionType() == InstructionBase::kImmediateType));
1623     return this->Bits(kImm26Shift + kImm26Bits - 1, kImm26Shift);
1624   }
1625 
MsaImm8Value()1626   inline int32_t MsaImm8Value() const {
1627     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1628     return this->Bits(kMsaImm8Shift + kMsaImm8Bits - 1, kMsaImm8Shift);
1629   }
1630 
MsaImm5Value()1631   inline int32_t MsaImm5Value() const {
1632     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1633     return this->Bits(kMsaImm5Shift + kMsaImm5Bits - 1, kMsaImm5Shift);
1634   }
1635 
MsaImm10Value()1636   inline int32_t MsaImm10Value() const {
1637     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1638     return this->Bits(kMsaImm10Shift + kMsaImm10Bits - 1, kMsaImm10Shift);
1639   }
1640 
MsaImmMI10Value()1641   inline int32_t MsaImmMI10Value() const {
1642     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1643     return this->Bits(kMsaImmMI10Shift + kMsaImmMI10Bits - 1, kMsaImmMI10Shift);
1644   }
1645 
MsaBitDf()1646   inline int32_t MsaBitDf() const {
1647     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1648     int32_t df_m = this->Bits(22, 16);
1649     if (((df_m >> 6) & 1U) == 0) {
1650       return 3;
1651     } else if (((df_m >> 5) & 3U) == 2) {
1652       return 2;
1653     } else if (((df_m >> 4) & 7U) == 6) {
1654       return 1;
1655     } else if (((df_m >> 3) & 15U) == 14) {
1656       return 0;
1657     } else {
1658       return -1;
1659     }
1660   }
1661 
MsaBitMValue()1662   inline int32_t MsaBitMValue() const {
1663     DCHECK_EQ(this->InstructionType(), InstructionBase::kImmediateType);
1664     return this->Bits(16 + this->MsaBitDf() + 3, 16);
1665   }
1666 
MsaElmDf()1667   inline int32_t MsaElmDf() const {
1668     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1669            this->InstructionType() == InstructionBase::kImmediateType);
1670     int32_t df_n = this->Bits(21, 16);
1671     if (((df_n >> 4) & 3U) == 0) {
1672       return 0;
1673     } else if (((df_n >> 3) & 7U) == 4) {
1674       return 1;
1675     } else if (((df_n >> 2) & 15U) == 12) {
1676       return 2;
1677     } else if (((df_n >> 1) & 31U) == 28) {
1678       return 3;
1679     } else {
1680       return -1;
1681     }
1682   }
1683 
MsaElmNValue()1684   inline int32_t MsaElmNValue() const {
1685     DCHECK(this->InstructionType() == InstructionBase::kRegisterType ||
1686            this->InstructionType() == InstructionBase::kImmediateType);
1687     return this->Bits(16 + 4 - this->MsaElmDf(), 16);
1688   }
1689 
1690   static bool IsForbiddenAfterBranchInstr(Instr instr);
1691 
1692   // Say if the instruction should not be used in a branch delay slot or
1693   // immediately after a compact branch.
IsForbiddenAfterBranch()1694   inline bool IsForbiddenAfterBranch() const {
1695     return IsForbiddenAfterBranchInstr(this->InstructionBits());
1696   }
1697 
IsForbiddenInBranchDelay()1698   inline bool IsForbiddenInBranchDelay() const {
1699     return IsForbiddenAfterBranch();
1700   }
1701 
1702   // Say if the instruction 'links'. e.g. jal, bal.
1703   bool IsLinkingInstruction() const;
1704   // Say if the instruction is a break or a trap.
1705   bool IsTrap() const;
1706 
IsMSABranchInstr()1707   inline bool IsMSABranchInstr() const {
1708     if (this->OpcodeFieldRaw() == COP1) {
1709       switch (this->RsFieldRaw()) {
1710         case BZ_V:
1711         case BZ_B:
1712         case BZ_H:
1713         case BZ_W:
1714         case BZ_D:
1715         case BNZ_V:
1716         case BNZ_B:
1717         case BNZ_H:
1718         case BNZ_W:
1719         case BNZ_D:
1720           return true;
1721         default:
1722           return false;
1723       }
1724     }
1725     return false;
1726   }
1727 
IsMSAInstr()1728   inline bool IsMSAInstr() const {
1729     if (this->IsMSABranchInstr() || (this->OpcodeFieldRaw() == MSA))
1730       return true;
1731     return false;
1732   }
1733 };
1734 
1735 class Instruction : public InstructionGetters<InstructionBase> {
1736  public:
1737   // Instructions are read of out a code stream. The only way to get a
1738   // reference to an instruction is to convert a pointer. There is no way
1739   // to allocate or create instances of class Instruction.
1740   // Use the At(pc) function to create references to Instruction.
At(byte * pc)1741   static Instruction* At(byte* pc) {
1742     return reinterpret_cast<Instruction*>(pc);
1743   }
1744 
1745  private:
1746   // We need to prevent the creation of instances of class Instruction.
1747   DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction);
1748 };
1749 
1750 // -----------------------------------------------------------------------------
1751 // MIPS assembly various constants.
1752 
1753 // C/C++ argument slots size.
1754 const int kCArgSlotCount = 0;
1755 
1756 // TODO(plind): below should be based on kPointerSize
1757 // TODO(plind): find all usages and remove the needless instructions for n64.
1758 const int kCArgsSlotsSize = kCArgSlotCount * kInstrSize * 2;
1759 
1760 const int kInvalidStackOffset = -1;
1761 const int kBranchReturnOffset = 2 * kInstrSize;
1762 
1763 static const int kNegOffset = 0x00008000;
1764 
InstructionType()1765 InstructionBase::Type InstructionBase::InstructionType() const {
1766   switch (OpcodeFieldRaw()) {
1767     case SPECIAL:
1768       if (FunctionFieldToBitNumber(FunctionFieldRaw()) &
1769           kFunctionFieldRegisterTypeMask) {
1770         return kRegisterType;
1771       }
1772       return kUnsupported;
1773     case SPECIAL2:
1774       switch (FunctionFieldRaw()) {
1775         case MUL:
1776         case CLZ:
1777         case DCLZ:
1778           return kRegisterType;
1779         default:
1780           return kUnsupported;
1781       }
1782       break;
1783     case SPECIAL3:
1784       switch (FunctionFieldRaw()) {
1785         case INS:
1786         case DINS:
1787         case DINSM:
1788         case DINSU:
1789         case EXT:
1790         case DEXT:
1791         case DEXTM:
1792         case DEXTU:
1793           return kRegisterType;
1794         case BSHFL: {
1795           int sa = SaFieldRaw() >> kSaShift;
1796           switch (sa) {
1797             case BITSWAP:
1798             case WSBH:
1799             case SEB:
1800             case SEH:
1801               return kRegisterType;
1802           }
1803           sa >>= kBp2Bits;
1804           switch (sa) {
1805             case ALIGN:
1806               return kRegisterType;
1807             default:
1808               return kUnsupported;
1809           }
1810         }
1811         case LL_R6:
1812         case LLD_R6:
1813         case SC_R6:
1814         case SCD_R6: {
1815           DCHECK_EQ(kArchVariant, kMips64r6);
1816           return kImmediateType;
1817         }
1818         case DBSHFL: {
1819           int sa = SaFieldRaw() >> kSaShift;
1820           switch (sa) {
1821             case DBITSWAP:
1822             case DSBH:
1823             case DSHD:
1824               return kRegisterType;
1825           }
1826           sa = SaFieldRaw() >> kSaShift;
1827           sa >>= kBp3Bits;
1828           switch (sa) {
1829             case DALIGN:
1830               return kRegisterType;
1831             default:
1832               return kUnsupported;
1833           }
1834         }
1835         default:
1836           return kUnsupported;
1837       }
1838       break;
1839     case COP1:  // Coprocessor instructions.
1840       switch (RsFieldRawNoAssert()) {
1841         case BC1:  // Branch on coprocessor condition.
1842         case BC1EQZ:
1843         case BC1NEZ:
1844           return kImmediateType;
1845         // MSA Branch instructions
1846         case BZ_V:
1847         case BNZ_V:
1848         case BZ_B:
1849         case BZ_H:
1850         case BZ_W:
1851         case BZ_D:
1852         case BNZ_B:
1853         case BNZ_H:
1854         case BNZ_W:
1855         case BNZ_D:
1856           return kImmediateType;
1857         default:
1858           return kRegisterType;
1859       }
1860       break;
1861     case COP1X:
1862       return kRegisterType;
1863 
1864     // 26 bits immediate type instructions. e.g.: j imm26.
1865     case J:
1866     case JAL:
1867       return kJumpType;
1868 
1869     case MSA:
1870       switch (MSAMinorOpcodeField()) {
1871         case kMsaMinor3R:
1872         case kMsaMinor3RF:
1873         case kMsaMinorVEC:
1874         case kMsaMinor2R:
1875         case kMsaMinor2RF:
1876           return kRegisterType;
1877         case kMsaMinorELM:
1878           switch (InstructionBits() & kMsaLongerELMMask) {
1879             case CFCMSA:
1880             case CTCMSA:
1881             case MOVE_V:
1882               return kRegisterType;
1883             default:
1884               return kImmediateType;
1885           }
1886         default:
1887           return kImmediateType;
1888       }
1889 
1890     default:
1891       return kImmediateType;
1892   }
1893   return kUnsupported;
1894 }
1895 #undef OpcodeToBitNumber
1896 #undef FunctionFieldToBitNumber
1897 
1898 // -----------------------------------------------------------------------------
1899 // Instructions.
1900 
1901 template <class P>
IsLinkingInstruction()1902 bool InstructionGetters<P>::IsLinkingInstruction() const {
1903   switch (OpcodeFieldRaw()) {
1904     case JAL:
1905       return true;
1906     case POP76:
1907       if (RsFieldRawNoAssert() == JIALC)
1908         return true;  // JIALC
1909       else
1910         return false;  // BNEZC
1911     case REGIMM:
1912       switch (RtFieldRaw()) {
1913         case BGEZAL:
1914         case BLTZAL:
1915           return true;
1916         default:
1917           return false;
1918       }
1919     case SPECIAL:
1920       switch (FunctionFieldRaw()) {
1921         case JALR:
1922           return true;
1923         default:
1924           return false;
1925       }
1926     default:
1927       return false;
1928   }
1929 }
1930 
1931 template <class P>
IsTrap()1932 bool InstructionGetters<P>::IsTrap() const {
1933   if (OpcodeFieldRaw() != SPECIAL) {
1934     return false;
1935   } else {
1936     switch (FunctionFieldRaw()) {
1937       case BREAK:
1938       case TGE:
1939       case TGEU:
1940       case TLT:
1941       case TLTU:
1942       case TEQ:
1943       case TNE:
1944         return true;
1945       default:
1946         return false;
1947     }
1948   }
1949 }
1950 
1951 // static
1952 template <class T>
IsForbiddenAfterBranchInstr(Instr instr)1953 bool InstructionGetters<T>::IsForbiddenAfterBranchInstr(Instr instr) {
1954   Opcode opcode = static_cast<Opcode>(instr & kOpcodeMask);
1955   switch (opcode) {
1956     case J:
1957     case JAL:
1958     case BEQ:
1959     case BNE:
1960     case BLEZ:  // POP06 bgeuc/bleuc, blezalc, bgezalc
1961     case BGTZ:  // POP07 bltuc/bgtuc, bgtzalc, bltzalc
1962     case BEQL:
1963     case BNEL:
1964     case BLEZL:  // POP26 bgezc, blezc, bgec/blec
1965     case BGTZL:  // POP27 bgtzc, bltzc, bltc/bgtc
1966     case BC:
1967     case BALC:
1968     case POP10:  // beqzalc, bovc, beqc
1969     case POP30:  // bnezalc, bnvc, bnec
1970     case POP66:  // beqzc, jic
1971     case POP76:  // bnezc, jialc
1972       return true;
1973     case REGIMM:
1974       switch (instr & kRtFieldMask) {
1975         case BLTZ:
1976         case BGEZ:
1977         case BLTZAL:
1978         case BGEZAL:
1979           return true;
1980         default:
1981           return false;
1982       }
1983       break;
1984     case SPECIAL:
1985       switch (instr & kFunctionFieldMask) {
1986         case JR:
1987         case JALR:
1988           return true;
1989         default:
1990           return false;
1991       }
1992       break;
1993     case COP1:
1994       switch (instr & kRsFieldMask) {
1995         case BC1:
1996         case BC1EQZ:
1997         case BC1NEZ:
1998         case BZ_V:
1999         case BZ_B:
2000         case BZ_H:
2001         case BZ_W:
2002         case BZ_D:
2003         case BNZ_V:
2004         case BNZ_B:
2005         case BNZ_H:
2006         case BNZ_W:
2007         case BNZ_D:
2008           return true;
2009           break;
2010         default:
2011           return false;
2012       }
2013       break;
2014     default:
2015       return false;
2016   }
2017 }
2018 }  // namespace internal
2019 }  // namespace v8
2020 
2021 #endif  // V8_CODEGEN_MIPS64_CONSTANTS_MIPS64_H_
2022