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