Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_MIPS_CONSTANTS_H_ |
| 6 | #define V8_MIPS_CONSTANTS_H_ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/globals.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 8 | // UNIMPLEMENTED_ macro for MIPS. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9 | #ifdef DEBUG |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 10 | #define UNIMPLEMENTED_MIPS() \ |
| 11 | v8::internal::PrintF("%s, \tline %d: \tfunction %s not implemented. \n", \ |
| 12 | __FILE__, __LINE__, __func__) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 13 | #else |
| 14 | #define UNIMPLEMENTED_MIPS() |
| 15 | #endif |
| 16 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 17 | #define UNSUPPORTED_MIPS() v8::internal::PrintF("Unsupported instruction.\n") |
| 18 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 19 | enum ArchVariants { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | kMips32r1 = v8::internal::MIPSr1, |
| 21 | kMips32r2 = v8::internal::MIPSr2, |
| 22 | kMips32r6 = v8::internal::MIPSr6, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 23 | kLoongson |
| 24 | }; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 25 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 26 | #ifdef _MIPS_ARCH_MIPS32R2 |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 27 | static const ArchVariants kArchVariant = kMips32r2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | #elif _MIPS_ARCH_MIPS32R6 |
| 29 | static const ArchVariants kArchVariant = kMips32r6; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 30 | #elif _MIPS_ARCH_LOONGSON |
| 31 | // The loongson flag refers to the LOONGSON architectures based on MIPS-III, |
| 32 | // which predates (and is a subset of) the mips32r2 and r1 architectures. |
| 33 | static const ArchVariants kArchVariant = kLoongson; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | #elif _MIPS_ARCH_MIPS32RX |
| 35 | // This flags referred to compatibility mode that creates universal code that |
| 36 | // can run on any MIPS32 architecture revision. The dynamically generated code |
| 37 | // by v8 is specialized for the MIPS host detected in runtime probing. |
| 38 | static const ArchVariants kArchVariant = kMips32r1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 39 | #else |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 40 | static const ArchVariants kArchVariant = kMips32r1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | enum Endianness { |
| 44 | kLittle, |
| 45 | kBig |
| 46 | }; |
| 47 | |
| 48 | #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 49 | static const Endianness kArchEndian = kLittle; |
| 50 | #elif defined(V8_TARGET_BIG_ENDIAN) |
| 51 | static const Endianness kArchEndian = kBig; |
| 52 | #else |
| 53 | #error Unknown endianness |
| 54 | #endif |
| 55 | |
| 56 | enum FpuMode { |
| 57 | kFP32, |
| 58 | kFP64, |
| 59 | kFPXX |
| 60 | }; |
| 61 | |
| 62 | #if defined(FPU_MODE_FP32) |
| 63 | static const FpuMode kFpuMode = kFP32; |
| 64 | #elif defined(FPU_MODE_FP64) |
| 65 | static const FpuMode kFpuMode = kFP64; |
| 66 | #elif defined(FPU_MODE_FPXX) |
| 67 | static const FpuMode kFpuMode = kFPXX; |
| 68 | #else |
| 69 | static const FpuMode kFpuMode = kFP32; |
| 70 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 72 | #if(defined(__mips_hard_float) && __mips_hard_float != 0) |
| 73 | // Use floating-point coprocessor instructions. This flag is raised when |
| 74 | // -mhard-float is passed to the compiler. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 75 | const bool IsMipsSoftFloatABI = false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 76 | #elif(defined(__mips_soft_float) && __mips_soft_float != 0) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | // This flag is raised when -msoft-float is passed to the compiler. |
| 78 | // Although FPU is a base requirement for v8, soft-float ABI is used |
| 79 | // on soft-float systems with FPU kernel emulation. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 80 | const bool IsMipsSoftFloatABI = true; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 81 | #else |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 82 | const bool IsMipsSoftFloatABI = true; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 86 | const uint32_t kHoleNanUpper32Offset = 4; |
| 87 | const uint32_t kHoleNanLower32Offset = 0; |
| 88 | #elif defined(V8_TARGET_BIG_ENDIAN) |
| 89 | const uint32_t kHoleNanUpper32Offset = 0; |
| 90 | const uint32_t kHoleNanLower32Offset = 4; |
| 91 | #else |
| 92 | #error Unknown endianness |
| 93 | #endif |
| 94 | |
| 95 | #ifndef FPU_MODE_FPXX |
| 96 | #define IsFp64Mode() \ |
| 97 | (kFpuMode == kFP64) |
| 98 | #else |
| 99 | #define IsFp64Mode() \ |
| 100 | (CpuFeatures::IsSupported(FP64FPU)) |
| 101 | #endif |
| 102 | |
| 103 | #ifndef _MIPS_ARCH_MIPS32RX |
| 104 | #define IsMipsArchVariant(check) \ |
| 105 | (kArchVariant == check) |
| 106 | #else |
| 107 | #define IsMipsArchVariant(check) \ |
Paul Lind | 18a7ebb | 2014-12-17 22:29:32 -0800 | [diff] [blame] | 108 | (CpuFeatures::IsSupported(static_cast<CpuFeature>(check))) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
| 111 | |
| 112 | #define __STDC_FORMAT_MACROS |
| 113 | #include <inttypes.h> |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 114 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 115 | // Defines constants and accessor classes to assemble, disassemble and |
| 116 | // simulate MIPS32 instructions. |
| 117 | // |
| 118 | // See: MIPS32 Architecture For Programmers |
| 119 | // Volume II: The MIPS32 Instruction Set |
| 120 | // Try www.cs.cornell.edu/courses/cs3410/2008fa/MIPS_Vol2.pdf. |
| 121 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | namespace v8 { |
| 123 | namespace internal { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 124 | |
| 125 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 126 | // Registers and FPURegisters. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 127 | |
| 128 | // Number of general purpose registers. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | const int kNumRegisters = 32; |
| 130 | const int kInvalidRegister = -1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 131 | |
| 132 | // Number of registers with HI, LO, and pc. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 133 | const int kNumSimuRegisters = 35; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 134 | |
| 135 | // In the simulator, the PC register is simulated as the 34th register. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 136 | const int kPCRegister = 34; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 137 | |
| 138 | // Number coprocessor registers. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 139 | const int kNumFPURegisters = 32; |
| 140 | const int kInvalidFPURegister = -1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 141 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 142 | // FPU (coprocessor 1) control registers. Currently only FCSR is implemented. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 143 | const int kFCSRRegister = 31; |
| 144 | const int kInvalidFPUControlRegister = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | const uint32_t kFPUInvalidResult = static_cast<uint32_t>(1 << 31) - 1; |
| 146 | const uint64_t kFPU64InvalidResult = |
| 147 | static_cast<uint64_t>(static_cast<uint64_t>(1) << 63) - 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 148 | |
| 149 | // FCSR constants. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 150 | const uint32_t kFCSRInexactFlagBit = 2; |
| 151 | const uint32_t kFCSRUnderflowFlagBit = 3; |
| 152 | const uint32_t kFCSROverflowFlagBit = 4; |
| 153 | const uint32_t kFCSRDivideByZeroFlagBit = 5; |
| 154 | const uint32_t kFCSRInvalidOpFlagBit = 6; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 155 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 156 | const uint32_t kFCSRInexactFlagMask = 1 << kFCSRInexactFlagBit; |
| 157 | const uint32_t kFCSRUnderflowFlagMask = 1 << kFCSRUnderflowFlagBit; |
| 158 | const uint32_t kFCSROverflowFlagMask = 1 << kFCSROverflowFlagBit; |
| 159 | const uint32_t kFCSRDivideByZeroFlagMask = 1 << kFCSRDivideByZeroFlagBit; |
| 160 | const uint32_t kFCSRInvalidOpFlagMask = 1 << kFCSRInvalidOpFlagBit; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 161 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 162 | const uint32_t kFCSRFlagMask = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 163 | kFCSRInexactFlagMask | |
| 164 | kFCSRUnderflowFlagMask | |
| 165 | kFCSROverflowFlagMask | |
| 166 | kFCSRDivideByZeroFlagMask | |
| 167 | kFCSRInvalidOpFlagMask; |
| 168 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 169 | const uint32_t kFCSRExceptionFlagMask = kFCSRFlagMask ^ kFCSRInexactFlagMask; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | // 'pref' instruction hints |
| 172 | const int32_t kPrefHintLoad = 0; |
| 173 | const int32_t kPrefHintStore = 1; |
| 174 | const int32_t kPrefHintLoadStreamed = 4; |
| 175 | const int32_t kPrefHintStoreStreamed = 5; |
| 176 | const int32_t kPrefHintLoadRetained = 6; |
| 177 | const int32_t kPrefHintStoreRetained = 7; |
| 178 | const int32_t kPrefHintWritebackInvalidate = 25; |
| 179 | const int32_t kPrefHintPrepareForStore = 30; |
| 180 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 181 | // Helper functions for converting between register numbers and names. |
| 182 | class Registers { |
| 183 | public: |
| 184 | // Return the name of the register. |
| 185 | static const char* Name(int reg); |
| 186 | |
| 187 | // Lookup the register number for the name provided. |
| 188 | static int Number(const char* name); |
| 189 | |
| 190 | struct RegisterAlias { |
| 191 | int reg; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 192 | const char* name; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static const int32_t kMaxValue = 0x7fffffff; |
| 196 | static const int32_t kMinValue = 0x80000000; |
| 197 | |
| 198 | private: |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 199 | static const char* names_[kNumSimuRegisters]; |
| 200 | static const RegisterAlias aliases_[]; |
| 201 | }; |
| 202 | |
| 203 | // Helper functions for converting between register numbers and names. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | class FPURegisters { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 205 | public: |
| 206 | // Return the name of the register. |
| 207 | static const char* Name(int reg); |
| 208 | |
| 209 | // Lookup the register number for the name provided. |
| 210 | static int Number(const char* name); |
| 211 | |
| 212 | struct RegisterAlias { |
| 213 | int creg; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 214 | const char* name; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 218 | static const char* names_[kNumFPURegisters]; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 219 | static const RegisterAlias aliases_[]; |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | // ----------------------------------------------------------------------------- |
| 224 | // Instructions encoding constants. |
| 225 | |
| 226 | // On MIPS all instructions are 32 bits. |
| 227 | typedef int32_t Instr; |
| 228 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 229 | // Special Software Interrupt codes when used in the presence of the MIPS |
| 230 | // simulator. |
| 231 | enum SoftwareInterruptCodes { |
| 232 | // Transition to C code. |
| 233 | call_rt_redirected = 0xfffff |
| 234 | }; |
| 235 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 236 | // On MIPS Simulator breakpoints can have different codes: |
| 237 | // - Breaks between 0 and kMaxWatchpointCode are treated as simple watchpoints, |
| 238 | // the simulator will run through them and print the registers. |
| 239 | // - Breaks between kMaxWatchpointCode and kMaxStopCode are treated as stop() |
| 240 | // instructions (see Assembler::stop()). |
| 241 | // - Breaks larger than kMaxStopCode are simple breaks, dropping you into the |
| 242 | // debugger. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 243 | const uint32_t kMaxWatchpointCode = 31; |
| 244 | const uint32_t kMaxStopCode = 127; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 245 | STATIC_ASSERT(kMaxWatchpointCode < kMaxStopCode); |
| 246 | |
| 247 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 248 | // ----- Fields offset and length. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 249 | const int kOpcodeShift = 26; |
| 250 | const int kOpcodeBits = 6; |
| 251 | const int kRsShift = 21; |
| 252 | const int kRsBits = 5; |
| 253 | const int kRtShift = 16; |
| 254 | const int kRtBits = 5; |
| 255 | const int kRdShift = 11; |
| 256 | const int kRdBits = 5; |
| 257 | const int kSaShift = 6; |
| 258 | const int kSaBits = 5; |
| 259 | const int kFunctionShift = 0; |
| 260 | const int kFunctionBits = 6; |
| 261 | const int kLuiShift = 16; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 262 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 263 | const int kImm16Shift = 0; |
| 264 | const int kImm16Bits = 16; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 265 | const int kImm21Shift = 0; |
| 266 | const int kImm21Bits = 21; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 267 | const int kImm26Shift = 0; |
| 268 | const int kImm26Bits = 26; |
| 269 | const int kImm28Shift = 0; |
| 270 | const int kImm28Bits = 28; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 271 | const int kImm32Shift = 0; |
| 272 | const int kImm32Bits = 32; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 273 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 274 | // In branches and jumps immediate fields point to words, not bytes, |
| 275 | // and are therefore shifted by 2. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 276 | const int kImmFieldShift = 2; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 277 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 278 | const int kFrBits = 5; |
| 279 | const int kFrShift = 21; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 280 | const int kFsShift = 11; |
| 281 | const int kFsBits = 5; |
| 282 | const int kFtShift = 16; |
| 283 | const int kFtBits = 5; |
| 284 | const int kFdShift = 6; |
| 285 | const int kFdBits = 5; |
| 286 | const int kFCccShift = 8; |
| 287 | const int kFCccBits = 3; |
| 288 | const int kFBccShift = 18; |
| 289 | const int kFBccBits = 3; |
| 290 | const int kFBtrueShift = 16; |
| 291 | const int kFBtrueBits = 1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 292 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 293 | // ----- Miscellaneous useful masks. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 294 | // Instruction bit masks. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 295 | const int kOpcodeMask = ((1 << kOpcodeBits) - 1) << kOpcodeShift; |
| 296 | const int kImm16Mask = ((1 << kImm16Bits) - 1) << kImm16Shift; |
| 297 | const int kImm26Mask = ((1 << kImm26Bits) - 1) << kImm26Shift; |
| 298 | const int kImm28Mask = ((1 << kImm28Bits) - 1) << kImm28Shift; |
| 299 | const int kRsFieldMask = ((1 << kRsBits) - 1) << kRsShift; |
| 300 | const int kRtFieldMask = ((1 << kRtBits) - 1) << kRtShift; |
| 301 | const int kRdFieldMask = ((1 << kRdBits) - 1) << kRdShift; |
| 302 | const int kSaFieldMask = ((1 << kSaBits) - 1) << kSaShift; |
| 303 | const int kFunctionFieldMask = ((1 << kFunctionBits) - 1) << kFunctionShift; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 304 | // Misc masks. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 305 | const int kHiMask = 0xffff << 16; |
| 306 | const int kLoMask = 0xffff; |
| 307 | const int kSignMask = 0x80000000; |
| 308 | const int kJumpAddrMask = (1 << (kImm26Bits + kImmFieldShift)) - 1; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 309 | |
| 310 | // ----- MIPS Opcodes and Function Fields. |
| 311 | // We use this presentation to stay close to the table representation in |
| 312 | // MIPS32 Architecture For Programmers, Volume II: The MIPS32 Instruction Set. |
| 313 | enum Opcode { |
| 314 | SPECIAL = 0 << kOpcodeShift, |
| 315 | REGIMM = 1 << kOpcodeShift, |
| 316 | |
| 317 | J = ((0 << 3) + 2) << kOpcodeShift, |
| 318 | JAL = ((0 << 3) + 3) << kOpcodeShift, |
| 319 | BEQ = ((0 << 3) + 4) << kOpcodeShift, |
| 320 | BNE = ((0 << 3) + 5) << kOpcodeShift, |
| 321 | BLEZ = ((0 << 3) + 6) << kOpcodeShift, |
| 322 | BGTZ = ((0 << 3) + 7) << kOpcodeShift, |
| 323 | |
| 324 | ADDI = ((1 << 3) + 0) << kOpcodeShift, |
| 325 | ADDIU = ((1 << 3) + 1) << kOpcodeShift, |
| 326 | SLTI = ((1 << 3) + 2) << kOpcodeShift, |
| 327 | SLTIU = ((1 << 3) + 3) << kOpcodeShift, |
| 328 | ANDI = ((1 << 3) + 4) << kOpcodeShift, |
| 329 | ORI = ((1 << 3) + 5) << kOpcodeShift, |
| 330 | XORI = ((1 << 3) + 6) << kOpcodeShift, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 331 | LUI = ((1 << 3) + 7) << kOpcodeShift, // LUI/AUI family. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 332 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | BEQC = ((2 << 3) + 0) << kOpcodeShift, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 334 | COP1 = ((2 << 3) + 1) << kOpcodeShift, // Coprocessor 1 class. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 335 | BEQL = ((2 << 3) + 4) << kOpcodeShift, |
| 336 | BNEL = ((2 << 3) + 5) << kOpcodeShift, |
| 337 | BLEZL = ((2 << 3) + 6) << kOpcodeShift, |
| 338 | BGTZL = ((2 << 3) + 7) << kOpcodeShift, |
| 339 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 340 | DADDI = ((3 << 3) + 0) << kOpcodeShift, // This is also BNEC. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 341 | SPECIAL2 = ((3 << 3) + 4) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 342 | SPECIAL3 = ((3 << 3) + 7) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 343 | |
| 344 | LB = ((4 << 3) + 0) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 345 | LH = ((4 << 3) + 1) << kOpcodeShift, |
| 346 | LWL = ((4 << 3) + 2) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 347 | LW = ((4 << 3) + 3) << kOpcodeShift, |
| 348 | LBU = ((4 << 3) + 4) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 349 | LHU = ((4 << 3) + 5) << kOpcodeShift, |
| 350 | LWR = ((4 << 3) + 6) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 351 | SB = ((5 << 3) + 0) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 352 | SH = ((5 << 3) + 1) << kOpcodeShift, |
| 353 | SWL = ((5 << 3) + 2) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 354 | SW = ((5 << 3) + 3) << kOpcodeShift, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 355 | SWR = ((5 << 3) + 6) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 356 | |
| 357 | LWC1 = ((6 << 3) + 1) << kOpcodeShift, |
| 358 | LDC1 = ((6 << 3) + 5) << kOpcodeShift, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 359 | BEQZC = ((6 << 3) + 6) << kOpcodeShift, |
| 360 | |
| 361 | PREF = ((6 << 3) + 3) << kOpcodeShift, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 362 | |
| 363 | SWC1 = ((7 << 3) + 1) << kOpcodeShift, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 364 | SDC1 = ((7 << 3) + 5) << kOpcodeShift, |
| 365 | BNEZC = ((7 << 3) + 6) << kOpcodeShift, |
| 366 | |
| 367 | COP1X = ((1 << 4) + 3) << kOpcodeShift |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | enum SecondaryField { |
| 371 | // SPECIAL Encoding of Function Field. |
| 372 | SLL = ((0 << 3) + 0), |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 373 | MOVCI = ((0 << 3) + 1), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 374 | SRL = ((0 << 3) + 2), |
| 375 | SRA = ((0 << 3) + 3), |
| 376 | SLLV = ((0 << 3) + 4), |
| 377 | SRLV = ((0 << 3) + 6), |
| 378 | SRAV = ((0 << 3) + 7), |
| 379 | |
| 380 | JR = ((1 << 3) + 0), |
| 381 | JALR = ((1 << 3) + 1), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 382 | MOVZ = ((1 << 3) + 2), |
| 383 | MOVN = ((1 << 3) + 3), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 384 | BREAK = ((1 << 3) + 5), |
| 385 | |
| 386 | MFHI = ((2 << 3) + 0), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | CLZ_R6 = ((2 << 3) + 0), |
| 388 | CLO_R6 = ((2 << 3) + 1), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 389 | MFLO = ((2 << 3) + 2), |
| 390 | |
| 391 | MULT = ((3 << 3) + 0), |
| 392 | MULTU = ((3 << 3) + 1), |
| 393 | DIV = ((3 << 3) + 2), |
| 394 | DIVU = ((3 << 3) + 3), |
| 395 | |
| 396 | ADD = ((4 << 3) + 0), |
| 397 | ADDU = ((4 << 3) + 1), |
| 398 | SUB = ((4 << 3) + 2), |
| 399 | SUBU = ((4 << 3) + 3), |
| 400 | AND = ((4 << 3) + 4), |
| 401 | OR = ((4 << 3) + 5), |
| 402 | XOR = ((4 << 3) + 6), |
| 403 | NOR = ((4 << 3) + 7), |
| 404 | |
| 405 | SLT = ((5 << 3) + 2), |
| 406 | SLTU = ((5 << 3) + 3), |
| 407 | |
| 408 | TGE = ((6 << 3) + 0), |
| 409 | TGEU = ((6 << 3) + 1), |
| 410 | TLT = ((6 << 3) + 2), |
| 411 | TLTU = ((6 << 3) + 3), |
| 412 | TEQ = ((6 << 3) + 4), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 413 | SELEQZ_S = ((6 << 3) + 5), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 414 | TNE = ((6 << 3) + 6), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 415 | SELNEZ_S = ((6 << 3) + 7), |
| 416 | |
| 417 | // Multiply integers in r6. |
| 418 | MUL_MUH = ((3 << 3) + 0), // MUL, MUH. |
| 419 | MUL_MUH_U = ((3 << 3) + 1), // MUL_U, MUH_U. |
| 420 | |
| 421 | MUL_OP = ((0 << 3) + 2), |
| 422 | MUH_OP = ((0 << 3) + 3), |
| 423 | DIV_OP = ((0 << 3) + 2), |
| 424 | MOD_OP = ((0 << 3) + 3), |
| 425 | |
| 426 | DIV_MOD = ((3 << 3) + 2), |
| 427 | DIV_MOD_U = ((3 << 3) + 3), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 428 | |
| 429 | // SPECIAL2 Encoding of Function Field. |
| 430 | MUL = ((0 << 3) + 2), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 431 | CLZ = ((4 << 3) + 0), |
| 432 | CLO = ((4 << 3) + 1), |
| 433 | |
| 434 | // SPECIAL3 Encoding of Function Field. |
| 435 | EXT = ((0 << 3) + 0), |
| 436 | INS = ((0 << 3) + 4), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 437 | |
| 438 | // REGIMM encoding of rt Field. |
| 439 | BLTZ = ((0 << 3) + 0) << 16, |
| 440 | BGEZ = ((0 << 3) + 1) << 16, |
| 441 | BLTZAL = ((2 << 3) + 0) << 16, |
| 442 | BGEZAL = ((2 << 3) + 1) << 16, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 443 | BGEZALL = ((2 << 3) + 3) << 16, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 444 | |
| 445 | // COP1 Encoding of rs Field. |
| 446 | MFC1 = ((0 << 3) + 0) << 21, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 447 | CFC1 = ((0 << 3) + 2) << 21, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 448 | MFHC1 = ((0 << 3) + 3) << 21, |
| 449 | MTC1 = ((0 << 3) + 4) << 21, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 450 | CTC1 = ((0 << 3) + 6) << 21, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 451 | MTHC1 = ((0 << 3) + 7) << 21, |
| 452 | BC1 = ((1 << 3) + 0) << 21, |
| 453 | S = ((2 << 3) + 0) << 21, |
| 454 | D = ((2 << 3) + 1) << 21, |
| 455 | W = ((2 << 3) + 4) << 21, |
| 456 | L = ((2 << 3) + 5) << 21, |
| 457 | PS = ((2 << 3) + 6) << 21, |
| 458 | // COP1 Encoding of Function Field When rs=S. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 459 | ROUND_L_S = ((1 << 3) + 0), |
| 460 | TRUNC_L_S = ((1 << 3) + 1), |
| 461 | CEIL_L_S = ((1 << 3) + 2), |
| 462 | FLOOR_L_S = ((1 << 3) + 3), |
| 463 | ROUND_W_S = ((1 << 3) + 4), |
| 464 | TRUNC_W_S = ((1 << 3) + 5), |
| 465 | CEIL_W_S = ((1 << 3) + 6), |
| 466 | FLOOR_W_S = ((1 << 3) + 7), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 467 | CVT_D_S = ((4 << 3) + 1), |
| 468 | CVT_W_S = ((4 << 3) + 4), |
| 469 | CVT_L_S = ((4 << 3) + 5), |
| 470 | CVT_PS_S = ((4 << 3) + 6), |
| 471 | // COP1 Encoding of Function Field When rs=D. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 472 | ADD_D = ((0 << 3) + 0), |
| 473 | SUB_D = ((0 << 3) + 1), |
| 474 | MUL_D = ((0 << 3) + 2), |
| 475 | DIV_D = ((0 << 3) + 3), |
| 476 | SQRT_D = ((0 << 3) + 4), |
| 477 | ABS_D = ((0 << 3) + 5), |
| 478 | MOV_D = ((0 << 3) + 6), |
| 479 | NEG_D = ((0 << 3) + 7), |
| 480 | ROUND_L_D = ((1 << 3) + 0), |
| 481 | TRUNC_L_D = ((1 << 3) + 1), |
| 482 | CEIL_L_D = ((1 << 3) + 2), |
| 483 | FLOOR_L_D = ((1 << 3) + 3), |
| 484 | ROUND_W_D = ((1 << 3) + 4), |
| 485 | TRUNC_W_D = ((1 << 3) + 5), |
| 486 | CEIL_W_D = ((1 << 3) + 6), |
| 487 | FLOOR_W_D = ((1 << 3) + 7), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 488 | MIN = ((3 << 3) + 4), |
| 489 | MINA = ((3 << 3) + 5), |
| 490 | MAX = ((3 << 3) + 6), |
| 491 | MAXA = ((3 << 3) + 7), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 492 | CVT_S_D = ((4 << 3) + 0), |
| 493 | CVT_W_D = ((4 << 3) + 4), |
| 494 | CVT_L_D = ((4 << 3) + 5), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | C_F_D = ((6 << 3) + 0), |
| 496 | C_UN_D = ((6 << 3) + 1), |
| 497 | C_EQ_D = ((6 << 3) + 2), |
| 498 | C_UEQ_D = ((6 << 3) + 3), |
| 499 | C_OLT_D = ((6 << 3) + 4), |
| 500 | C_ULT_D = ((6 << 3) + 5), |
| 501 | C_OLE_D = ((6 << 3) + 6), |
| 502 | C_ULE_D = ((6 << 3) + 7), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 503 | // COP1 Encoding of Function Field When rs=W or L. |
| 504 | CVT_S_W = ((4 << 3) + 0), |
| 505 | CVT_D_W = ((4 << 3) + 1), |
| 506 | CVT_S_L = ((4 << 3) + 0), |
| 507 | CVT_D_L = ((4 << 3) + 1), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 508 | BC1EQZ = ((2 << 2) + 1) << 21, |
| 509 | BC1NEZ = ((3 << 2) + 1) << 21, |
| 510 | // COP1 CMP positive predicates Bit 5..4 = 00. |
| 511 | CMP_AF = ((0 << 3) + 0), |
| 512 | CMP_UN = ((0 << 3) + 1), |
| 513 | CMP_EQ = ((0 << 3) + 2), |
| 514 | CMP_UEQ = ((0 << 3) + 3), |
| 515 | CMP_LT = ((0 << 3) + 4), |
| 516 | CMP_ULT = ((0 << 3) + 5), |
| 517 | CMP_LE = ((0 << 3) + 6), |
| 518 | CMP_ULE = ((0 << 3) + 7), |
| 519 | CMP_SAF = ((1 << 3) + 0), |
| 520 | CMP_SUN = ((1 << 3) + 1), |
| 521 | CMP_SEQ = ((1 << 3) + 2), |
| 522 | CMP_SUEQ = ((1 << 3) + 3), |
| 523 | CMP_SSLT = ((1 << 3) + 4), |
| 524 | CMP_SSULT = ((1 << 3) + 5), |
| 525 | CMP_SLE = ((1 << 3) + 6), |
| 526 | CMP_SULE = ((1 << 3) + 7), |
| 527 | // COP1 CMP negative predicates Bit 5..4 = 01. |
| 528 | CMP_AT = ((2 << 3) + 0), // Reserved, not implemented. |
| 529 | CMP_OR = ((2 << 3) + 1), |
| 530 | CMP_UNE = ((2 << 3) + 2), |
| 531 | CMP_NE = ((2 << 3) + 3), |
| 532 | CMP_UGE = ((2 << 3) + 4), // Reserved, not implemented. |
| 533 | CMP_OGE = ((2 << 3) + 5), // Reserved, not implemented. |
| 534 | CMP_UGT = ((2 << 3) + 6), // Reserved, not implemented. |
| 535 | CMP_OGT = ((2 << 3) + 7), // Reserved, not implemented. |
| 536 | CMP_SAT = ((3 << 3) + 0), // Reserved, not implemented. |
| 537 | CMP_SOR = ((3 << 3) + 1), |
| 538 | CMP_SUNE = ((3 << 3) + 2), |
| 539 | CMP_SNE = ((3 << 3) + 3), |
| 540 | CMP_SUGE = ((3 << 3) + 4), // Reserved, not implemented. |
| 541 | CMP_SOGE = ((3 << 3) + 5), // Reserved, not implemented. |
| 542 | CMP_SUGT = ((3 << 3) + 6), // Reserved, not implemented. |
| 543 | CMP_SOGT = ((3 << 3) + 7), // Reserved, not implemented. |
| 544 | |
| 545 | SEL = ((2 << 3) + 0), |
| 546 | SELEQZ_C = ((2 << 3) + 4), // COP1 on FPR registers. |
| 547 | SELNEZ_C = ((2 << 3) + 7), // COP1 on FPR registers. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 548 | // COP1 Encoding of Function Field When rs=PS. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 549 | // COP1X Encoding of Function Field. |
| 550 | MADD_D = ((4 << 3) + 1), |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 551 | |
| 552 | NULLSF = 0 |
| 553 | }; |
| 554 | |
| 555 | |
| 556 | // ----- Emulated conditions. |
| 557 | // On MIPS we use this enum to abstract from conditionnal branch instructions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 558 | // The 'U' prefix is used to specify unsigned comparisons. |
| 559 | // Oppposite conditions must be paired as odd/even numbers |
| 560 | // because 'NegateCondition' function flips LSB to negate condition. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 561 | enum Condition { |
| 562 | // Any value < 0 is considered no_condition. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 563 | kNoCondition = -1, |
| 564 | overflow = 0, |
| 565 | no_overflow = 1, |
| 566 | Uless = 2, |
| 567 | Ugreater_equal = 3, |
| 568 | equal = 4, |
| 569 | not_equal = 5, |
| 570 | Uless_equal = 6, |
| 571 | Ugreater = 7, |
| 572 | negative = 8, |
| 573 | positive = 9, |
| 574 | parity_even = 10, |
| 575 | parity_odd = 11, |
| 576 | less = 12, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 577 | greater_equal = 13, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 578 | less_equal = 14, |
| 579 | greater = 15, |
| 580 | ueq = 16, // Unordered or Equal. |
| 581 | nue = 17, // Not (Unordered or Equal). |
| 582 | cc_always = 18, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 583 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 584 | // Aliases. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 585 | carry = Uless, |
| 586 | not_carry = Ugreater_equal, |
| 587 | zero = equal, |
| 588 | eq = equal, |
| 589 | not_zero = not_equal, |
| 590 | ne = not_equal, |
| 591 | nz = not_equal, |
| 592 | sign = negative, |
| 593 | not_sign = positive, |
| 594 | mi = negative, |
| 595 | pl = positive, |
| 596 | hi = Ugreater, |
| 597 | ls = Uless_equal, |
| 598 | ge = greater_equal, |
| 599 | lt = less, |
| 600 | gt = greater, |
| 601 | le = less_equal, |
| 602 | hs = Ugreater_equal, |
| 603 | lo = Uless, |
| 604 | al = cc_always, |
| 605 | cc_default = kNoCondition |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 608 | |
| 609 | // Returns the equivalent of !cc. |
| 610 | // Negation of the default kNoCondition (-1) results in a non-default |
| 611 | // no_condition value (-2). As long as tests for no_condition check |
| 612 | // for condition < 0, this will work as expected. |
| 613 | inline Condition NegateCondition(Condition cc) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 614 | DCHECK(cc != cc_always); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 615 | return static_cast<Condition>(cc ^ 1); |
| 616 | } |
| 617 | |
| 618 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 619 | // Commute a condition such that {a cond b == b cond' a}. |
| 620 | inline Condition CommuteCondition(Condition cc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 621 | switch (cc) { |
| 622 | case Uless: |
| 623 | return Ugreater; |
| 624 | case Ugreater: |
| 625 | return Uless; |
| 626 | case Ugreater_equal: |
| 627 | return Uless_equal; |
| 628 | case Uless_equal: |
| 629 | return Ugreater_equal; |
| 630 | case less: |
| 631 | return greater; |
| 632 | case greater: |
| 633 | return less; |
| 634 | case greater_equal: |
| 635 | return less_equal; |
| 636 | case less_equal: |
| 637 | return greater_equal; |
| 638 | default: |
| 639 | return cc; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 640 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 644 | // ----- Coprocessor conditions. |
| 645 | enum FPUCondition { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 646 | kNoFPUCondition = -1, |
| 647 | |
| 648 | F = 0, // False. |
| 649 | UN = 1, // Unordered. |
| 650 | EQ = 2, // Equal. |
| 651 | UEQ = 3, // Unordered or Equal. |
| 652 | OLT = 4, // Ordered or Less Than. |
| 653 | ULT = 5, // Unordered or Less Than. |
| 654 | OLE = 6, // Ordered or Less Than or Equal. |
| 655 | ULE = 7 // Unordered or Less Than or Equal. |
| 656 | }; |
| 657 | |
| 658 | |
| 659 | // FPU rounding modes. |
| 660 | enum FPURoundingMode { |
| 661 | RN = 0 << 0, // Round to Nearest. |
| 662 | RZ = 1 << 0, // Round towards zero. |
| 663 | RP = 2 << 0, // Round towards Plus Infinity. |
| 664 | RM = 3 << 0, // Round towards Minus Infinity. |
| 665 | |
| 666 | // Aliases. |
| 667 | kRoundToNearest = RN, |
| 668 | kRoundToZero = RZ, |
| 669 | kRoundToPlusInf = RP, |
| 670 | kRoundToMinusInf = RM |
| 671 | }; |
| 672 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 673 | const uint32_t kFPURoundingModeMask = 3 << 0; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 674 | |
| 675 | enum CheckForInexactConversion { |
| 676 | kCheckForInexactConversion, |
| 677 | kDontCheckForInexactConversion |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 678 | }; |
| 679 | |
| 680 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 681 | // ----------------------------------------------------------------------------- |
| 682 | // Hints. |
| 683 | |
| 684 | // Branch hints are not used on the MIPS. They are defined so that they can |
| 685 | // appear in shared function signatures, but will be ignored in MIPS |
| 686 | // implementations. |
| 687 | enum Hint { |
| 688 | no_hint = 0 |
| 689 | }; |
| 690 | |
| 691 | |
| 692 | inline Hint NegateHint(Hint hint) { |
| 693 | return no_hint; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | // ----------------------------------------------------------------------------- |
| 698 | // Specific instructions, constants, and masks. |
| 699 | // These constants are declared in assembler-mips.cc, as they use named |
| 700 | // registers and other constants. |
| 701 | |
| 702 | // addiu(sp, sp, 4) aka Pop() operation or part of Pop(r) |
| 703 | // operations as post-increment of sp. |
| 704 | extern const Instr kPopInstruction; |
| 705 | // addiu(sp, sp, -4) part of Push(r) operation as pre-decrement of sp. |
| 706 | extern const Instr kPushInstruction; |
| 707 | // sw(r, MemOperand(sp, 0)) |
| 708 | extern const Instr kPushRegPattern; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 709 | // lw(r, MemOperand(sp, 0)) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 710 | extern const Instr kPopRegPattern; |
| 711 | extern const Instr kLwRegFpOffsetPattern; |
| 712 | extern const Instr kSwRegFpOffsetPattern; |
| 713 | extern const Instr kLwRegFpNegOffsetPattern; |
| 714 | extern const Instr kSwRegFpNegOffsetPattern; |
| 715 | // A mask for the Rt register for push, pop, lw, sw instructions. |
| 716 | extern const Instr kRtMask; |
| 717 | extern const Instr kLwSwInstrTypeMask; |
| 718 | extern const Instr kLwSwInstrArgumentMask; |
| 719 | extern const Instr kLwSwOffsetMask; |
| 720 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 721 | // Break 0xfffff, reserved for redirected real time call. |
| 722 | const Instr rtCallRedirInstr = SPECIAL | BREAK | call_rt_redirected << 6; |
| 723 | // A nop instruction. (Encoding of sll 0 0 0). |
| 724 | const Instr nopInstr = 0; |
| 725 | |
| 726 | class Instruction { |
| 727 | public: |
| 728 | enum { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 729 | kInstrSize = 4, |
| 730 | kInstrSizeLog2 = 2, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 731 | // On MIPS PC cannot actually be directly accessed. We behave as if PC was |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 732 | // always the value of the current instruction being executed. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 733 | kPCReadOffset = 0 |
| 734 | }; |
| 735 | |
| 736 | // Get the raw instruction bits. |
| 737 | inline Instr InstructionBits() const { |
| 738 | return *reinterpret_cast<const Instr*>(this); |
| 739 | } |
| 740 | |
| 741 | // Set the raw instruction bits to value. |
| 742 | inline void SetInstructionBits(Instr value) { |
| 743 | *reinterpret_cast<Instr*>(this) = value; |
| 744 | } |
| 745 | |
| 746 | // Read one particular bit out of the instruction bits. |
| 747 | inline int Bit(int nr) const { |
| 748 | return (InstructionBits() >> nr) & 1; |
| 749 | } |
| 750 | |
| 751 | // Read a bit field out of the instruction bits. |
| 752 | inline int Bits(int hi, int lo) const { |
| 753 | return (InstructionBits() >> lo) & ((2 << (hi - lo)) - 1); |
| 754 | } |
| 755 | |
| 756 | // Instruction type. |
| 757 | enum Type { |
| 758 | kRegisterType, |
| 759 | kImmediateType, |
| 760 | kJumpType, |
| 761 | kUnsupported = -1 |
| 762 | }; |
| 763 | |
| 764 | // Get the encoding type of the instruction. |
| 765 | Type InstructionType() const; |
| 766 | |
| 767 | |
| 768 | // Accessors for the different named fields used in the MIPS encoding. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 769 | inline Opcode OpcodeValue() const { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 770 | return static_cast<Opcode>( |
| 771 | Bits(kOpcodeShift + kOpcodeBits - 1, kOpcodeShift)); |
| 772 | } |
| 773 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 774 | inline int RsValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 775 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 776 | InstructionType() == kImmediateType); |
| 777 | return Bits(kRsShift + kRsBits - 1, kRsShift); |
| 778 | } |
| 779 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 780 | inline int RtValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 781 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 782 | InstructionType() == kImmediateType); |
| 783 | return Bits(kRtShift + kRtBits - 1, kRtShift); |
| 784 | } |
| 785 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 786 | inline int RdValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 787 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 788 | return Bits(kRdShift + kRdBits - 1, kRdShift); |
| 789 | } |
| 790 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 791 | inline int SaValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 792 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 793 | return Bits(kSaShift + kSaBits - 1, kSaShift); |
| 794 | } |
| 795 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 796 | inline int FunctionValue() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 797 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 798 | InstructionType() == kImmediateType); |
| 799 | return Bits(kFunctionShift + kFunctionBits - 1, kFunctionShift); |
| 800 | } |
| 801 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 802 | inline int FdValue() const { |
| 803 | return Bits(kFdShift + kFdBits - 1, kFdShift); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 806 | inline int FsValue() const { |
| 807 | return Bits(kFsShift + kFsBits - 1, kFsShift); |
| 808 | } |
| 809 | |
| 810 | inline int FtValue() const { |
| 811 | return Bits(kFtShift + kFtBits - 1, kFtShift); |
| 812 | } |
| 813 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 814 | inline int FrValue() const { |
| 815 | return Bits(kFrShift + kFrBits -1, kFrShift); |
| 816 | } |
| 817 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 818 | // Float Compare condition code instruction bits. |
| 819 | inline int FCccValue() const { |
| 820 | return Bits(kFCccShift + kFCccBits - 1, kFCccShift); |
| 821 | } |
| 822 | |
| 823 | // Float Branch condition code instruction bits. |
| 824 | inline int FBccValue() const { |
| 825 | return Bits(kFBccShift + kFBccBits - 1, kFBccShift); |
| 826 | } |
| 827 | |
| 828 | // Float Branch true/false instruction bit. |
| 829 | inline int FBtrueValue() const { |
| 830 | return Bits(kFBtrueShift + kFBtrueBits - 1, kFBtrueShift); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // Return the fields at their original place in the instruction encoding. |
| 834 | inline Opcode OpcodeFieldRaw() const { |
| 835 | return static_cast<Opcode>(InstructionBits() & kOpcodeMask); |
| 836 | } |
| 837 | |
| 838 | inline int RsFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 839 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 840 | InstructionType() == kImmediateType); |
| 841 | return InstructionBits() & kRsFieldMask; |
| 842 | } |
| 843 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 844 | // Same as above function, but safe to call within InstructionType(). |
| 845 | inline int RsFieldRawNoAssert() const { |
| 846 | return InstructionBits() & kRsFieldMask; |
| 847 | } |
| 848 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 849 | inline int RtFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 850 | DCHECK(InstructionType() == kRegisterType || |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 851 | InstructionType() == kImmediateType); |
| 852 | return InstructionBits() & kRtFieldMask; |
| 853 | } |
| 854 | |
| 855 | inline int RdFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 856 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 857 | return InstructionBits() & kRdFieldMask; |
| 858 | } |
| 859 | |
| 860 | inline int SaFieldRaw() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 861 | DCHECK(InstructionType() == kRegisterType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 862 | return InstructionBits() & kSaFieldMask; |
| 863 | } |
| 864 | |
| 865 | inline int FunctionFieldRaw() const { |
| 866 | return InstructionBits() & kFunctionFieldMask; |
| 867 | } |
| 868 | |
| 869 | // Get the secondary field according to the opcode. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 870 | inline int SecondaryValue() const { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 871 | Opcode op = OpcodeFieldRaw(); |
| 872 | switch (op) { |
| 873 | case SPECIAL: |
| 874 | case SPECIAL2: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 875 | return FunctionValue(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 876 | case COP1: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 877 | return RsValue(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 878 | case REGIMM: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 879 | return RtValue(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 880 | default: |
| 881 | return NULLSF; |
| 882 | } |
| 883 | } |
| 884 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 885 | inline int32_t Imm16Value() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 886 | DCHECK(InstructionType() == kImmediateType); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 887 | return Bits(kImm16Shift + kImm16Bits - 1, kImm16Shift); |
| 888 | } |
| 889 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 890 | inline int32_t Imm21Value() const { |
| 891 | DCHECK(InstructionType() == kImmediateType); |
| 892 | return Bits(kImm21Shift + kImm21Bits - 1, kImm21Shift); |
| 893 | } |
| 894 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 895 | inline int32_t Imm26Value() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 896 | DCHECK(InstructionType() == kJumpType); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 897 | return Bits(kImm26Shift + kImm26Bits - 1, kImm26Shift); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // Say if the instruction should not be used in a branch delay slot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 901 | bool IsForbiddenInBranchDelay() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 902 | // Say if the instruction 'links'. e.g. jal, bal. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 903 | bool IsLinkingInstruction() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 904 | // Say if the instruction is a break or a trap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 905 | bool IsTrap() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 906 | |
| 907 | // Instructions are read of out a code stream. The only way to get a |
| 908 | // reference to an instruction is to convert a pointer. There is no way |
| 909 | // to allocate or create instances of class Instruction. |
| 910 | // Use the At(pc) function to create references to Instruction. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 911 | static Instruction* At(byte* pc) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 912 | return reinterpret_cast<Instruction*>(pc); |
| 913 | } |
| 914 | |
| 915 | private: |
| 916 | // We need to prevent the creation of instances of class Instruction. |
| 917 | DISALLOW_IMPLICIT_CONSTRUCTORS(Instruction); |
| 918 | }; |
| 919 | |
| 920 | |
| 921 | // ----------------------------------------------------------------------------- |
| 922 | // MIPS assembly various constants. |
| 923 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 924 | // C/C++ argument slots size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 925 | const int kCArgSlotCount = 4; |
| 926 | const int kCArgsSlotsSize = kCArgSlotCount * Instruction::kInstrSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 927 | // JS argument slots size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 928 | const int kJSArgsSlotsSize = 0 * Instruction::kInstrSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 929 | // Assembly builtins argument slots size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 930 | const int kBArgsSlotsSize = 0 * Instruction::kInstrSize; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 931 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 932 | const int kBranchReturnOffset = 2 * Instruction::kInstrSize; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 933 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 934 | } } // namespace v8::internal |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 935 | |
| 936 | #endif // #ifndef V8_MIPS_CONSTANTS_H_ |