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